1.uniform response status code for friend component

main
Gary 1 year ago
parent 67b26776b5
commit a29d8b9bc1

@ -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;

@ -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);
}

@ -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<Void> 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<Void> 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();
}
}

Loading…
Cancel
Save