调整了message相关架构

This commit is contained in:
chuyan 2020-09-12 21:09:28 +08:00
parent 882e5afa5d
commit 61f71403a9
13 changed files with 161 additions and 68 deletions

View File

@ -1,11 +1,9 @@
package com.codesdream.ase.controller.student;
package com.codesdream.ase.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("student")
public class StudentController {
//public
@RequestMapping("file")
public class FileController {
}

View File

@ -0,0 +1,28 @@
package com.codesdream.ase.controller;
import com.codesdream.ase.model.message.Notification;
import com.codesdream.ase.model.permission.User;
import com.codesdream.ase.service.MessageService;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("message")
public class MessageController {
@Resource
MessageService messageService;
@PostMapping("notification/create")
@ResponseStatus(HttpStatus.CREATED)
public Notification createNotification (@RequestBody String title, @RequestBody String context,
@RequestBody List<Integer> files, Authentication authentication){
User user = (User)authentication.getPrincipal();
return null;//messageService.createNotification(title,context,);
}
}

View File

@ -0,0 +1,15 @@
package com.codesdream.ase.controller;
import com.codesdream.ase.model.message.Notification;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("student")
public class StudentController {
}

View File

@ -1,4 +1,4 @@
package com.codesdream.ase.controller.permission;
package com.codesdream.ase.controller;
import com.codesdream.ase.component.datamanager.JSONParameter;
import com.codesdream.ase.component.json.model.JsonableUser;

View File

@ -1,13 +1,11 @@
package com.codesdream.ase.model.message;
import com.codesdream.ase.model.permission.User;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.List;
import java.util.Date;
@Entity
@Table
@ -20,12 +18,9 @@ public class Message {
String text;
boolean isRead = false;
Date creationDate = new Date();;
// 重要性 值为0-1
int type;
@OneToMany
List<User> Receiver;
}

View File

@ -0,0 +1,23 @@
package com.codesdream.ase.model.message;
import lombok.Data;
import javax.persistence.*;
@Entity
@Table
@Data
public class MessageDelivery {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
boolean isRead = false;
int messageId;
int senderId;
int receiverId;
}

View File

@ -2,26 +2,17 @@ package com.codesdream.ase.model.message;
import com.codesdream.ase.model.file.File;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.util.Date;
import java.util.List;
@EqualsAndHashCode(callSuper = true)
@Entity
@Table
@Data
public class Notification {
@Id
int id;
String context;
String title;
Date creationDate = new Date();
public class Notification extends Message{
Date announcementDate;

View File

@ -0,0 +1,16 @@
package com.codesdream.ase.repository.message;
import com.codesdream.ase.model.message.MessageDelivery;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface MessageDeliveryRepository extends JpaRepository<MessageDelivery, Integer> {
List<MessageDelivery> findByReceiverId(int id);
List<MessageDelivery> findBySenderId(int id);
List<MessageDelivery> findBySenderIdAndIsReadTrue(int id);
}

View File

@ -1,4 +1,4 @@
package com.codesdream.ase.repository.student;
package com.codesdream.ase.repository.message;
import com.codesdream.ase.model.message.Notification;
import org.springframework.data.jpa.repository.JpaRepository;

View File

@ -119,7 +119,7 @@ public class ActivityService {
Message message = messageService.createMessage(
String.format("活动%s即将开始", activity.getTitle()),
formatter.format(activity.getRealBeginDate())
formatter.format(activity.getRealBeginDate()), -1
);

View File

@ -1,9 +1,12 @@
package com.codesdream.ase.service;
import com.codesdream.ase.exception.notfound.NotFoundException;
import com.codesdream.ase.model.file.File;
import com.codesdream.ase.model.message.Message;
import com.codesdream.ase.model.message.Notification;
import com.codesdream.ase.model.permission.User;
import com.codesdream.ase.repository.message.MessageRepository;
import com.codesdream.ase.repository.message.NotificationRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -16,6 +19,10 @@ public class MessageService {
@Resource
MessageRepository messageRepository;
@Resource
NotificationRepository notificationRepository;
/**
* 创建新消息
* @see Message
@ -23,7 +30,7 @@ public class MessageService {
* @param text 消息内容
* @return 持久化的消息
*/
public Message createMessage(String title, String text){
public Message createMessage(String title, String text, int userId){
Message message = new Message();
message.setText(text);
@ -40,8 +47,8 @@ public class MessageService {
* @param type 消息类型紧急程度
* @return 持久化的消息
*/
public Message createMessage(String title, String text, int type){
Message message = createMessage(title, text);
public Message createMessage(String title, String text, int userId, int type){
Message message = createMessage(title, text, userId);
message.setType(type);
return messageRepository.save(message);
}
@ -78,6 +85,12 @@ public class MessageService {
return true;
}
/**
* 根据message的id寻找对应的message若不存在则抛出{@code NotFoundException}异常
* @exception NotFoundException 若id无效
* @param messageId 消息id
* @return 寻找到的message
*/
public Message searchMessageById(int messageId){
if(!checkMessageExistence(messageId)){
throw new NotFoundException("No such message.");
@ -85,10 +98,22 @@ public class MessageService {
return messageRepository.findById(messageId).get();
}
/**
* 根据消息标题寻找消息若无此标题返回空列表
* @param messageTitle 消息标题
* @return 消息列表
*/
public List<Message> searchMessageByTitle(String messageTitle){
return messageRepository.findByTitle(messageTitle);
}
/**
* 把消息发送给指定用户若消息id不存在则抛出{@code NotFoundException}异常
* @exception NotFoundException 若消息id不存在
* @param messageId 消息id
* @param users 用户列表
* @return 是否发送成功
*/
public boolean sendMessageToGroup(int messageId, List<User> users){
if(!checkMessageExistence(messageId)){
throw new NotFoundException("No such message.");
@ -97,6 +122,45 @@ public class MessageService {
return sendMessage(message, users);
}
/**
* 用于创建一个公告
* @see Notification
* @param title 公告标题
* @param description 公告内容
* @param files 公告所需附件
* @return 已经持久化的公告
*/
public Notification createNotification(String title, String description, List<File> files){
Notification notification = new Notification();
notification.setTitle(title);
notification.setText(description);
notification.setFiles(files);
return notificationRepository.save(notification);
}
/**
* 在数据库中删除指定公告
* @param notificationId 需要删除的公告id如果此id不存在则会抛出异常删除失败
* @return 是否删除成功
*/
public boolean cancelNotification(int notificationId){
Optional<Notification> notification = notificationRepository.findById(notificationId);
if(notification.isPresent()){
notificationRepository.delete(notification.get());
return true;
}
else{
throw new NotFoundException("No such notification.");
}
}
/**
* 私有方法用于判断消息id是否存在
* @param messageId 消息id
* @return id存在与否
*/
private boolean checkMessageExistence(int messageId){
Optional<Message> optionalMessage = messageRepository.findById(messageId);
if(!optionalMessage.isPresent()){

View File

@ -10,6 +10,7 @@ import com.codesdream.ase.model.student.Course;
import com.codesdream.ase.model.student.Honor;
import com.codesdream.ase.model.student.Student;
import com.codesdream.ase.model.student.StudentCourse;
import com.codesdream.ase.repository.message.NotificationRepository;
import com.codesdream.ase.repository.parent.ExerciseRepository;
import com.codesdream.ase.repository.permission.UserRepository;
import com.codesdream.ase.repository.student.*;

View File

@ -4,14 +4,11 @@ import com.codesdream.ase.exception.innerservererror.DataInvalidFormatException;
import com.codesdream.ase.exception.innerservererror.InvalidDataException;
import com.codesdream.ase.exception.notfound.NotFoundException;
import com.codesdream.ase.model.activity.Activity;
import com.codesdream.ase.model.file.File;
import com.codesdream.ase.model.file.Image;
import com.codesdream.ase.model.permission.UserDetail;
import com.codesdream.ase.model.student.Honor;
import com.codesdream.ase.model.message.Notification;
import com.codesdream.ase.model.student.Student;
import com.codesdream.ase.repository.student.HonorRepository;
import com.codesdream.ase.repository.student.NotificationRepository;
import com.codesdream.ase.repository.student.StudentRepository;
import com.codesdream.ase.validator.GeneralValidator;
import org.springframework.stereotype.Service;
@ -28,48 +25,13 @@ public class StudentService {
@Resource
StudentRepository studentRepository;
@Resource
NotificationRepository notificationRepository;
@Resource
ActivityService activityService;
@Resource
HonorRepository honorRepository;
/**
* 用于创建一个公告
* @see Notification
* @param title 公告标题
* @param description 公告内容
* @param files 公告所需附件
* @return 已经持久化的公告
*/
public Notification createNotification(String title, String description, List<File> files){
Notification notification = new Notification();
notification.setTitle(title);
notification.setContext(description);
notification.setFiles(files);
return notificationRepository.save(notification);
}
/**
* 在数据库中删除指定公告
* @param notificationId 需要删除的公告id如果此id不存在则会抛出异常删除失败
* @return 是否删除成功
*/
public boolean cancelNotification(int notificationId){
Optional<Notification> notification = notificationRepository.findById(notificationId);
if(notification.isPresent()){
notificationRepository.delete(notification.get());
return true;
}
else{
throw new NotFoundException("No such notification.");
}
}
/**
* 指定学生加入指定活动