完成消息提醒后端接口设计

This commit is contained in:
柏码の讲师 2023-10-28 03:09:26 +08:00
parent 09f889145e
commit a2ecd7b06b
8 changed files with 154 additions and 1 deletions

View File

@ -0,0 +1,37 @@
package com.example.controller;
import com.example.entity.RestBean;
import com.example.entity.vo.response.NotificationVO;
import com.example.service.NotificationService;
import com.example.utils.Const;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Min;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/notification")
public class NotificationController {
@Resource
NotificationService service;
@GetMapping("/list")
public RestBean<List<NotificationVO>> listNotification(@RequestAttribute(Const.ATTR_USER_ID) int id) {
return RestBean.success(service.findUserNotification(id));
}
@GetMapping("/delete")
public RestBean<List<NotificationVO>> deleteNotification(@RequestParam @Min(0) int id,
@RequestAttribute(Const.ATTR_USER_ID) int uid) {
service.deleteUserNotification(id, uid);
return RestBean.success();
}
@GetMapping("/delete-all")
public RestBean<List<NotificationVO>> deleteAllNotification(@RequestAttribute(Const.ATTR_USER_ID) int uid) {
service.deleteUserAllNotification(uid);
return RestBean.success();
}
}

View File

@ -0,0 +1,19 @@
package com.example.entity.dto;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.example.entity.BaseData;
import lombok.Data;
@Data
@TableName("db_notification")
public class Notification implements BaseData {
@TableId(type = IdType.AUTO)
Integer id;
Integer uid;
String title;
String content;
String type;
String url;
}

View File

@ -0,0 +1,12 @@
package com.example.entity.vo.response;
import lombok.Data;
@Data
public class NotificationVO {
Integer id;
String title;
String content;
String type;
String url;
}

View File

@ -0,0 +1,9 @@
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.dto.Notification;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface NotificationMapper extends BaseMapper<Notification> {
}

View File

@ -0,0 +1,14 @@
package com.example.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.entity.dto.Notification;
import com.example.entity.vo.response.NotificationVO;
import java.util.List;
public interface NotificationService extends IService<Notification> {
List<NotificationVO> findUserNotification(int uid);
void deleteUserNotification(int id, int uid);
void deleteUserAllNotification(int uid);
void addNotification(int uid, String title, String content, String type, String url);
}

View File

@ -0,0 +1,42 @@
package com.example.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.dto.Notification;
import com.example.entity.vo.response.NotificationVO;
import com.example.mapper.NotificationMapper;
import com.example.service.NotificationService;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements NotificationService {
@Override
public List<NotificationVO> findUserNotification(int uid) {
return this.list(Wrappers.<Notification>query().eq("uid", uid))
.stream()
.map(notification -> notification.asViewObject(NotificationVO.class))
.toList();
}
public void deleteUserNotification(int id, int uid){
this.remove(Wrappers.<Notification>query().eq("id", id).eq("uid", uid));
}
public void deleteUserAllNotification(int uid){
this.remove(Wrappers.<Notification>query().eq("uid", uid));
}
@Override
public void addNotification(int uid, String title, String content, String type, String url) {
Notification notification = new Notification();
notification.setUid(uid);
notification.setTitle(title);
notification.setContent(content);
notification.setType(type);
notification.setUrl(url);
this.save(notification);
}
}

View File

@ -14,6 +14,7 @@ import com.example.entity.vo.response.TopicDetailVO;
import com.example.entity.vo.response.TopicPreviewVO;
import com.example.entity.vo.response.TopicTopVO;
import com.example.mapper.*;
import com.example.service.NotificationService;
import com.example.service.TopicService;
import com.example.utils.CacheUtils;
import com.example.utils.Const;
@ -56,6 +57,9 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
@Resource
StringRedisTemplate template;
@Resource
NotificationService notificationService;
@Override
public List<TopicType> listType() {
return typeMapper.selectList(null);
@ -171,6 +175,22 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
BeanUtils.copyProperties(vo, comment);
comment.setTime(new Date());
commentMapper.insert(comment);
Topic topic = baseMapper.selectById(vo.getTid());
Account account = accountMapper.selectById(uid);
if(vo.getQuote() > 0) {
TopicComment com = commentMapper.selectById(vo.getQuote());
if(!Objects.equals(com.getUid(), account.getId())) {
notificationService.addNotification(com.getUid(),
"您有新的帖子评论回复",
account.getUsername()+" 回复了你发表的评论,快去看看吧!",
"success", "/index/post-detail/"+com.getTid());
}
} else if(!Objects.equals(topic.getUid(), account.getId())) {
notificationService.addNotification(topic.getUid(),
"您有新的帖子回复",
account.getUsername()+" 回复了你发表的主题: "+topic.getTitle() +",快去看看吧!",
"success", "/index/post-detail/"+topic.getId());
}
return null;
}

View File

@ -202,7 +202,7 @@ function deleteComment(id) {
:submit="updateTopic"/>
<topic-comment-editor :show="comment.show" @close="comment.show = false"
:quote="comment.quote" :tid="tid"
@comment="comment.show = false;loadComments(Math.ceil(topic.data.commentCount / 10) - 1)"/>
@comment="comment.show = false;loadComments(Math.max(0, Math.ceil(topic.data.commentCount / 10) - 1))"/>
<div class="add-comment" @click="comment.show = true">
<el-icon><Plus /></el-icon>
</div>