From 5bb760672119af444b22221c9916bf291d9e7dab Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Fri, 5 Apr 2024 12:54:51 +0200 Subject: [PATCH] signing and commenting --- .../herisson/Clyde/DTO/Msg/DiscussionDTO.java | 8 ++++++ .../herisson/Clyde/DTO/Msg/MessagesDTO.java | 8 ++++++ .../EndPoints/Msg/MessagesController.java | 12 +++++++++ .../Msg/DiscussionRepository.java | 9 +++++++ .../Repositories/Msg/MessageRepository.java | 10 +++++-- .../Clyde/Services/Msg/DiscussionService.java | 26 +++++++++++++++++++ .../herisson/Clyde/Tables/Msg/Discussion.java | 10 +++++++ .../herisson/Clyde/Tables/Msg/Message.java | 9 +++++++ 8 files changed, 90 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/DTO/Msg/DiscussionDTO.java b/backend/src/main/java/ovh/herisson/Clyde/DTO/Msg/DiscussionDTO.java index 7b4d4f8..cd7f3cf 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/DTO/Msg/DiscussionDTO.java +++ b/backend/src/main/java/ovh/herisson/Clyde/DTO/Msg/DiscussionDTO.java @@ -1,5 +1,13 @@ package ovh.herisson.Clyde.DTO.Msg; +/****************************************************** + * @file DiscussionDTO.java + * @author Anthony Debucquoy + * @scope Extension messagerie + * + * File to format a discussion using messageDTO + ******************************************************/ + import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; diff --git a/backend/src/main/java/ovh/herisson/Clyde/DTO/Msg/MessagesDTO.java b/backend/src/main/java/ovh/herisson/Clyde/DTO/Msg/MessagesDTO.java index d9dfd14..2ce0228 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/DTO/Msg/MessagesDTO.java +++ b/backend/src/main/java/ovh/herisson/Clyde/DTO/Msg/MessagesDTO.java @@ -1,5 +1,13 @@ package ovh.herisson.Clyde.DTO.Msg; +/****************************************************** + * @file MessagesDTO.java + * @author Anthony Debucquoy + * @scope Extension messagerie + * + * File to Format the response adding the sender field + ******************************************************/ + import lombok.AllArgsConstructor; import lombok.Data; import ovh.herisson.Clyde.Tables.User; diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/MessagesController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/MessagesController.java index 591e172..9f1db44 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/MessagesController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/MessagesController.java @@ -1,5 +1,13 @@ package ovh.herisson.Clyde.EndPoints.Msg; +/****************************************************** + * @file MessagesController.java + * @author Anthony Debucquoy + * @scope Extension messagerie + * + * Entry point for the messages application + ******************************************************/ + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -48,6 +56,10 @@ public class MessagesController { @GetMapping("/discussion/{id}") public ResponseEntity getDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id){ + User user = authServ.getUserFromToken(token); + if(user == null || !discServ.hasDiscussion(user, id) ){ + return new UnauthorizedResponse<>(null); + } return new ResponseEntity<>(DiscussionDTO.construct(discRepo.findById(id).orElse(null), authServ.getUserFromToken(token)), HttpStatus.OK); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/DiscussionRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/DiscussionRepository.java index 3e74cb4..9b2675b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/DiscussionRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/DiscussionRepository.java @@ -1,5 +1,14 @@ package ovh.herisson.Clyde.Repositories.Msg; +/****************************************************** + * @file DiscussionRepository.java + * @author Anthony Debucquoy + * @scope Extension messagerie + * + * Repository of Discussion allowing to fetch discussion by user + ******************************************************/ + + import java.util.List; import org.springframework.data.jpa.repository.Query; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/MessageRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/MessageRepository.java index 8c90e92..021ac79 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/MessageRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/MessageRepository.java @@ -1,8 +1,14 @@ package ovh.herisson.Clyde.Repositories.Msg; +/****************************************************** + * @file MessageRepository.java + * @author Anthony Debucquoy + * @scope Extension messagerie + ******************************************************/ + + import org.springframework.data.repository.CrudRepository; import ovh.herisson.Clyde.Tables.Msg.Message; -public interface MessageRepository extends CrudRepository { -} +public interface MessageRepository extends CrudRepository {} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/DiscussionService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/DiscussionService.java index c1aa534..9a471da 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/DiscussionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/DiscussionService.java @@ -1,5 +1,16 @@ package ovh.herisson.Clyde.Services.Msg; +import java.util.List; + +/****************************************************** + * @file DiscussionService.java + * @author Anthony Debucquoy + * @scope Extension messagerie + * + * Various function utilised by the messages application + ******************************************************/ + + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -20,12 +31,27 @@ public class DiscussionService { return discRepo.save(new Discussion(name, author)); } + /** + * list discussions owned by a certain user + */ public Iterable getOwned(User author){ return discRepo.findByMembership(author.getRegNo()); } + /** + * Create a message and link it to it's discussion + */ public Discussion CreateMessage(Discussion disc, Message msg){ disc.addMessage(msg); return discRepo.save(disc); } + + /** + * Check if a user is in a discussion + */ + public boolean hasDiscussion(User user, long id) { + Discussion disc = discRepo.findById(id).orElse(null); + List members = disc.getMembers(); + return members.contains(user); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Discussion.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Discussion.java index ee0912e..4d76d5f 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Discussion.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Discussion.java @@ -1,5 +1,15 @@ package ovh.herisson.Clyde.Tables.Msg; +/****************************************************** + * @file Discussion.java + * @author Anthony Debucquoy + * @scope Extension messagerie + * + * Discussion allow to regroupe multiple user in and message together + * for the messages application to work + ******************************************************/ + + import java.util.List; import jakarta.persistence.CascadeType; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Message.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Message.java index 246d92c..2afa6ba 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Message.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Message.java @@ -1,5 +1,14 @@ package ovh.herisson.Clyde.Tables.Msg; +/****************************************************** + * @file Message.java + * @author Anthony Debucquoy + * @scope Extension messagerie + * + * Represent a message sent to a discussion + ******************************************************/ + + import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp;