6 changed files with 143 additions and 16 deletions
-
52src/main/java/com/dreamworks/boditech/controller/UserController.java
-
23src/main/java/com/dreamworks/boditech/entity/ActionLogEntry.java
-
15src/main/java/com/dreamworks/boditech/mapper/ActionLogMapper.java
-
38src/main/java/com/dreamworks/boditech/service/ActionLogService.java
-
14src/main/java/com/dreamworks/boditech/service/UserService.java
-
17src/main/java/com/dreamworks/boditech/utils/MyExceptionHandler.java
@ -0,0 +1,23 @@ |
|||
package com.dreamworks.boditech.entity; |
|||
import com.dreamworks.boditech.mapper.ActionLogMapper; |
|||
public class ActionLogEntry { |
|||
public Integer id; |
|||
public Integer userId; |
|||
public String action; |
|||
public String status; |
|||
public String params; |
|||
public String result; |
|||
public String startedAt; |
|||
public String finishedAt; |
|||
|
|||
// action log mapper |
|||
private ActionLogMapper mapper; |
|||
|
|||
// constructor |
|||
public ActionLogEntry(ActionLogMapper mapper) { |
|||
this.mapper = mapper; |
|||
} |
|||
|
|||
public void done() {} |
|||
public void error() {} |
|||
} |
@ -0,0 +1,15 @@ |
|||
package com.dreamworks.boditech.mapper; |
|||
import com.dreamworks.boditech.entity.ActionLogEntry; |
|||
import org.apache.ibatis.annotations.Insert; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
import org.apache.ibatis.annotations.Options; |
|||
|
|||
@Mapper |
|||
public interface ActionLogMapper { |
|||
@Insert( |
|||
"INSERT INTO bdt_action_log (userId, action, params, startedAt) VALUES " + |
|||
"(#{userId}, #{action}, #{params}, #{startedAt})" |
|||
) |
|||
@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id") |
|||
void insert(ActionLogEntry entry); |
|||
} |
@ -0,0 +1,38 @@ |
|||
package com.dreamworks.boditech.service; |
|||
import com.dreamworks.boditech.entity.ActionLogEntry; |
|||
import com.dreamworks.boditech.mapper.ActionLogMapper; |
|||
import jakarta.annotation.Resource; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.time.LocalDateTime; |
|||
import java.time.format.DateTimeFormatter; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
@Service |
|||
public class ActionLogService { |
|||
@Resource |
|||
private ActionLogMapper actionLogMapper; |
|||
// user id |
|||
private Integer userId = 0; |
|||
|
|||
// set user id |
|||
public void setUserId( Integer userId ) { |
|||
this.userId = userId; |
|||
} |
|||
|
|||
// log action start |
|||
public void log( String name, Object ... params ) { |
|||
ActionLogEntry entry = new ActionLogEntry(this.actionLogMapper); |
|||
entry.userId = this.userId; |
|||
entry.action = name; |
|||
|
|||
// join params to string |
|||
List<String> paramList = new ArrayList<>(); |
|||
for ( Object param : params ) { |
|||
paramList.add(param.toString()); |
|||
} |
|||
entry.params = String.join(",", paramList); |
|||
entry.startedAt = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); |
|||
this.actionLogMapper.insert(entry); |
|||
} |
|||
} |
@ -0,0 +1,17 @@ |
|||
package com.dreamworks.boditech.utils; |
|||
import com.dreamworks.boditech.controller.entity.ApiResponse; |
|||
import org.springframework.web.bind.annotation.ControllerAdvice; |
|||
import org.springframework.web.bind.annotation.ExceptionHandler; |
|||
import org.springframework.web.bind.annotation.ResponseBody; |
|||
@ControllerAdvice |
|||
public class MyExceptionHandler { |
|||
@ExceptionHandler(value = RuntimeException.class) |
|||
@ResponseBody |
|||
public ApiResponse handleRuntimeException(RuntimeException e ) { |
|||
ApiResponse response = new ApiResponse(); |
|||
response.success = false; |
|||
response.message = e.getMessage(); |
|||
response.data = null; |
|||
return response; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue