Write some part code of Message Module.

hikari
PeterAlbus 1 year ago
parent 1c051a313a
commit 1b7be7e20a

@ -0,0 +1,37 @@
package com.peteralbus.controller;
import cn.dev33.satoken.stp.StpUtil;
import com.peteralbus.domain.Message;
import com.peteralbus.domain.Result;
import com.peteralbus.service.MessageService;
import com.peteralbus.util.ResultUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin
@RequestMapping("/message")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MessageController {
private final MessageService messageService;
@RequestMapping("/getUnreadMessageCount")
public Result<?> getUnreadMessageCount() {
if (!StpUtil.isLogin()) {
return ResultUtil.error(400,"未登录");
}
Long userId = StpUtil.getLoginIdAsLong();
return ResultUtil.success(messageService.getUnreadMessageCount(userId));
}
@RequestMapping("/readMessage")
public Result<?> readMessage(Long messageId) {
if (messageService.readMessage(messageId) > 0) {
return ResultUtil.success(null);
}
return ResultUtil.error(500,"设为已读失败");
}
}

@ -0,0 +1,36 @@
package com.peteralbus.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
/**
* The type Message.
*
* @author PeterAlbus Created on 2023/9/18.
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Message {
@TableId(type= IdType.ASSIGN_ID)
@JsonFormat(shape = JsonFormat.Shape.STRING)
Long messageId;
Long senderId;
Long targetId;
String messageContent;
String messageTitle;
String senderTime;
Boolean isRead;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
LocalDateTime gmtCreate;
}

@ -0,0 +1,9 @@
package com.peteralbus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.peteralbus.domain.Message;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface MessageMapper extends BaseMapper<Message> {
}

@ -0,0 +1,15 @@
package com.peteralbus.service;
import com.peteralbus.domain.Message;
import java.util.List;
public interface MessageService {
List<Message> queryMessageByTargetId(Long userId);
List<Message> queryMessageBySenderId(Long userId);
Message queryMessageById(Long messageId);
int addMessage(Message message);
int deleteMessage(Long messageId);
int readMessage(Long messageId);
Long getUnreadMessageCount(Long userId);
}

@ -0,0 +1,66 @@
package com.peteralbus.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peteralbus.domain.Message;
import com.peteralbus.mapper.MessageMapper;
import com.peteralbus.service.MessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MessageServiceImpl implements MessageService {
private final MessageMapper messageMapper;
@Override
public List<Message> queryMessageByTargetId(Long userId) {
QueryWrapper<Message> messageQueryWrapper=new QueryWrapper<>();
messageQueryWrapper.eq("target_id",userId);
return messageMapper.selectList(messageQueryWrapper);
}
@Override
public List<Message> queryMessageBySenderId(Long userId) {
QueryWrapper<Message> messageQueryWrapper=new QueryWrapper<>();
messageQueryWrapper.eq("sender_id",userId);
return messageMapper.selectList(messageQueryWrapper);
}
@Override
public Message queryMessageById(Long messageId) {
return messageMapper.selectById(messageId);
}
@Override
public int addMessage(Message message) {
return messageMapper.insert(message);
}
@Override
public int deleteMessage(Long messageId) {
return messageMapper.deleteById(messageId);
}
@Override
public int readMessage(Long messageId) {
Message message = messageMapper.selectById(messageId);
Long userId = StpUtil.getLoginIdAsLong();
if (!message.getTargetId().equals(userId)) {
return 0;
}
message.setIsRead(true);
return messageMapper.updateById(message);
}
@Override
public Long getUnreadMessageCount(Long userId) {
QueryWrapper<Message> messageQueryWrapper=new QueryWrapper<>();
messageQueryWrapper.eq("target_id",userId).eq("is_read",false);
return messageMapper.selectCount(messageQueryWrapper);
}
}

@ -0,0 +1,12 @@
package com.peteralbus.util;
import com.peteralbus.service.MessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MessageUtil {
private final MessageService messageService;
}
Loading…
Cancel
Save