From 61f71403a912bf4188c94cf3c881acb124c07715 Mon Sep 17 00:00:00 2001 From: chuyan <1047381936@qq.com> Date: Sat, 12 Sep 2020 21:09:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BA=86message=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=9E=B6=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...entController.java => FileController.java} | 8 +-- .../ase/controller/MessageController.java | 28 ++++++++ .../ase/controller/StudentController.java | 15 ++++ .../{permission => }/UserController.java | 2 +- .../codesdream/ase/model/message/Message.java | 9 +-- .../ase/model/message/MessageDelivery.java | 23 ++++++ .../ase/model/message/Notification.java | 15 +--- .../message/MessageDeliveryRepository.java | 16 +++++ .../NotificationRepository.java | 2 +- .../ase/service/ActivityService.java | 2 +- .../ase/service/MessageService.java | 70 ++++++++++++++++++- .../ase/service/StaticsService.java | 1 + .../ase/service/StudentService.java | 38 ---------- 13 files changed, 161 insertions(+), 68 deletions(-) rename src/main/java/com/codesdream/ase/controller/{student/StudentController.java => FileController.java} (54%) create mode 100644 src/main/java/com/codesdream/ase/controller/MessageController.java create mode 100644 src/main/java/com/codesdream/ase/controller/StudentController.java rename src/main/java/com/codesdream/ase/controller/{permission => }/UserController.java (97%) create mode 100644 src/main/java/com/codesdream/ase/model/message/MessageDelivery.java create mode 100644 src/main/java/com/codesdream/ase/repository/message/MessageDeliveryRepository.java rename src/main/java/com/codesdream/ase/repository/{student => message}/NotificationRepository.java (90%) diff --git a/src/main/java/com/codesdream/ase/controller/student/StudentController.java b/src/main/java/com/codesdream/ase/controller/FileController.java similarity index 54% rename from src/main/java/com/codesdream/ase/controller/student/StudentController.java rename to src/main/java/com/codesdream/ase/controller/FileController.java index 38f0205..d6d7045 100644 --- a/src/main/java/com/codesdream/ase/controller/student/StudentController.java +++ b/src/main/java/com/codesdream/ase/controller/FileController.java @@ -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 { } diff --git a/src/main/java/com/codesdream/ase/controller/MessageController.java b/src/main/java/com/codesdream/ase/controller/MessageController.java new file mode 100644 index 0000000..7e15fc7 --- /dev/null +++ b/src/main/java/com/codesdream/ase/controller/MessageController.java @@ -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 files, Authentication authentication){ + User user = (User)authentication.getPrincipal(); + return null;//messageService.createNotification(title,context,); + } + +} diff --git a/src/main/java/com/codesdream/ase/controller/StudentController.java b/src/main/java/com/codesdream/ase/controller/StudentController.java new file mode 100644 index 0000000..d0a37df --- /dev/null +++ b/src/main/java/com/codesdream/ase/controller/StudentController.java @@ -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 { + + + + +} diff --git a/src/main/java/com/codesdream/ase/controller/permission/UserController.java b/src/main/java/com/codesdream/ase/controller/UserController.java similarity index 97% rename from src/main/java/com/codesdream/ase/controller/permission/UserController.java rename to src/main/java/com/codesdream/ase/controller/UserController.java index 857d609..46c800b 100644 --- a/src/main/java/com/codesdream/ase/controller/permission/UserController.java +++ b/src/main/java/com/codesdream/ase/controller/UserController.java @@ -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; diff --git a/src/main/java/com/codesdream/ase/model/message/Message.java b/src/main/java/com/codesdream/ase/model/message/Message.java index 5daa945..3bb6589 100644 --- a/src/main/java/com/codesdream/ase/model/message/Message.java +++ b/src/main/java/com/codesdream/ase/model/message/Message.java @@ -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 Receiver; - } \ No newline at end of file diff --git a/src/main/java/com/codesdream/ase/model/message/MessageDelivery.java b/src/main/java/com/codesdream/ase/model/message/MessageDelivery.java new file mode 100644 index 0000000..7da98ac --- /dev/null +++ b/src/main/java/com/codesdream/ase/model/message/MessageDelivery.java @@ -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; +} diff --git a/src/main/java/com/codesdream/ase/model/message/Notification.java b/src/main/java/com/codesdream/ase/model/message/Notification.java index 81c1d15..5b0bf2c 100644 --- a/src/main/java/com/codesdream/ase/model/message/Notification.java +++ b/src/main/java/com/codesdream/ase/model/message/Notification.java @@ -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; diff --git a/src/main/java/com/codesdream/ase/repository/message/MessageDeliveryRepository.java b/src/main/java/com/codesdream/ase/repository/message/MessageDeliveryRepository.java new file mode 100644 index 0000000..55d1a1b --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/message/MessageDeliveryRepository.java @@ -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 { + List findByReceiverId(int id); + + List findBySenderId(int id); + + List findBySenderIdAndIsReadTrue(int id); +} diff --git a/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java b/src/main/java/com/codesdream/ase/repository/message/NotificationRepository.java similarity index 90% rename from src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java rename to src/main/java/com/codesdream/ase/repository/message/NotificationRepository.java index 629d181..078f77c 100644 --- a/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java +++ b/src/main/java/com/codesdream/ase/repository/message/NotificationRepository.java @@ -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; diff --git a/src/main/java/com/codesdream/ase/service/ActivityService.java b/src/main/java/com/codesdream/ase/service/ActivityService.java index 73a0c00..b0fa8b9 100644 --- a/src/main/java/com/codesdream/ase/service/ActivityService.java +++ b/src/main/java/com/codesdream/ase/service/ActivityService.java @@ -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 ); diff --git a/src/main/java/com/codesdream/ase/service/MessageService.java b/src/main/java/com/codesdream/ase/service/MessageService.java index 6d606a3..2535bfa 100644 --- a/src/main/java/com/codesdream/ase/service/MessageService.java +++ b/src/main/java/com/codesdream/ase/service/MessageService.java @@ -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 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 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 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 = 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 optionalMessage = messageRepository.findById(messageId); if(!optionalMessage.isPresent()){ diff --git a/src/main/java/com/codesdream/ase/service/StaticsService.java b/src/main/java/com/codesdream/ase/service/StaticsService.java index d36d56c..1b5483b 100644 --- a/src/main/java/com/codesdream/ase/service/StaticsService.java +++ b/src/main/java/com/codesdream/ase/service/StaticsService.java @@ -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.*; diff --git a/src/main/java/com/codesdream/ase/service/StudentService.java b/src/main/java/com/codesdream/ase/service/StudentService.java index bf380bb..79cf903 100644 --- a/src/main/java/com/codesdream/ase/service/StudentService.java +++ b/src/main/java/com/codesdream/ase/service/StudentService.java @@ -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 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 = notificationRepository.findById(notificationId); - if(notification.isPresent()){ - notificationRepository.delete(notification.get()); - return true; - } - else{ - throw new NotFoundException("No such notification."); - } - } /** * 指定学生加入指定活动 From 95fdf7d063b683273db4da704869fd5ae1b3cd44 Mon Sep 17 00:00:00 2001 From: chuyan <1047381936@qq.com> Date: Sat, 12 Sep 2020 21:14:09 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AF=B9=E8=BF=9C=E7=AB=AF=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=BF=9B=E8=A1=8Cbug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/codesdream/ase/model/parent/Parent.java | 7 ++----- .../java/com/codesdream/ase/model/student/Student.java | 1 + .../ase/repository/parent/ParentRepository.java | 6 ++---- .../ase/repository/student/StudentRepository.java | 8 ++------ 4 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/codesdream/ase/model/parent/Parent.java b/src/main/java/com/codesdream/ase/model/parent/Parent.java index 7fb7617..002a922 100644 --- a/src/main/java/com/codesdream/ase/model/parent/Parent.java +++ b/src/main/java/com/codesdream/ase/model/parent/Parent.java @@ -1,18 +1,15 @@ package com.codesdream.ase.model.parent; -import com.codesdream.ase.model.mark.Tag; import com.codesdream.ase.model.permission.User; import lombok.Data; import lombok.EqualsAndHashCode; -import javax.annotation.Generated; -import javax.persistence.*; -import java.util.List; +import javax.persistence.Entity; @Entity -@Table @Data @EqualsAndHashCode(callSuper = false) public class Parent extends User { + int studentId; } diff --git a/src/main/java/com/codesdream/ase/model/student/Student.java b/src/main/java/com/codesdream/ase/model/student/Student.java index 82329db..979e60c 100644 --- a/src/main/java/com/codesdream/ase/model/student/Student.java +++ b/src/main/java/com/codesdream/ase/model/student/Student.java @@ -13,6 +13,7 @@ import java.util.Map; @Data @EqualsAndHashCode(callSuper = false) public class Student extends User { + int parentId; String profilePicture; diff --git a/src/main/java/com/codesdream/ase/repository/parent/ParentRepository.java b/src/main/java/com/codesdream/ase/repository/parent/ParentRepository.java index 4560a04..83f2e1f 100644 --- a/src/main/java/com/codesdream/ase/repository/parent/ParentRepository.java +++ b/src/main/java/com/codesdream/ase/repository/parent/ParentRepository.java @@ -1,17 +1,15 @@ package com.codesdream.ase.repository.parent; import com.codesdream.ase.model.parent.Parent; -import com.codesdream.ase.model.student.Student; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; -import java.util.Optional; @Repository public interface ParentRepository extends JpaRepository { - List findByStudentId(String studentId); - List findByStudentId(String studentId, Sort sort); + List findByStudentId(int studentId); + List findByStudentId(int studentId, Sort sort); } diff --git a/src/main/java/com/codesdream/ase/repository/student/StudentRepository.java b/src/main/java/com/codesdream/ase/repository/student/StudentRepository.java index bcc9863..ca99a4a 100644 --- a/src/main/java/com/codesdream/ase/repository/student/StudentRepository.java +++ b/src/main/java/com/codesdream/ase/repository/student/StudentRepository.java @@ -1,18 +1,14 @@ package com.codesdream.ase.repository.student; import com.codesdream.ase.model.student.Student; -import com.codesdream.ase.model.student.StudentCourse; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.List; -import java.util.Optional; - @Repository public interface StudentRepository extends JpaRepository { - Student findByParentId(String parentId); - Student findByParentId(String parentId, Sort sort); + Student findByParentId(int parentId); + Student findByParentId(int parentId, Sort sort); }