From a29d8b9bc1a1fe589078463e61048f014ed84047 Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 12 Jan 2024 16:45:28 +0800 Subject: [PATCH] 1.uniform response status code for friend component --- luoo_common/src/main/java/api/StatusCode.java | 2 ++ .../com/luoo/friend/client/UserClient.java | 5 ++- .../friend/controller/FriendController.java | 35 +++++++++---------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/luoo_common/src/main/java/api/StatusCode.java b/luoo_common/src/main/java/api/StatusCode.java index 04e42b8..ce17ac1 100644 --- a/luoo_common/src/main/java/api/StatusCode.java +++ b/luoo_common/src/main/java/api/StatusCode.java @@ -20,6 +20,8 @@ public enum StatusCode implements IErrorCode { COMMENT_REPEAT_SUBMIT(30001, "评论重复提交"), // friend 模块错误码以40XXX,不足5位补0; FRIEND_COMMON_FAILED(40000, "交友模块失败"), + FRIEND_CAN_NOT_REPEAT_ADD_FRIEND(40001, "不能重复添加好友"), + FRIEND_CAN_NOT_REPEAT_ADD_NON_FRIEND(40002, "不能重复添加非好友"), // tag 模块错误码以50XXX,不足5位补0; TAG_COMMON_FAILED(50000, "标签模块失败"); private int code; diff --git a/luoo_friend/src/main/java/com/luoo/friend/client/UserClient.java b/luoo_friend/src/main/java/com/luoo/friend/client/UserClient.java index bdf3712..b422e74 100644 --- a/luoo_friend/src/main/java/com/luoo/friend/client/UserClient.java +++ b/luoo_friend/src/main/java/com/luoo/friend/client/UserClient.java @@ -3,12 +3,11 @@ package com.luoo.friend.client; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.PutMapping; @FeignClient("luoo-user") public interface UserClient { - @RequestMapping(value = "/user/{userid}/{friendid}/{x}",method = RequestMethod.PUT) + @PutMapping("/user/{userid}/{friendid}/{x}") public void updatefanscountandfollowcount(@PathVariable String userid, @PathVariable String friendid, @PathVariable int x); } diff --git a/luoo_friend/src/main/java/com/luoo/friend/controller/FriendController.java b/luoo_friend/src/main/java/com/luoo/friend/controller/FriendController.java index 1184416..2f51878 100644 --- a/luoo_friend/src/main/java/com/luoo/friend/controller/FriendController.java +++ b/luoo_friend/src/main/java/com/luoo/friend/controller/FriendController.java @@ -3,14 +3,11 @@ package com.luoo.friend.controller; import com.luoo.friend.client.UserClient; import com.luoo.friend.service.FriendService; -import entity.Result; -import entity.StatusCode; +import api.Result; +import api.StatusCode; import io.jsonwebtoken.Claims; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; @@ -32,13 +29,13 @@ public class FriendController { * * @return */ - @RequestMapping(value = "/like/{friendid}/{type}", method = RequestMethod.PUT) - public Result addFriend(@PathVariable String friendid, @PathVariable String type) { + @PutMapping("/like/{friendid}/{type}") + public Result addFriend(@PathVariable String friendid, @PathVariable String type) { //验证是否登录,并且拿到ID Claims claims = (Claims) request.getAttribute("claims_user"); if (claims == null) { - return new Result(false, StatusCode.LOGINERROR, "权限不足"); + return Result.unauthorized(null); } String userid = claims.getId(); //判断是添加好友还是非好友 @@ -47,41 +44,41 @@ public class FriendController { //添加好友 int flag = friendService.addFriend(userid,friendid); if (flag == 0) { - return new Result(false, StatusCode.ERROR, "不能重复添加好友"); + return Result.failed(StatusCode.FRIEND_CAN_NOT_REPEAT_ADD_FRIEND); } if (flag == 1) { userClient.updatefanscountandfollowcount(userid,friendid,1); - return new Result(true, StatusCode.OK, "添加成功"); + return Result.success(); } } else if (type.equals("2")) { //添加非好友 int flag = friendService.addNoFriend(userid,friendid); if (flag == 0) { - return new Result(false, StatusCode.ERROR, "不能重复添加非好友"); + return Result.failed(StatusCode.FRIEND_CAN_NOT_REPEAT_ADD_NON_FRIEND); } if (flag == 1) { - return new Result(true, StatusCode.OK, "添加成功"); + return Result.success(); } } - return new Result(false, StatusCode.ERROR, "参数异常"); + return Result.failed(StatusCode.VALIDATE_FAILED); } else { - return new Result(false, StatusCode.ERROR, "参数异常"); + return Result.failed(StatusCode.VALIDATE_FAILED); } } - @RequestMapping(value = "/{friendid}", method = RequestMethod.DELETE) - public Result deleteFriend(@PathVariable String friendid){ + @DeleteMapping("/{friendid}") + public Result deleteFriend(@PathVariable String friendid){ //验证是否登录,并且拿到ID Claims claims = (Claims) request.getAttribute("claims_user"); if (claims == null) { - return new Result(false, StatusCode.LOGINERROR, "权限不足"); + return Result.unauthorized(null); } String userid = claims.getId(); friendService.deleteFriend(userid,friendid); userClient.updatefanscountandfollowcount(userid,friendid,-1); - return new Result(true,StatusCode.OK,"删除成功"); + return Result.success(); } }