responses and general modifications
This commit is contained in:
@ -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<List<Topic>> 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<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)){
|
||||
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<Topic> 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<Topic> 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: <tonitch> Create a new post/topic and response to a topic
|
||||
|
||||
// TODO: <tonitch> Check if authorization to view a post/forum/...
|
||||
}
|
||||
|
@ -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<Answer, Long> {
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
@ -20,7 +20,7 @@ public class Forum {
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToMany
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
private List<Topic> topics;
|
||||
|
||||
public void addTopic(Topic t) {
|
||||
|
@ -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> answers;
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
private List<Answer> answers;
|
||||
|
||||
public void addAnswer(Answer a){
|
||||
answers.add(a);
|
||||
}
|
||||
|
||||
private boolean locked; // true if new messages can be posted
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user