feat:1.优化commonResult 2. 重新初始化数据库结构
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package com.guo.learningprogresstracker.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebCorsConfig implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**") // 配置所有路径
|
||||
.allowedOrigins("http://localhost:5173")
|
||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
||||
.allowedHeaders("*")
|
||||
.allowCredentials(true);
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,6 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
/**
|
||||
* @Author: ChangYu
|
||||
* @Version 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* 拦截器
|
||||
@@ -23,10 +19,7 @@ public class Interceptor implements HandlerInterceptor {
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
/**
|
||||
* token过期或失效时,由于前端基本都是AJAX请求,拦截器返回401,由前端负责跳转至OA登陆页面
|
||||
* note:chang yu
|
||||
*/
|
||||
|
||||
// StpUtil.checkLogin();
|
||||
// //续签
|
||||
// StpUtil.updateLastActiveToNow();
|
||||
|
||||
@@ -26,7 +26,7 @@ public class LoginController {
|
||||
|
||||
@Operation(summary = "登录API")
|
||||
@PostMapping("/login")
|
||||
public CommonResult<Object> login(@RequestBody LoginBody loginBody) throws AppException {
|
||||
public CommonResult<String> login(@RequestBody LoginBody loginBody) throws AppException {
|
||||
String userId = userServiceImpl.authenticate(loginBody.getUsername(), loginBody.getPassword());
|
||||
String token = loginService.login(userId);
|
||||
//todo 参数传递未加密
|
||||
@@ -35,7 +35,7 @@ public class LoginController {
|
||||
|
||||
@Operation(summary = "退出登录API-功能未完成")
|
||||
@PostMapping("/loingOut")
|
||||
public CommonResult loingOut(@RequestBody LoginBody loginBody) throws AppException {
|
||||
public CommonResult<Void> loingOut(@RequestBody LoginBody loginBody) throws AppException {
|
||||
//todo loginOut 退出登录API
|
||||
// loginService.loginOut();
|
||||
return CommonResult.success("已登出");
|
||||
|
||||
+7
-6
@@ -41,7 +41,7 @@ public class StudySessionController {
|
||||
* 通过sessionNum获取一个【学习会话】
|
||||
*/
|
||||
@GetMapping("/{sessionNum}")
|
||||
public CommonResult getStudySessionBySessionNum(@NotEmpty(message = "sessionNum不可为空") @PathVariable String sessionNum) throws ErrorParameterException {
|
||||
public CommonResult<StudySessionResponce> getStudySessionBySessionNum(@NotEmpty(message = "sessionNum不可为空") @PathVariable String sessionNum) throws ErrorParameterException {
|
||||
StudySessionsEntity studySessionsEntity = studySessionsServiceImpl.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
|
||||
.eq(StudySessionsEntity::getSessionNum, sessionNum))
|
||||
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
|
||||
@@ -53,7 +53,7 @@ public class StudySessionController {
|
||||
* 暂停一个【学习会话】
|
||||
*/
|
||||
@PostMapping("/{sessionNum}/study-sessions/pause")
|
||||
public CommonResult pauseStudySession(@PathVariable("sessionNum") String sessionNum) throws ErrorParameterException {
|
||||
public CommonResult<Void> pauseStudySession(@PathVariable("sessionNum") String sessionNum) throws ErrorParameterException {
|
||||
studySessionsServiceImpl.pauseStudySession(sessionNum);
|
||||
return CommonResult.success();
|
||||
}
|
||||
@@ -62,16 +62,17 @@ public class StudySessionController {
|
||||
* 结束一个【学习会话】
|
||||
*/
|
||||
@PostMapping("/{sessionNum}/study-sessions/ended")
|
||||
public CommonResult endedStudySession(@PathVariable("sessionNum") String sessionNum,
|
||||
@Valid @RequestBody EndedStudySessionRequest request) throws ErrorParameterException {
|
||||
studySessionsServiceImpl.endedStudySession(sessionNum,request.getContent());
|
||||
public CommonResult<Void> endedStudySession(@PathVariable("sessionNum") String sessionNum,
|
||||
@Valid @RequestBody EndedStudySessionRequest request) throws ErrorParameterException {
|
||||
studySessionsServiceImpl.endedStudySession(sessionNum, request.getContent());
|
||||
return CommonResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取【学习会话】所有的学习残片数据,以列表返回
|
||||
*/
|
||||
@GetMapping("/{sessionNum}/all-fragments")
|
||||
public CommonResult getAllFragments(@PathVariable("sessionNum") String sessionNum) throws ErrorParameterException {
|
||||
public CommonResult<ArrayList<String>> getAllFragments(@PathVariable("sessionNum") String sessionNum) throws ErrorParameterException {
|
||||
ArrayList<String> allFragments = studySessionsServiceImpl.getAllFragments(sessionNum);
|
||||
return CommonResult.success(allFragments);
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.guo.learningprogresstracker.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.guo.learningprogresstracker.common.Ops;
|
||||
import com.guo.learningprogresstracker.dto.TaskInfo;
|
||||
import com.guo.learningprogresstracker.dto.response.StudySessionResponce;
|
||||
import com.guo.learningprogresstracker.dto.response.TaskInfoResponse;
|
||||
import com.guo.learningprogresstracker.entity.CommonResult;
|
||||
import com.guo.learningprogresstracker.dto.request.TaskRequest;
|
||||
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
||||
@@ -27,6 +30,7 @@ import java.rmi.ServerException;
|
||||
|
||||
/**
|
||||
* 任务控制层
|
||||
*
|
||||
* @author guo
|
||||
*/
|
||||
@RequestMapping("/tasks")
|
||||
@@ -41,44 +45,43 @@ public class TaskController {
|
||||
|
||||
@PostMapping
|
||||
@Operation(summary = "添加新任务")
|
||||
public CommonResult addTask(@RequestBody @Validated({Ops.CreateG.class}) TaskRequest taskRequest) throws ErrorParameterException {
|
||||
public CommonResult<String> addTask(@RequestBody @Validated({Ops.CreateG.class}) TaskRequest taskRequest) throws ErrorParameterException {
|
||||
return CommonResult.success(tasksService.addTask(taskRequest));
|
||||
}
|
||||
|
||||
@Operation(summary = "任务详情API")
|
||||
@GetMapping("/{taskId}")
|
||||
public CommonResult getTask(@PathVariable("taskId") String taskId) {
|
||||
public CommonResult<TaskInfoResponse> getTask(@PathVariable("taskId") String taskId) {
|
||||
return CommonResult.success(tasksService.getTask(taskId));
|
||||
}
|
||||
|
||||
@PutMapping("/{taskId}")
|
||||
@Operation(summary = "更新任务详情")
|
||||
public CommonResult updateTask(@PathVariable("taskId") String taskId,@Validated(Ops.UpdateG.class) @RequestBody TaskRequest updatedTask) {
|
||||
public CommonResult<Void> updateTask(@PathVariable("taskId") String taskId, @Validated(Ops.UpdateG.class) @RequestBody TaskRequest updatedTask) {
|
||||
tasksService.updateTask(taskId, updatedTask);
|
||||
return CommonResult.success();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{taskId}")
|
||||
@Operation(summary = "删除指定任务")
|
||||
public CommonResult deleteTask(@PathVariable("taskId") String taskId) throws ServerException {
|
||||
public CommonResult<Void> deleteTask(@PathVariable("taskId") String taskId) throws ServerException {
|
||||
tasksService.deleteTask(taskId);
|
||||
return CommonResult.success();
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "获取所有任务列表")
|
||||
public CommonResult tasksList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
|
||||
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
|
||||
public CommonResult<Page<TaskInfo>> tasksList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
|
||||
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
|
||||
return CommonResult.success(tasksService.taskList(pageNum, pageSize));
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始或继续一个【学习会话】
|
||||
*
|
||||
*/
|
||||
@GetMapping("/{taskNum}/study-sessions/start-or-continue")
|
||||
public CommonResult startOrContinueStudySession(@NotEmpty(message = "taskNum不可为空")
|
||||
@PathVariable String taskNum) throws NotFindEntitiesException {
|
||||
public CommonResult<StudySessionResponce> startOrContinueStudySession(@NotEmpty(message = "taskNum不可为空")
|
||||
@PathVariable String taskNum) throws NotFindEntitiesException {
|
||||
StudySessionResponce response = studySessionsServiceImpl.startOrContinueStudySession(taskNum);
|
||||
return CommonResult.success(response);
|
||||
|
||||
@@ -88,8 +91,8 @@ public class TaskController {
|
||||
* 通过学习任务num获取该任务的未结束会话
|
||||
*/
|
||||
@GetMapping("/{taskNum}/not-ended-study-session")
|
||||
public CommonResult getNotEndedStudySessionByTaskNum(@NotEmpty(message = "taskNum不可为空")
|
||||
@PathVariable String taskNum) throws ErrorParameterException {
|
||||
public CommonResult<StudySessionResponce> getNotEndedStudySessionByTaskNum(@NotEmpty(message = "taskNum不可为空")
|
||||
@PathVariable String taskNum) throws ErrorParameterException {
|
||||
StudySessionResponce response = studySessionsServiceImpl.getNotEndedStudySessionByTaskNum(taskNum);
|
||||
return CommonResult.success(response);
|
||||
}
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ public class reportFragmentsController {
|
||||
* @return
|
||||
*/
|
||||
@PostMapping
|
||||
public CommonResult createFragments(@Valid @RequestBody CreateFragmentsRequest request) throws NotFindEntitiesException {
|
||||
public CommonResult<Void> createFragments(@Valid @RequestBody CreateFragmentsRequest request) throws NotFindEntitiesException {
|
||||
studyReportFragmentsServiceImpl.createFragments(request);
|
||||
return CommonResult.success();
|
||||
}
|
||||
|
||||
@@ -1,54 +1,50 @@
|
||||
package com.guo.learningprogresstracker.entity;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class CommonResult<T> {
|
||||
private Integer code;
|
||||
|
||||
private String message;
|
||||
|
||||
private T data;
|
||||
|
||||
public CommonResult(Integer code, String message){
|
||||
this(code,message, null);
|
||||
public CommonResult(Integer code, String message) {
|
||||
this(code, message, null);
|
||||
}
|
||||
|
||||
public static CommonResult<String> success(){
|
||||
return new CommonResult(200,"请求成功",null);
|
||||
public static <T> CommonResult<T> success() {
|
||||
return new CommonResult<>(200, "请求成功", null);
|
||||
}
|
||||
|
||||
public static CommonResult<String> success(String message){
|
||||
return new CommonResult(200,message,null);
|
||||
public static <T> CommonResult<T> success(String message) {
|
||||
return new CommonResult<>(200, message, null);
|
||||
}
|
||||
|
||||
public static CommonResult<Object> success(String message,Object data){
|
||||
return new CommonResult(200,message,data);
|
||||
public static <T> CommonResult<T> success(T data) {
|
||||
return new CommonResult<>(200, "请求成功", data);
|
||||
}
|
||||
|
||||
public static CommonResult success(Object data){
|
||||
return new CommonResult(200,"请求成功",data);
|
||||
public static <T> CommonResult<T> success(String message, T data) {
|
||||
return new CommonResult<>(200, message, data);
|
||||
}
|
||||
|
||||
public static CommonResult<String> error(String message){
|
||||
return new CommonResult<>(400,message,null);
|
||||
public static <T> CommonResult<T> error(String message) {
|
||||
return new CommonResult<>(400, message, null);
|
||||
}
|
||||
|
||||
public static CommonResult<String> notFind(){
|
||||
return new CommonResult<>(404,"请求的资源不存在",null);
|
||||
}
|
||||
public static CommonResult<String> notFind(String message){
|
||||
return new CommonResult<>(404,message,null);
|
||||
public static <T> CommonResult<T> notFind() {
|
||||
return new CommonResult<>(404, "请求的资源不存在", null);
|
||||
}
|
||||
|
||||
public static CommonResult<String> serverError(String message){
|
||||
return new CommonResult<>(500,message,null);
|
||||
public static <T> CommonResult<T> notFind(String message) {
|
||||
return new CommonResult<>(404, message, null);
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> serverError(String message) {
|
||||
return new CommonResult<>(500, message, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.guo.learningprogresstracker.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@@ -8,23 +7,19 @@ import com.guo.learningprogresstracker.dto.PriorityDto;
|
||||
import com.guo.learningprogresstracker.dto.TaskInfo;
|
||||
import com.guo.learningprogresstracker.dto.request.TaskRequest;
|
||||
import com.guo.learningprogresstracker.dto.response.TaskInfoResponse;
|
||||
import com.guo.learningprogresstracker.dto.response.TasksListInfoResponse;
|
||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
||||
import com.guo.learningprogresstracker.mapStruct.RequestConvert;
|
||||
import com.guo.learningprogresstracker.mapStruct.TaskConvert;
|
||||
import com.guo.learningprogresstracker.mapStruct.TaskConvertImpl;
|
||||
import com.guo.learningprogresstracker.service.TasksService;
|
||||
import com.guo.learningprogresstracker.mapper.TasksMapper;
|
||||
import com.guo.learningprogresstracker.utils.CalculatedPriorityTool;
|
||||
import com.guo.learningprogresstracker.utils.GenerateNumTool;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.exceptions.TooManyResultsException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.rmi.ServerException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author guo
|
||||
@@ -44,7 +39,7 @@ public class TasksServiceImpl extends ServiceImpl<TasksMapper, TaskEntity>
|
||||
public Page<TaskInfo> taskList(Integer pageNum, Integer pageSize) {
|
||||
Page<TaskEntity> page = tasksMapper.selectPage(new Page<TaskEntity>(pageNum, pageSize), Wrappers.lambdaQuery(TaskEntity.class));
|
||||
Page<TaskInfo> response = TaskConvert.MAPPER.toTaskInfoPage(page);
|
||||
|
||||
// todo 需要补充TaskInfo中的lastLearningStatus字段信息
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@ class LoginControllerTest {
|
||||
LoginBody loginBody = new LoginBody();
|
||||
loginBody.setUsername("admin");
|
||||
loginBody.setPassword("admin");
|
||||
CommonResult<Object> login = loginController.login(loginBody);
|
||||
assertEquals(login.getMessage(), "登录成功!");
|
||||
CommonResult<String> login = loginController.login(loginBody);
|
||||
assertEquals("登录成功!", login.getMessage());
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user