1.uniform response status code for comment component

main
Gary 1 year ago
parent c7414e18e7
commit 67b26776b5

@ -3,9 +3,12 @@ package com.luoo.comment.controller;
import com.luoo.comment.pojo.Comment; import com.luoo.comment.pojo.Comment;
import com.luoo.comment.service.CommentService; import com.luoo.comment.service.CommentService;
import entity.PageResult; import api.PageResult;
import entity.Result; import api.Result;
import entity.StatusCode; import api.StatusCode;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
@ -22,64 +25,62 @@ public class CommentController {
@Autowired @Autowired
private RedisTemplate redisTemplate; private RedisTemplate redisTemplate;
@RequestMapping(method = RequestMethod.GET) @GetMapping
public Result findAll(){ public Result<List<Comment>> findAll(){
return new Result(true, StatusCode.OK,"",commentService.findAll()); return Result.success(commentService.findAll());
} }
@RequestMapping(value = "/findByVolid/{volid}",method = RequestMethod.GET) @GetMapping("/findByVolid/{volid}")
public Result findByVolid(@PathVariable String volid){ public Result<List<Comment>> findByVolid(@PathVariable String volid){
return new Result(true,StatusCode.OK,"查询成功",commentService.findByVolid(volid)); return Result.success(commentService.findByVolid(volid));
} }
@RequestMapping(value = "/{commentId}",method = RequestMethod.GET) @GetMapping("/{commentId}")
public Result findById(@PathVariable String commentId) { public Result<Comment> findById(@PathVariable String commentId) {
return new Result(true, StatusCode.OK,"",commentService.findById(commentId)); return Result.success(commentService.findById(commentId));
} }
@RequestMapping(method = RequestMethod.POST) @PostMapping
public Result save(@RequestBody Comment comment){ public Result<Void> save(@RequestBody Comment comment){
commentService.save(comment); commentService.save(comment);
return new Result(true,StatusCode.OK,"保存成功"); return Result.success();
} }
@RequestMapping(value = "/{commentId}",method = RequestMethod.PUT) @PutMapping("/{commentId}")
public Result update(@PathVariable String commentId,@RequestBody Comment comment){ public Result<Void> update(@PathVariable String commentId,@RequestBody Comment comment){
comment.set_id(commentId); comment.set_id(commentId);
commentService.update(comment); commentService.update(comment);
return new Result(true,StatusCode.OK,"修改成功"); return Result.success();
} }
@RequestMapping(value = "/{commentId}",method = RequestMethod.DELETE) @DeleteMapping("/{commentId}")
public Result delete(@PathVariable String commentId){ public Result<Void> delete(@PathVariable String commentId){
commentService.deleteById(commentId); commentService.deleteById(commentId);
return new Result(true,StatusCode.OK,"删除成功"); return Result.success();
} }
@RequestMapping(value = "/{parentid}/{page}/{size}",method = RequestMethod.GET) @GetMapping("/{parentid}/{page}/{size}")
public Result findByParentid(@PathVariable String parentid,@PathVariable int page,@PathVariable int size){ public Result<PageResult<Comment>> findByParentid(@PathVariable String parentid,@PathVariable int page,@PathVariable int size){
Page<Comment> pageData = commentService.findByParentId(parentid,page,size); Page<Comment> pageData = commentService.findByParentId(parentid,page,size);
return Result.success(new PageResult<Comment>(pageData.getTotalElements(),pageData.getContent()));
return new Result(true,StatusCode.OK,"查询成功",new PageResult<Comment>(pageData.getTotalElements(),pageData.getContent()));
} }
@RequestMapping(value = "/thumbup/{commentId}",method = RequestMethod.PUT) @PutMapping("/thumbup/{commentId}")
public Result thumbup(@PathVariable String commentId){ public Result thumbup(@PathVariable String commentId){
String userid="111"; String userid="111";
if (redisTemplate.opsForValue().get("thumbup_"+userid)!=null){ if (redisTemplate.opsForValue().get("thumbup_"+userid)!=null){
return new Result(true,StatusCode.REPERROR,"重复提交"); return Result.failed(StatusCode.COMMENT_REPEAT_SUBMIT);
} }
commentService.thumbup(commentId); commentService.thumbup(commentId);
redisTemplate.opsForValue().set("thumbup_"+userid,1); redisTemplate.opsForValue().set("thumbup_"+userid,1);
return new Result(true,StatusCode.OK,"点赞成功"); return Result.success();
} }
} }

@ -17,6 +17,7 @@ public enum StatusCode implements IErrorCode {
MUSIC_COMMON_FAILED(20000, "歌曲模块失败"), MUSIC_COMMON_FAILED(20000, "歌曲模块失败"),
// comment 模块错误码以30XXX不足5位补0; // comment 模块错误码以30XXX不足5位补0;
COMMENT_COMMON_FAILED(30000, "评论模块失败"), COMMENT_COMMON_FAILED(30000, "评论模块失败"),
COMMENT_REPEAT_SUBMIT(30001, "评论重复提交"),
// friend 模块错误码以40XXX不足5位补0; // friend 模块错误码以40XXX不足5位补0;
FRIEND_COMMON_FAILED(40000, "交友模块失败"), FRIEND_COMMON_FAILED(40000, "交友模块失败"),
// tag 模块错误码以50XXX不足5位补0; // tag 模块错误码以50XXX不足5位补0;

Loading…
Cancel
Save