mirror of
https://github.com/vran-dev/databasir.git
synced 2025-09-19 18:19:26 +08:00
feat: auto record operation log
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.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,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,22 @@
|
||||
package com.databasir.core.domain.log.service;
|
||||
|
||||
import com.databasir.core.domain.log.converter.OperationLogRequestConverter;
|
||||
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.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class OperationLogService {
|
||||
|
||||
private final OperationLogDao operationLogDao;
|
||||
|
||||
private final OperationLogRequestConverter operationLogRequestConverter;
|
||||
|
||||
public void save(OperationLogRequest request) {
|
||||
OperationLogPojo pojo = operationLogRequestConverter.toPojo(request);
|
||||
operationLogDao.insertAndReturnId(pojo);
|
||||
}
|
||||
}
|
@@ -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,22 @@ public class JsonConverter {
|
||||
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