Forum and topic getter and creator endpoints
This commit is contained in:
parent
f9bcff6d4f
commit
5a4d066c45
@ -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<List<Forum>> 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<Forum> 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<List<Topic>> 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<Topic> 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);
|
||||||
|
}
|
||||||
|
}
|
@ -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<Forum, Long> {
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package ovh.herisson.Clyde.Tables;
|
package ovh.herisson.Clyde.Tables;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Data;
|
||||||
import ovh.herisson.Clyde.Tables.Msg.Forum;
|
import ovh.herisson.Clyde.Tables.Msg.Forum;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -9,6 +10,7 @@ import org.hibernate.annotations.OnDelete;
|
|||||||
import org.hibernate.annotations.OnDeleteAction;
|
import org.hibernate.annotations.OnDeleteAction;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
@Data
|
||||||
public class Course {
|
public class Course {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
@ -24,6 +26,10 @@ public class Course {
|
|||||||
//// Extension Messagerie /////
|
//// Extension Messagerie /////
|
||||||
@OneToMany
|
@OneToMany
|
||||||
private List<Forum> forums;
|
private List<Forum> forums;
|
||||||
|
|
||||||
|
public void addForum(Forum f){
|
||||||
|
forums.add(f);
|
||||||
|
}
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
||||||
public Course(int credits, String title, User owner){
|
public Course(int credits, String title, User owner){
|
||||||
|
@ -21,6 +21,13 @@ public class Forum {
|
|||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@OneToMany
|
@OneToMany
|
||||||
|
private List<Topic> topics;
|
||||||
|
|
||||||
|
public void addTopic(Topic t) {
|
||||||
|
topics.add(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OneToMany
|
||||||
private List<User> writers; // User who are authorized to create a post
|
private List<User> writers; // User who are authorized to create a post
|
||||||
|
|
||||||
@OneToMany
|
@OneToMany
|
||||||
|
Loading…
Reference in New Issue
Block a user