Commit 5dd42263 by 段启岩

修复重复关注的问题

parent db2d6b8e
...@@ -31,8 +31,8 @@ ...@@ -31,8 +31,8 @@
on q.question_id = qt.question_id on q.question_id = qt.question_id
left join tag t left join tag t
on t.tag_id = qt.tag_id on t.tag_id = qt.tag_id
<if test="ew != null and ew.sqlSegment != null"> <if test="ew != null and ew.customSqlSegment != null">
where ${ew.sqlSegment} ${ew.customSqlSegment}
</if> </if>
</select> </select>
......
...@@ -161,9 +161,13 @@ public class UserApi { ...@@ -161,9 +161,13 @@ public class UserApi {
@DeleteMapping("/user/{userId}/follower") @DeleteMapping("/user/{userId}/follower")
public Response delFollower(@PathVariable(name = "userId") String userId, @CurrentSubject Subject subject){ public Response delFollower(@PathVariable(name = "userId") String userId, @CurrentSubject Subject subject){
userFollowService.delFollow(userId, String.valueOf(subject.getId())); try {
return Response.success(); userFollowService.delFollow(userId, String.valueOf(subject.getId()));
return Response.success();
} catch (UserServiceException e) {
e.printStackTrace();
return Response.error(e);
}
} }
@ApiOperation(("获取我的粉丝")) @ApiOperation(("获取我的粉丝"))
......
...@@ -16,7 +16,8 @@ public enum UserErrorCode implements IErrorCode { ...@@ -16,7 +16,8 @@ public enum UserErrorCode implements IErrorCode {
CAN_NOT_FOLLOW_SELF(1003, "不能关注自己"), CAN_NOT_FOLLOW_SELF(1003, "不能关注自己"),
FOLLOWED_USER_NOT_EXISTS(1004, "被关注用户不存在"), FOLLOWED_USER_NOT_EXISTS(1004, "被关注用户不存在"),
FOLLOWER_USER_NOT_EXISTS(1005, "关注者不存在"), FOLLOWER_USER_NOT_EXISTS(1005, "关注者不存在"),
ALREADY_FOLLOWED(1006, "已关注过该用户"); ALREADY_FOLLOWED(1006, "已关注过该用户"),
NON_FOLLOWED(1007, "没有关注过该用户");
UserErrorCode(long code, String msg) { UserErrorCode(long code, String msg) {
this.code = code; this.code = code;
......
...@@ -26,7 +26,7 @@ public interface IUserFollowService extends IService<UserFollow> { ...@@ -26,7 +26,7 @@ public interface IUserFollowService extends IService<UserFollow> {
* @param followedId * @param followedId
* @param followerId * @param followerId
*/ */
void delFollow(String followedId, String followerId); void delFollow(String followedId, String followerId) throws UserServiceException;
......
...@@ -8,7 +8,6 @@ import cn.meteor.beyondclouds.modules.user.mapper.IUserFollowMapper; ...@@ -8,7 +8,6 @@ import cn.meteor.beyondclouds.modules.user.mapper.IUserFollowMapper;
import cn.meteor.beyondclouds.modules.user.service.IUserFollowService; import cn.meteor.beyondclouds.modules.user.service.IUserFollowService;
import cn.meteor.beyondclouds.modules.user.service.IUserService; import cn.meteor.beyondclouds.modules.user.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
...@@ -43,16 +42,25 @@ public class UserFollowServiceImpl extends ServiceImpl<IUserFollowMapper, UserFo ...@@ -43,16 +42,25 @@ public class UserFollowServiceImpl extends ServiceImpl<IUserFollowMapper, UserFo
throw new UserServiceException(UserErrorCode.CAN_NOT_FOLLOW_SELF); throw new UserServiceException(UserErrorCode.CAN_NOT_FOLLOW_SELF);
} }
UserFollow userFollow = new UserFollow();
userFollow.setFollowedId(followedId);
userFollow.setFollowerId(followerId);
QueryWrapper<UserFollow> userFollowQueryWrapper = new QueryWrapper<>(userFollow); QueryWrapper<UserFollow> userFollowQueryWrapper = new QueryWrapper<>();
if (null != getOne(userFollowQueryWrapper)) { userFollowQueryWrapper.eq("followed_id", followedId);
userFollowQueryWrapper.eq("follower_id", followerId);
UserFollow userFollow = getOne(userFollowQueryWrapper);
if (null != userFollow && userFollow.getFollowStatus() == 0) {
throw new UserServiceException(UserErrorCode.ALREADY_FOLLOWED); throw new UserServiceException(UserErrorCode.ALREADY_FOLLOWED);
} }
boolean update = true;
if (null == userFollow) {
update = false;
userFollow = new UserFollow();
userFollow.setFollowerId(followerId);
userFollow.setFollowedId(followedId);
}
userFollow.setFollowStatus(0);
//1.查询被关注用户的基本信息 //1.查询被关注用户的基本信息
User followedUser = userService.getById(followedId); User followedUser = userService.getById(followedId);
if (null == followedUser) { if (null == followedUser) {
...@@ -69,8 +77,11 @@ public class UserFollowServiceImpl extends ServiceImpl<IUserFollowMapper, UserFo ...@@ -69,8 +77,11 @@ public class UserFollowServiceImpl extends ServiceImpl<IUserFollowMapper, UserFo
userFollow.setFollowerNick(followerUser.getNickName()); userFollow.setFollowerNick(followerUser.getNickName());
userFollow.setFollowerAvatar(followerUser.getUserAvatar()); userFollow.setFollowerAvatar(followerUser.getUserAvatar());
userFollow.setFollowStatus(0); if (update) {
save(userFollow); update(userFollow, userFollowQueryWrapper);
} else {
save(userFollow);
}
} }
...@@ -80,16 +91,20 @@ public class UserFollowServiceImpl extends ServiceImpl<IUserFollowMapper, UserFo ...@@ -80,16 +91,20 @@ public class UserFollowServiceImpl extends ServiceImpl<IUserFollowMapper, UserFo
* @param followerId 关注者户的userId * @param followerId 关注者户的userId
*/ */
@Override @Override
public void delFollow(String followedId, String followerId) { public void delFollow(String followedId, String followerId) throws UserServiceException {
QueryWrapper<UserFollow> userFollowQueryWrapper = new QueryWrapper<>();
userFollowQueryWrapper.eq("followed_id", followedId);
userFollowQueryWrapper.eq("follower_id", followerId);
userFollowQueryWrapper.eq("follow_status", 0);
UserFollow userFollow = getOne(userFollowQueryWrapper);
if (null == userFollow) {
throw new UserServiceException(UserErrorCode.NON_FOLLOWED);
}
userFollow.setFollowStatus(-1);
//1.查找符合条件的关注信息 //1.查找符合条件的关注信息
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.eq("followed_id", followedId);
updateWrapper.eq("follower_id", followerId);
//2.将关注状态设置为-1并保存 //2.将关注状态设置为-1并保存
updateWrapper.set("follow_status", -1); update(userFollow, userFollowQueryWrapper);
update(updateWrapper);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment