From 5a4d066c4548611f8f9e943a0985cb388c7d2280 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Sun, 7 Apr 2024 18:16:05 +0200 Subject: [PATCH] Forum and topic getter and creator endpoints --- .../Clyde/EndPoints/Msg/ForumController.java | 84 +++++++++++++++++++ .../Repositories/Msg/ForumRepository.java | 9 ++ .../Clyde/Services/Msg/ForumService.java | 29 +++++++ .../ovh/herisson/Clyde/Tables/Course.java | 6 ++ .../ovh/herisson/Clyde/Tables/Msg/Forum.java | 7 ++ 5 files changed, 135 insertions(+) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/ForumController.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/ForumRepository.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Services/Msg/ForumService.java 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 new file mode 100644 index 0000000..ef502d6 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/Msg/ForumController.java @@ -0,0 +1,84 @@ +package ovh.herisson.Clyde.EndPoints.Msg; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; +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.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +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.ForumRepository; +import ovh.herisson.Clyde.Responses.UnauthorizedResponse; +import ovh.herisson.Clyde.Services.AuthenticatorService; +import ovh.herisson.Clyde.Services.CourseService; +import ovh.herisson.Clyde.Services.Msg.ForumService; +import ovh.herisson.Clyde.Tables.Course; +import ovh.herisson.Clyde.Tables.User; +import ovh.herisson.Clyde.Tables.Msg.Forum; +import ovh.herisson.Clyde.Tables.Msg.Topic; + +@RestController +@CrossOrigin(originPatterns = "*", allowCredentials = "true") +@AllArgsConstructor +public class ForumController { + + private CourseRepository courseRepo; + private CourseService courseServ; + private AuthenticatorService authServ; + private ForumService forumServ; + private ForumRepository forumRepo; + + //// Endpoints to get and create new forums + + @GetMapping("/forums/{id}") + public ResponseEntity> getForumFromCourseId(@RequestHeader("Authorization") String token, @PathVariable long id){ + User u = authServ.getUserFromToken(token); + if(u != null){ + return new UnauthorizedResponse<>(null); + } + return new ResponseEntity<>(courseRepo.findById(id).getForums(), HttpStatus.OK); + } + + @PostMapping("/forums/{id}") + public ResponseEntity createForumOfCourse(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Forum data){ + User u = authServ.getUserFromToken(token); + Course c = courseRepo.findById(id); + if(!c.getOwner().equals(u)){ + return new UnauthorizedResponse<>(null); + } + forumServ.createForum(c, data); + return new ResponseEntity<>(HttpStatus.ACCEPTED); + } + + //// Endpoints to get and create forum's topic + + @GetMapping("/forum/{id}") + public ResponseEntity> getTopicsFromForumId(@RequestHeader("Authorization") String token, @PathVariable long id){ + User u = authServ.getUserFromToken(token); + if(u != null){ + return new UnauthorizedResponse<>(null); + } + return new ResponseEntity<>(forumRepo.findById(id).orElse(null).getTopics(), HttpStatus.OK); + } + + @PostMapping("/forum/{id}") + 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)){ + return new UnauthorizedResponse<>(null); + } + forumServ.createTopic(f, data); + return new ResponseEntity<>(HttpStatus.ACCEPTED); + } +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/ForumRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/ForumRepository.java new file mode 100644 index 0000000..07afe54 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/Msg/ForumRepository.java @@ -0,0 +1,9 @@ +package ovh.herisson.Clyde.Repositories.Msg; + +import org.springframework.data.repository.CrudRepository; + +import ovh.herisson.Clyde.Tables.Msg.Forum; + +public interface ForumRepository 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 new file mode 100644 index 0000000..3fbf0df --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/Msg/ForumService.java @@ -0,0 +1,29 @@ +package ovh.herisson.Clyde.Services.Msg; + +import org.springframework.beans.factory.annotation.Autowired; +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.Tables.Course; +import ovh.herisson.Clyde.Tables.Msg.Forum; +import ovh.herisson.Clyde.Tables.Msg.Topic; + +@Service +@AllArgsConstructor +public class ForumService { + + private CourseRepository courseRepo; + private ForumRepository forumRepo; + + public void createForum(Course c, Forum f){ + c.addForum(f); + courseRepo.save(c); + } + + public void createTopic(Forum f, Topic data) { + f.addTopic(data); + forumRepo.save(f); + } +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java index 8870618..7fff00f 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java @@ -1,6 +1,7 @@ package ovh.herisson.Clyde.Tables; import jakarta.persistence.*; +import lombok.Data; import ovh.herisson.Clyde.Tables.Msg.Forum; import java.util.List; @@ -9,6 +10,7 @@ import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; @Entity +@Data public class Course { @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -24,6 +26,10 @@ public class Course { //// Extension Messagerie ///// @OneToMany private List forums; + + public void addForum(Forum f){ + forums.add(f); + } /////////////////////////////// public Course(int credits, String title, User owner){ 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 e021c18..3baa406 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 @@ -21,6 +21,13 @@ public class Forum { private String name; @OneToMany + private List topics; + + public void addTopic(Topic t) { + topics.add(t); + } + + @OneToMany private List writers; // User who are authorized to create a post @OneToMany