From 384bd106be7e0615c996b5fdf0d5aaab14a90e3a Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Tue, 26 Mar 2024 07:24:26 +0100 Subject: [PATCH] DTO --- .../herisson/Clyde/DTO/Msg/DiscussionDTO.java | 26 +++++++++++++++++++ .../herisson/Clyde/DTO/Msg/MessagesDTO.java | 23 ++++++++++++++++ .../EndPoints/Msg/MessagesController.java | 5 ++-- .../Clyde/Services/Msg/DiscussionService.java | 7 ++--- .../herisson/Clyde/Tables/Msg/Message.java | 10 ++++++- 5 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/DTO/Msg/DiscussionDTO.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/DTO/Msg/MessagesDTO.java 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 new file mode 100644 index 0000000..7b4d4f8 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/DTO/Msg/DiscussionDTO.java @@ -0,0 +1,26 @@ +package ovh.herisson.Clyde.DTO.Msg; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import lombok.AllArgsConstructor; +import lombok.Data; +import ovh.herisson.Clyde.Tables.User; +import ovh.herisson.Clyde.Tables.Msg.Discussion; +import ovh.herisson.Clyde.DTO.Msg.MessagesDTO; + +@Data +@AllArgsConstructor +public class DiscussionDTO { + private long id; + private String name; + private List members; + private List msgs; + + public static DiscussionDTO construct(Discussion d, User u){ + List msgsdto = new ArrayList<>(); + d.getMsgs().forEach(x -> msgsdto.add(MessagesDTO.construct(x, u))); + return new DiscussionDTO(d.getId(), d.getName(), d.getMembers(), msgsdto); + } +} 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 new file mode 100644 index 0000000..7eb7ea1 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/DTO/Msg/MessagesDTO.java @@ -0,0 +1,23 @@ +package ovh.herisson.Clyde.DTO.Msg; + +import lombok.AllArgsConstructor; +import lombok.Data; +import ovh.herisson.Clyde.Tables.User; +import ovh.herisson.Clyde.Tables.Msg.Message; + +@Data +@AllArgsConstructor +public class MessagesDTO { + private long id; + private String content; + private User author; + private boolean sender; + //TODO: Attachment + + public static MessagesDTO construct(Message m, User user){ + boolean sender = false; + if(m.getAuthor().equals(user)) + sender = true; + return new MessagesDTO(m.getId(), m.getContent(), m.getAuthor(), sender); + } +} 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 91107e6..e42e8c0 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 @@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RestController; import lombok.AllArgsConstructor; +import ovh.herisson.Clyde.DTO.Msg.DiscussionDTO; import ovh.herisson.Clyde.Repositories.Msg.DiscussionRepository; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Services.AuthenticatorService; @@ -42,8 +43,8 @@ public class MessagesController { } @GetMapping("/discussion/{id}") - public ResponseEntity getDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id){ - return new ResponseEntity<>(discRepo.findById(id).orElse(null), HttpStatus.OK); + public ResponseEntity getDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id){ + return new ResponseEntity<>(DiscussionDTO.construct(discRepo.findById(id).orElse(null), authServ.getUserFromToken(token)), HttpStatus.OK); } @PatchMapping("/discussion/{id}") 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 5cca320..c1aa534 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,17 +1,19 @@ package ovh.herisson.Clyde.Services.Msg; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import lombok.AllArgsConstructor; +import com.fasterxml.jackson.databind.util.JSONPObject; + import ovh.herisson.Clyde.Repositories.Msg.DiscussionRepository; import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.Msg.Discussion; import ovh.herisson.Clyde.Tables.Msg.Message; @Service -@AllArgsConstructor public class DiscussionService { + @Autowired private DiscussionRepository discRepo; public Discussion create(String name, User author){ @@ -26,5 +28,4 @@ public class DiscussionService { disc.addMessage(msg); return discRepo.save(disc); } - } 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 26ce1ef..a06e2b3 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,7 @@ package ovh.herisson.Clyde.Tables.Msg; +import com.fasterxml.jackson.annotation.JsonIgnore; + import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -27,10 +29,16 @@ public class Message { @OneToOne private User author; - @OneToOne + public User getAuthor() { + return author; + } + + @OneToOne private Message response; @ManyToOne(optional = false) + @JsonIgnore private Discussion discussion; + //TODO: Attachment }