From 67b26776b582ba7af59d589ed31368477b1f7a0c Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 12 Jan 2024 16:33:05 +0800 Subject: [PATCH] 1.uniform response status code for comment component --- .../comment/controller/CommentController.java | 59 ++++++++++--------- luoo_common/src/main/java/api/StatusCode.java | 1 + 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java index 9d7fd3c..36f4504 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java @@ -3,9 +3,12 @@ package com.luoo.comment.controller; import com.luoo.comment.pojo.Comment; import com.luoo.comment.service.CommentService; -import entity.PageResult; -import entity.Result; -import entity.StatusCode; +import api.PageResult; +import api.Result; +import api.StatusCode; + +import java.util.List; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.redis.core.RedisTemplate; @@ -22,64 +25,62 @@ public class CommentController { @Autowired private RedisTemplate redisTemplate; - @RequestMapping(method = RequestMethod.GET) - public Result findAll(){ - return new Result(true, StatusCode.OK,"",commentService.findAll()); + @GetMapping + public Result> findAll(){ + return Result.success(commentService.findAll()); } - @RequestMapping(value = "/findByVolid/{volid}",method = RequestMethod.GET) - public Result findByVolid(@PathVariable String volid){ - return new Result(true,StatusCode.OK,"查询成功",commentService.findByVolid(volid)); + @GetMapping("/findByVolid/{volid}") + public Result> findByVolid(@PathVariable String volid){ + return Result.success(commentService.findByVolid(volid)); } - @RequestMapping(value = "/{commentId}",method = RequestMethod.GET) - public Result findById(@PathVariable String commentId) { - return new Result(true, StatusCode.OK,"",commentService.findById(commentId)); + @GetMapping("/{commentId}") + public Result findById(@PathVariable String commentId) { + return Result.success(commentService.findById(commentId)); } - @RequestMapping(method = RequestMethod.POST) - public Result save(@RequestBody Comment comment){ + @PostMapping + public Result save(@RequestBody Comment comment){ commentService.save(comment); - return new Result(true,StatusCode.OK,"保存成功"); + return Result.success(); } - @RequestMapping(value = "/{commentId}",method = RequestMethod.PUT) - public Result update(@PathVariable String commentId,@RequestBody Comment comment){ + @PutMapping("/{commentId}") + public Result update(@PathVariable String commentId,@RequestBody Comment comment){ comment.set_id(commentId); commentService.update(comment); - return new Result(true,StatusCode.OK,"修改成功"); + return Result.success(); } - @RequestMapping(value = "/{commentId}",method = RequestMethod.DELETE) - public Result delete(@PathVariable String commentId){ + @DeleteMapping("/{commentId}") + public Result delete(@PathVariable String commentId){ commentService.deleteById(commentId); - return new Result(true,StatusCode.OK,"删除成功"); + return Result.success(); } - @RequestMapping(value = "/{parentid}/{page}/{size}",method = RequestMethod.GET) - public Result findByParentid(@PathVariable String parentid,@PathVariable int page,@PathVariable int size){ + @GetMapping("/{parentid}/{page}/{size}") + public Result> findByParentid(@PathVariable String parentid,@PathVariable int page,@PathVariable int size){ Page pageData = commentService.findByParentId(parentid,page,size); - - return new Result(true,StatusCode.OK,"查询成功",new PageResult(pageData.getTotalElements(),pageData.getContent())); + return Result.success(new PageResult(pageData.getTotalElements(),pageData.getContent())); } - @RequestMapping(value = "/thumbup/{commentId}",method = RequestMethod.PUT) + @PutMapping("/thumbup/{commentId}") public Result thumbup(@PathVariable String commentId){ - String userid="111"; if (redisTemplate.opsForValue().get("thumbup_"+userid)!=null){ - return new Result(true,StatusCode.REPERROR,"重复提交"); + return Result.failed(StatusCode.COMMENT_REPEAT_SUBMIT); } commentService.thumbup(commentId); redisTemplate.opsForValue().set("thumbup_"+userid,1); - return new Result(true,StatusCode.OK,"点赞成功"); + return Result.success(); } } diff --git a/luoo_common/src/main/java/api/StatusCode.java b/luoo_common/src/main/java/api/StatusCode.java index b8bd5ce..04e42b8 100644 --- a/luoo_common/src/main/java/api/StatusCode.java +++ b/luoo_common/src/main/java/api/StatusCode.java @@ -17,6 +17,7 @@ public enum StatusCode implements IErrorCode { MUSIC_COMMON_FAILED(20000, "歌曲模块失败"), // comment 模块错误码以30XXX,不足5位补0; COMMENT_COMMON_FAILED(30000, "评论模块失败"), + COMMENT_REPEAT_SUBMIT(30001, "评论重复提交"), // friend 模块错误码以40XXX,不足5位补0; FRIEND_COMMON_FAILED(40000, "交友模块失败"), // tag 模块错误码以50XXX,不足5位补0;