Commit 611f56f1 by 段启岩

新增删除话题关注的接口

parent e9b05067
......@@ -142,4 +142,18 @@ public class TopicApi {
return Response.success(topicPageVO);
}
@ApiOperation("取消关注话题")
@DeleteMapping("/topic/{topicId}/follower")
public Response delTopicFollower(@PathVariable("topicId") String topicId, @CurrentSubject Subject subject) {
try {
topicService.delFollowTopic((String) subject.getId(), topicId);
return Response.success();
} catch (TopicServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
}
......@@ -13,7 +13,8 @@ public enum TopicErrorCode implements IErrorCode {
*/
TOPIC_EXISTS(3001, "该话题已存在"),
TOPIC_NOT_EXISTS(3002, "该话题不存在"),
AlREADY_FOLLOWED(3003, "已关注过该话题");
AlREADY_FOLLOWED(3003, "已关注过该话题"),
NOT_FOLLOWED(3004, "未关注过该话题");
TopicErrorCode(long code, String msg) {
this.code = code;
......
package cn.meteor.beyondclouds.modules.topic.service;
import cn.meteor.beyondclouds.modules.tag.exception.TagServiceException;
import cn.meteor.beyondclouds.modules.topic.entity.Topic;
import cn.meteor.beyondclouds.modules.topic.exception.TopicServiceException;
import cn.meteor.beyondclouds.modules.user.entity.User;
......@@ -87,4 +88,12 @@ public interface ITopicService extends IService<Topic> {
* @param count
*/
void increaseReferenceCount(String topicId, int count);
/**
* 取消关注话题
* @param userId
* @param topicId
* @throws TopicServiceException
*/
void delFollowTopic(String userId, String topicId) throws TopicServiceException;
}
package cn.meteor.beyondclouds.modules.topic.service.impl;
import cn.meteor.beyondclouds.modules.tag.exception.TagServiceException;
import cn.meteor.beyondclouds.modules.topic.entity.Topic;
import cn.meteor.beyondclouds.modules.topic.entity.TopicFollow;
import cn.meteor.beyondclouds.modules.topic.enums.TopicErrorCode;
......@@ -15,7 +16,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.stream.Collectors;
......@@ -109,7 +109,7 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
@Override
public IPage<Topic> getTopicsMyFollowed(Integer pageNumber, Integer pageSize, String userId) {
// 1. 通过用户id获取该话题所有关注着
// 1. 通过用户id获取关注的所有话题id
// 设置分页信息
IPage<TopicFollow> page = new Page<>(pageNumber, pageSize);
......@@ -123,13 +123,8 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
.map(TopicFollow::getTopicId)
.collect(Collectors.toList());
// 3. 通过用户id批量查询用户
List<Topic> topics;
if (CollectionUtils.isEmpty(topicIds)) {
topics = List.of();
} else {
topics = listByIds(topicIds);
}
// 3. 通过话题id批量查询用户
List<Topic> topics = listByIds(topicIds);
// 5.构造分页结果
IPage<Topic> userPage = new Page<>();
......@@ -160,12 +155,7 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
.collect(Collectors.toList());
// 3. 通过用户id批量查询用户
List<User> followers;
if (CollectionUtils.isEmpty(followerIds)) {
followers = List.of();
} else {
followers = userService.listByIds(followerIds);
}
List<User> followers = userService.listByIds(followerIds);
// 4.去除敏感信息
followers.stream()
......@@ -212,4 +202,29 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
}
}
@Override
public void delFollowTopic(String userId, String topicId) throws TopicServiceException {
//1. 获取话题
Topic topic = getById(topicId);
//2. 找不到该话题,抛出业务异常
if (topic == null) {
throw new TopicServiceException(TopicErrorCode.TOPIC_NOT_EXISTS);
}
//3. 判断用户是否关注该话题
QueryWrapper<TopicFollow> delTopicFollowQueryWrapper = new QueryWrapper<>();
delTopicFollowQueryWrapper.eq("user_id", userId);
delTopicFollowQueryWrapper.eq("topic_id", topicId);
if (null == topicFollowService.getOne(delTopicFollowQueryWrapper)) {
throw new TopicServiceException(TopicErrorCode.NOT_FOLLOWED);
}
//4. 取消关注话题
topicFollowService.remove(delTopicFollowQueryWrapper);
}
}
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