From 106bf96a9815427db0d0de504e1f172e18973bd8 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Tue, 9 Apr 2024 23:41:24 +0200 Subject: [PATCH] responses and general modifications --- .../Clyde/EndPoints/Msg/ForumController.java | 23 +++++++-- .../Repositories/Msg/AnswerRepository.java | 10 ++++ .../Clyde/Services/Msg/ForumService.java | 10 ++++ .../Tables/Msg/{Answers.java => Answer.java} | 7 +-- .../ovh/herisson/Clyde/Tables/Msg/Forum.java | 2 +- .../ovh/herisson/Clyde/Tables/Msg/Topic.java | 11 ++-- frontend/src/Apps/Forums.vue | 48 +++++++++++++---- frontend/src/rest/forum.js | 51 +++++++------------ 8 files changed, 105 insertions(+), 57 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/AnswerRepository.java rename backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/{Answers.java => Answer.java} (85%) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/ForumController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/ForumController.java index 8163a69..2186129 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/ForumController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/ForumController.java @@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController; import jakarta.websocket.server.PathParam; import lombok.AllArgsConstructor; import ovh.herisson.Clyde.Repositories.CourseRepository; +import ovh.herisson.Clyde.Repositories.Msg.AnswerRepository; import ovh.herisson.Clyde.Repositories.Msg.ForumRepository; import ovh.herisson.Clyde.Repositories.Msg.TopicRepository; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; @@ -27,6 +28,7 @@ import ovh.herisson.Clyde.Tables.Course; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.User; +import ovh.herisson.Clyde.Tables.Msg.Answer; import ovh.herisson.Clyde.Tables.Msg.Forum; import ovh.herisson.Clyde.Tables.Msg.Topic; @@ -36,7 +38,6 @@ import ovh.herisson.Clyde.Tables.Msg.Topic; public class ForumController { private CourseRepository courseRepo; - private CourseService courseServ; private AuthenticatorService authServ; private ForumService forumServ; private ForumRepository forumRepo; @@ -69,7 +70,7 @@ public class ForumController { @GetMapping("/forum/{id}") public ResponseEntity> getTopicsFromForumId(@RequestHeader("Authorization") String token, @PathVariable long id){ User u = authServ.getUserFromToken(token); - if(u != null){ + if(u == null){ return new UnauthorizedResponse<>(null); } return new ResponseEntity<>(forumRepo.findById(id).orElse(null).getTopics(), HttpStatus.OK); @@ -79,7 +80,7 @@ public class ForumController { public ResponseEntity postTopicToForum(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Topic data){ User u = authServ.getUserFromToken(token); Forum f = forumRepo.findById(id).orElse(null); - if(!f.getWriters().contains(u)){ + if(!(f.getWriters().contains(u) || u.getRole() == Role.Admin)){ return new UnauthorizedResponse<>(null); } forumServ.createTopic(f, data); @@ -91,13 +92,25 @@ public class ForumController { @GetMapping("/forum/post/{id}") public ResponseEntity getPost(@RequestHeader("Authorization") String token, @PathVariable long id){ User u = authServ.getUserFromToken(token); - if(u != null){ + if(u == null){ return new UnauthorizedResponse<>(null); } Topic t = topicRepo.findById(id).orElse(null); return new ResponseEntity<>(t, HttpStatus.OK); } + @PostMapping("/forum/post/{id}") + public ResponseEntity postTopicToForum(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Answer data){ + User u = authServ.getUserFromToken(token); + Topic t = topicRepo.findById(id).orElse(null); + if(t.isLocked() && u.getRole() != Role.Admin){ + return new UnauthorizedResponse<>(null); + } + System.out.println(data); + forumServ.answerTopic(t, data, u); + return new ResponseEntity<>(HttpStatus.ACCEPTED); + } - // TODO: Create a new post/topic and response to a topic + + // TODO: Check if authorization to view a post/forum/... } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/AnswerRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/AnswerRepository.java new file mode 100644 index 0000000..0ca5e22 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/AnswerRepository.java @@ -0,0 +1,10 @@ +package ovh.herisson.Clyde.Repositories.Msg; + +import org.springframework.data.repository.CrudRepository; + +import ovh.herisson.Clyde.Tables.Msg.Answer; + +public interface AnswerRepository extends CrudRepository { + +} + diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/ForumService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/ForumService.java index ddf5be6..02fca22 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/ForumService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/ForumService.java @@ -5,7 +5,10 @@ import org.springframework.stereotype.Service; import lombok.AllArgsConstructor; import ovh.herisson.Clyde.Repositories.CourseRepository; import ovh.herisson.Clyde.Repositories.Msg.ForumRepository; +import ovh.herisson.Clyde.Repositories.Msg.TopicRepository; import ovh.herisson.Clyde.Tables.Course; +import ovh.herisson.Clyde.Tables.User; +import ovh.herisson.Clyde.Tables.Msg.Answer; import ovh.herisson.Clyde.Tables.Msg.Forum; import ovh.herisson.Clyde.Tables.Msg.Topic; @@ -15,6 +18,7 @@ public class ForumService { private CourseRepository courseRepo; private ForumRepository forumRepo; + private TopicRepository topicRepo; public void createForum(Course c, Forum f){ c.addForum(f); @@ -25,4 +29,10 @@ public class ForumService { f.addTopic(data); forumRepo.save(f); } + + public void answerTopic(Topic t, Answer data, User u) { + data.setAuthor(u); + t.addAnswer(data); + topicRepo.save(t); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Answers.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Answer.java similarity index 85% rename from backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Answers.java rename to backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Answer.java index 2f4d452..4101abb 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Answers.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Answer.java @@ -11,7 +11,7 @@ import ovh.herisson.Clyde.Tables.User; @Entity @Data -public class Answers { +public class Answer { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @@ -19,12 +19,9 @@ public class Answers { @CreationTimestamp private Date creation; - @ManyToOne - private Topic topic; - private String content; - @OneToOne + @ManyToOne(cascade=CascadeType.ALL) private User author; private boolean anonymous; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Forum.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Forum.java index 3ca1637..75508aa 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Forum.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Forum.java @@ -20,7 +20,7 @@ public class Forum { private String name; - @OneToMany + @OneToMany(cascade = CascadeType.ALL) private List topics; public void addTopic(Topic t) { diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Topic.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Topic.java index d63b3e1..3641f5f 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Topic.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Msg/Topic.java @@ -16,11 +16,16 @@ public class Topic { private String subject, content; - @OneToOne + @ManyToOne private User author; - @OneToMany(mappedBy = "topic", cascade = CascadeType.ALL) - private List answers; + @OneToMany(cascade = CascadeType.ALL) + private List answers; + + public void addAnswer(Answer a){ + answers.add(a); + } private boolean locked; // true if new messages can be posted + } diff --git a/frontend/src/Apps/Forums.vue b/frontend/src/Apps/Forums.vue index 5f56704..50e17f6 100644 --- a/frontend/src/Apps/Forums.vue +++ b/frontend/src/Apps/Forums.vue @@ -9,8 +9,8 @@ import { ref, reactive } from 'vue' import { getCourses } from '@/rest/courses.js' import { ForumsOfCurrentCourse, getForumsOfCourse, createForum } from '@/rest/forum.js' -import { PostsOfCurrentForum, getPostsOfForum } from '@/rest/forum.js' -import { fetchedPost, fetchPost } from '@/rest/forum.js' +import { PostsOfCurrentForum, getPostsOfForum, createPost } from '@/rest/forum.js' +import { fetchedPost, fetchPost, sendAnswer } from '@/rest/forum.js' import { getSelf } from '@/rest/Users.js' const courses = await reactive(getCourses()); @@ -20,6 +20,9 @@ const Role = (await getSelf()).role; const addForum = ref(false); const addForumName = ref(""); +const addPost = ref(false); +const addPostSubject = ref(""); +const addPostContent = ref(""); @@ -36,8 +39,8 @@ const addForumName = ref("");
-
{{ post.name }}
- +
{{ post.subject }}
+
@@ -45,21 +48,33 @@ const addForumName = ref(""); {{fetchedPost.content}}
-

{{msg.author}} - {{msg.content}}

+

{{msg.author.firtName}} {{msg.author.lastName}} - {{msg.content}}

+
-
-
+