From 0b9227a82236d3227f4f8848fc81ee7c6107af60 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Fri, 19 Apr 2024 20:22:31 +0200 Subject: [PATCH] indev --- .../EndPoints/NotificationController.java | 14 +++++++--- .../Repositories/NotificationRepository.java | 2 +- .../herisson/Clyde/Tables/Notification.java | 6 ++++- .../java/ovh/herisson/Clyde/Tables/User.java | 9 +++++++ frontend/src/App.vue | 27 +++++++++++++++---- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/NotificationController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/NotificationController.java index 7dac27a..536f76b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/NotificationController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/NotificationController.java @@ -13,10 +13,12 @@ import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import lombok.AllArgsConstructor; +import ovh.herisson.Clyde.Repositories.NotificationRepository; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Tables.Notification; import ovh.herisson.Clyde.Tables.User; +import ovh.herisson.Clyde.Tables.Notification.Status; @RestController @AllArgsConstructor @@ -24,6 +26,7 @@ import ovh.herisson.Clyde.Tables.User; public class NotificationController { private AuthenticatorService authServ; + private NotificationRepository notifRepo; @GetMapping("/notifications") public ResponseEntity> getNotifications(@RequestHeader("Authorization") String token){ @@ -37,8 +40,13 @@ public class NotificationController { } @PostMapping("/notifications/{id}") - public ResponseStatus archiveNotification(@RequestHeader("Authorization") String token, @PathVariable long id){ - return null; + public ResponseEntity archiveNotification(@RequestHeader("Authorization") String token, @PathVariable long id){ + User u = authServ.getUserFromToken(token); + Notification n = notifRepo.findById(id).orElse(null); + if(u == null || n.getUser() != u){ + return new UnauthorizedResponse<>(null); + } + n.setStatus(Status.Archived); + return new ResponseEntity<>(HttpStatus.OK); } - } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/NotificationRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/NotificationRepository.java index 2d7ce13..a9f9546 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/NotificationRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/NotificationRepository.java @@ -4,5 +4,5 @@ import org.springframework.data.repository.CrudRepository; import ovh.herisson.Clyde.Tables.Notification; -interface NotificationRepository extends CrudRepository {} +public interface NotificationRepository extends CrudRepository {} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java index ce8f0ad..1b7ab53 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Notification.java @@ -8,6 +8,8 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.annotation.Nullable; import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import lombok.Data; @@ -18,12 +20,14 @@ import lombok.NoArgsConstructor; @Entity public class Notification { - private enum Status { + public enum Status { Unread, Read, + Archived } @Id + @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String subject; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java index 4ce2bb4..e2a0c27 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java @@ -9,6 +9,7 @@ import org.hibernate.annotations.OnDeleteAction; import org.hibernate.annotations.GenericGenerator; import ovh.herisson.Clyde.Tables.Msg.Discussion; import ovh.herisson.Clyde.Tables.Msg.Message; +import ovh.herisson.Clyde.Tables.Notification.Status; import java.util.Date; import java.util.List; @@ -77,4 +78,12 @@ public class User { this.password = password; this.role = Role.Student; } + + public List getNotifications(){ + for(Notification n: this.notifications){ + if(n.getStatus() == Status.Archived) + this.notifications.remove(n); + } + return this.notifications; + } } diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 2a15677..dfa5ca9 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -49,17 +49,17 @@ window.addEventListener('hashchange', () => {
  • -
    +
  • -
  • - +
  • +
      -
    • {{ i18n(notif.subject) }} - {{ notif.body }}
    • +
    • {{ i18n(notif.subject) }} - {{ notif.body }}
  • - +