mirror of
https://github.com/vran-dev/databasir.git
synced 2025-08-08 17:32:14 +08:00
Feature: add operation log (#16)
* feat: auto record operation log * feat: add list project operation log api * feat: update frontend resource
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package com.databasir.core.domain.log.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Operation {
|
||||
|
||||
String module();
|
||||
|
||||
String name() default "UNKNOWN";
|
||||
|
||||
/**
|
||||
* @return the Spring-EL expression
|
||||
*/
|
||||
String involvedProjectId() default "N/A";
|
||||
|
||||
/**
|
||||
* @return the Spring-EL expression
|
||||
*/
|
||||
String involvedGroupId() default "N/A";
|
||||
|
||||
/**
|
||||
* @return the Spring-EL expression
|
||||
*/
|
||||
String involvedUserId() default "N/A";
|
||||
|
||||
interface Modules {
|
||||
String UNKNOWN = "UNKNOWN";
|
||||
String PROJECT = "project";
|
||||
String USER = "user";
|
||||
String GROUP = "group";
|
||||
String SETTING = "setting";
|
||||
}
|
||||
|
||||
interface Types {
|
||||
int SYSTEM_USER_ID = -1;
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package com.databasir.core.domain.log.converter;
|
||||
|
||||
import com.databasir.core.domain.log.data.OperationLogPageResponse;
|
||||
import com.databasir.core.infrastructure.converter.JsonConverter;
|
||||
import com.databasir.dao.tables.pojos.OperationLogPojo;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
@Mapper(componentModel = "spring", uses = JsonConverter.class)
|
||||
public interface OperationLogPojoConverter {
|
||||
|
||||
OperationLogPageResponse to(OperationLogPojo pojo);
|
||||
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package com.databasir.core.domain.log.converter;
|
||||
|
||||
import com.databasir.core.domain.log.data.OperationLogRequest;
|
||||
import com.databasir.core.infrastructure.converter.JsonConverter;
|
||||
import com.databasir.dao.tables.pojos.OperationLogPojo;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
@Mapper(componentModel = "spring", uses = JsonConverter.class)
|
||||
public interface OperationLogRequestConverter {
|
||||
|
||||
OperationLogPojo toPojo(OperationLogRequest request);
|
||||
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
package com.databasir.core.domain.log.data;
|
||||
|
||||
import com.databasir.dao.Tables;
|
||||
import lombok.Data;
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.impl.DSL;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class OperationLogPageCondition {
|
||||
|
||||
private String module;
|
||||
|
||||
private String code;
|
||||
|
||||
private Integer operatorUserId;
|
||||
|
||||
private Integer involveProjectId;
|
||||
|
||||
private Integer involveGroupId;
|
||||
|
||||
private Integer involveUserId;
|
||||
|
||||
private Boolean isSuccess;
|
||||
|
||||
public Condition toCondition() {
|
||||
List<Condition> conditions = new ArrayList<>();
|
||||
if (module != null) {
|
||||
conditions.add(Tables.OPERATION_LOG.OPERATION_MODULE.eq(module));
|
||||
}
|
||||
if (code != null) {
|
||||
conditions.add(Tables.OPERATION_LOG.OPERATION_CODE.eq(module));
|
||||
}
|
||||
if (operatorUserId != null) {
|
||||
conditions.add(Tables.OPERATION_LOG.OPERATOR_USER_ID.eq(operatorUserId));
|
||||
}
|
||||
if (involveProjectId != null) {
|
||||
conditions.add(Tables.OPERATION_LOG.INVOLVED_PROJECT_ID.eq(involveProjectId));
|
||||
}
|
||||
if (involveGroupId != null) {
|
||||
conditions.add(Tables.OPERATION_LOG.INVOLVED_GROUP_ID.eq(involveGroupId));
|
||||
}
|
||||
if (involveUserId != null) {
|
||||
conditions.add(Tables.OPERATION_LOG.INVOLVED_USER_ID.eq(involveUserId));
|
||||
}
|
||||
if (isSuccess != null) {
|
||||
conditions.add(Tables.OPERATION_LOG.IS_SUCCESS.eq(isSuccess));
|
||||
}
|
||||
return conditions.stream()
|
||||
.reduce(Condition::and)
|
||||
.orElse(DSL.trueCondition());
|
||||
}
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
package com.databasir.core.domain.log.data;
|
||||
|
||||
import com.databasir.common.JsonData;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class OperationLogPageResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private Integer operatorUserId;
|
||||
|
||||
private String operatorUsername;
|
||||
|
||||
private String operatorNickname;
|
||||
|
||||
private String operationModule;
|
||||
|
||||
private String operationCode;
|
||||
|
||||
private String operationName;
|
||||
|
||||
private JsonData<Object> operationResponse;
|
||||
|
||||
private Boolean isSuccess;
|
||||
|
||||
private Integer involvedProjectId;
|
||||
|
||||
private Integer involvedGroupId;
|
||||
|
||||
private Integer involvedUserId;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package com.databasir.core.domain.log.data;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class OperationLogRequest {
|
||||
|
||||
private Integer operatorUserId;
|
||||
|
||||
private String operatorUsername;
|
||||
|
||||
private String operatorNickname;
|
||||
|
||||
private String operationModule;
|
||||
|
||||
private String operationCode;
|
||||
|
||||
private String operationName;
|
||||
|
||||
private Object operationResponse;
|
||||
|
||||
private Boolean isSuccess;
|
||||
|
||||
private Integer involvedProjectId;
|
||||
|
||||
private Integer involvedGroupId;
|
||||
|
||||
private Integer involvedUserId;
|
||||
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
package com.databasir.core.domain.log.service;
|
||||
|
||||
import com.databasir.core.domain.log.converter.OperationLogPojoConverter;
|
||||
import com.databasir.core.domain.log.converter.OperationLogRequestConverter;
|
||||
import com.databasir.core.domain.log.data.OperationLogPageCondition;
|
||||
import com.databasir.core.domain.log.data.OperationLogPageResponse;
|
||||
import com.databasir.core.domain.log.data.OperationLogRequest;
|
||||
import com.databasir.dao.impl.OperationLogDao;
|
||||
import com.databasir.dao.tables.pojos.OperationLogPojo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class OperationLogService {
|
||||
|
||||
private final OperationLogDao operationLogDao;
|
||||
|
||||
private final OperationLogRequestConverter operationLogRequestConverter;
|
||||
|
||||
private final OperationLogPojoConverter operationLogPojoConverter;
|
||||
|
||||
public void save(OperationLogRequest request) {
|
||||
OperationLogPojo pojo = operationLogRequestConverter.toPojo(request);
|
||||
operationLogDao.insertAndReturnId(pojo);
|
||||
}
|
||||
|
||||
public Page<OperationLogPageResponse> list(Pageable page,
|
||||
OperationLogPageCondition condition) {
|
||||
Page<OperationLogPojo> pojoData = operationLogDao.selectByPage(page, condition.toCondition());
|
||||
return pojoData.map(operationLogPojoConverter::to);
|
||||
}
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
package com.databasir.core.infrastructure.converter;
|
||||
|
||||
import com.databasir.common.JsonData;
|
||||
import com.databasir.core.domain.document.data.DatabaseDocumentResponse;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
@@ -20,12 +21,8 @@ public class JsonConverter {
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
public JSON toJson(List<String> array) {
|
||||
try {
|
||||
String json = objectMapper.writeValueAsString(array);
|
||||
return JSON.valueOf(json);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
String json = objToJson(array);
|
||||
return JSON.valueOf(json);
|
||||
}
|
||||
|
||||
public List<String> fromJson(JSON json) {
|
||||
@@ -44,12 +41,8 @@ public class JsonConverter {
|
||||
}
|
||||
|
||||
public JSON toJson(DatabaseDocumentResponse response) {
|
||||
try {
|
||||
String json = objectMapper.writeValueAsString(response);
|
||||
return JSON.valueOf(json);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
String json = objToJson(response);
|
||||
return JSON.valueOf(json);
|
||||
}
|
||||
|
||||
public DatabaseDocumentResponse of(JSON json) {
|
||||
@@ -62,4 +55,33 @@ public class JsonConverter {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public JsonData toJsonData(JSON json) {
|
||||
try {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
return objectMapper.readValue(json.data().getBytes(StandardCharsets.UTF_8), JsonData.class);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public JSON toJson(JsonData<Object> data) {
|
||||
String json = objToJson(data);
|
||||
return JSON.valueOf(json);
|
||||
}
|
||||
|
||||
public JSON objToJsonData(Object obj) {
|
||||
String json = objToJson(obj);
|
||||
return JSON.valueOf(json);
|
||||
}
|
||||
|
||||
private String objToJson(Object obj) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user