From 4848a2c2695112ace112b2628913ddf36dedc9ba Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Mon, 25 Mar 2024 13:19:27 +0100 Subject: [PATCH] indev --- .../EndPoints/Msg/MessagesController.java | 20 +++- .../Repositories/Msg/MessageRepository.java | 8 ++ .../Clyde/Services/Msg/DiscussionService.java | 6 + .../herisson/Clyde/Tables/Msg/Discussion.java | 10 ++ .../herisson/Clyde/Tables/Msg/Message.java | 36 ++++++ frontend/src/Apps/Msg.vue | 7 +- frontend/src/rest/msg.js | 108 ++++++++++-------- 7 files changed, 142 insertions(+), 53 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/MessageRepository.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Message.java 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 e5534f4..91107e6 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 @@ -4,6 +4,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -11,11 +12,13 @@ import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RestController; import lombok.AllArgsConstructor; +import ovh.herisson.Clyde.Repositories.Msg.DiscussionRepository; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Services.Msg.DiscussionService; import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.Msg.Discussion; +import ovh.herisson.Clyde.Tables.Msg.Message; @RestController @CrossOrigin(originPatterns = "*", allowCredentials = "true") @@ -24,6 +27,7 @@ public class MessagesController { private AuthenticatorService authServ; private DiscussionService discServ; + private DiscussionRepository discRepo; @GetMapping("/discussions") public ResponseEntity> getDiscussions(@RequestHeader("Authorization") String token ){ @@ -39,9 +43,23 @@ public class MessagesController { @GetMapping("/discussion/{id}") public ResponseEntity getDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id){ - return null; // TODO + return new ResponseEntity<>(discRepo.findById(id).orElse(null), HttpStatus.OK); } + @PatchMapping("/discussion/{id}") + public ResponseEntity AlterDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Discussion data){ + return new ResponseEntity<>(discRepo.findById(id).orElse(null), HttpStatus.OK); + } + + @PostMapping("/discussion/{id}") + public ResponseEntity sendMessage(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Message msg){ + Discussion disc = discRepo.findById(id).orElse(null); + if(disc != null) + discServ.CreateMessage(disc, msg); + return new ResponseEntity<>(disc, HttpStatus.OK); + } + + @PostMapping("/discussion") public ResponseEntity createDiscussion(@RequestHeader("Authorization") String token, @RequestBody Discussion data){ return new ResponseEntity<>(discServ.create(data.getName(), authServ.getUserFromToken(token)), HttpStatus.OK); 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 new file mode 100644 index 0000000..8c90e92 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/MessageRepository.java @@ -0,0 +1,8 @@ +package ovh.herisson.Clyde.Repositories.Msg; + +import org.springframework.data.repository.CrudRepository; + +import ovh.herisson.Clyde.Tables.Msg.Message; + +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 e1b36e5..5cca320 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 @@ -6,6 +6,7 @@ import lombok.AllArgsConstructor; 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 @@ -21,4 +22,9 @@ public class DiscussionService { return discRepo.findByMembership(author.getRegNo()); } + public Discussion CreateMessage(Discussion disc, Message msg){ + disc.addMessage(msg); + return discRepo.save(disc); + } + } 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 57e5958..028bb4f 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 @@ -2,6 +2,7 @@ package ovh.herisson.Clyde.Tables.Msg; import java.util.List; +import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -9,6 +10,7 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinTable; import jakarta.persistence.ManyToMany; +import jakarta.persistence.OneToMany; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -33,6 +35,9 @@ public class Discussion{ ) private List members; + @OneToMany(mappedBy="discussion", orphanRemoval = true, cascade = CascadeType.ALL) + private List msgs; + public Discussion(String name){ this.name = name; } @@ -41,4 +46,9 @@ public class Discussion{ this.name = name; this.members = List.of(user); } + + public void addMessage(Message msg){ + msg.setDiscussion(this); + msgs.add(msg); + } } 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 new file mode 100644 index 0000000..26ce1ef --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Message.java @@ -0,0 +1,36 @@ +package ovh.herisson.Clyde.Tables.Msg; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToOne; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import ovh.herisson.Clyde.Tables.User; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Entity +public class Message { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + private String content; + + @OneToOne + private User author; + + @OneToOne + private Message response; + + @ManyToOne(optional = false) + private Discussion discussion; + +} diff --git a/frontend/src/Apps/Msg.vue b/frontend/src/Apps/Msg.vue index 433300c..cb78eae 100644 --- a/frontend/src/Apps/Msg.vue +++ b/frontend/src/Apps/Msg.vue @@ -7,9 +7,10 @@ @@ -27,8 +28,8 @@
- - + +
diff --git a/frontend/src/rest/msg.js b/frontend/src/rest/msg.js index 1cecd42..d862cba 100644 --- a/frontend/src/rest/msg.js +++ b/frontend/src/rest/msg.js @@ -21,55 +21,55 @@ export async function getDiscussions(){ } export async function fetchDiscussion(id){ - currentDiscussion.value = { - id: id, - name: "Discussion#2", - msgs: [ - { - id: 1, - author: 1, - sender: true, - attachment: null, - text: "Hello world!" - }, - { - id: 2, - author: 2, - sender: false, - attachment: null, - text: "Hello What?" - }, - { - id: 3, - author: 2, - sender: false, - attachment: null, - text: "You morron" - }, - { - id: 4, - author: 1, - sender: true, - attachment: null, - text: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." - }, - // { - // id: 5, - // author: 1, - // sender: true, - // attachment: null, - // text: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." - // }, - { - id: 6, - author: 2, - sender: false, - attachment: null, - text: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." - } - ] - } - // currentDiscussion.value = restGet("/discussion/" + id); + // currentDiscussion.value = { + // id: id, + // name: "Discussion#2", + // msgs: [ + // { + // id: 1, + // author: 1, + // sender: true, + // attachment: null, + // text: "Hello world!" + // }, + // { + // id: 2, + // author: 2, + // sender: false, + // attachment: null, + // text: "Hello What?" + // }, + // { + // id: 3, + // author: 2, + // sender: false, + // attachment: null, + // text: "You morron" + // }, + // { + // id: 4, + // author: 1, + // sender: true, + // attachment: null, + // text: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." + // }, + // // { + // // id: 5, + // // author: 1, + // // sender: true, + // // attachment: null, + // // text: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." + // // }, + // { + // id: 6, + // author: 2, + // sender: false, + // attachment: null, + // text: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." + // } + // ] + // } + currentDiscussion.value = await restGet("/discussion/" + id); } export async function createDiscussion(name){ @@ -80,3 +80,13 @@ export async function createDiscussion(name){ export async function invite(id, regNo){ restPost("/discussion/"+ id+ "/invite", {user: regNo}); } + +export async function sendMessage(id, content, responseId){ + let data = { + content: content, + response: responseId, + } + console.log(content) + console.log(data) + restPost("/discussion/" + id, data); +}