Commit fc1465a5 by 胡明森

发布话题不可重复

parent d76b5876
......@@ -42,8 +42,13 @@ public class TopicApi {
@PostMapping("/topic")
public Response createTopic(@RequestBody @Valid CreateTopicForm createTopicForm, @CurrentSubject Subject subject) {
TopicDetail topic = topicService.createTopic(String.valueOf(subject.getId()), createTopicForm.getTopicName());
return Response.success(topic);
try {
TopicDetail topic = topicService.createTopic(String.valueOf(subject.getId()), createTopicForm.getTopicName());
return Response.success(topic);
} catch (TopicServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
......
......@@ -25,7 +25,7 @@ public interface ITopicService extends IService<Topic> {
* @param topicName
* @return TopicDetail
*/
TopicDetail createTopic(String userId, String topicName);
TopicDetail createTopic(String userId, String topicName) throws TopicServiceException;
/**
* 检索话题
......
......@@ -53,15 +53,22 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
}
@Override
public TopicDetail createTopic(String userId, String topicName) {
public TopicDetail createTopic(String userId, String topicName) throws TopicServiceException{
//1. 创建话题
//1. 检测是否存在该话题
QueryWrapper<Topic> topicQueryWrapper = new QueryWrapper<>();
topicQueryWrapper.eq("topic_name",topicName);
if(getOne(topicQueryWrapper) != null) {
throw new TopicServiceException(TopicErrorCode.TOPIC_EXISTS);
}
//2. 创建话题
Topic topic = new Topic();
topic.setUserId(userId);
topic.setTopicName(topicName);
save(topic);
//2. 返回话题的id与话题名称
//3. 返回话题的id与话题名称
TopicDetail topicDetail=new TopicDetail();
BeanUtils.copyProperties(topic, topicDetail);
return topicDetail;
......
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