-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathPostController.java
More file actions
155 lines (139 loc) · 6.41 KB
/
Copy pathPostController.java
File metadata and controls
155 lines (139 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.booleanuk.api.controller;
import com.booleanuk.api.model.Comment;
import com.booleanuk.api.model.Post;
import com.booleanuk.api.repository.CommentRepository;
import com.booleanuk.api.repository.PostRepository;
import com.booleanuk.api.repository.UserRepository;
import com.booleanuk.api.response.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
@RestController
@RequestMapping("posts")
public class PostController {
@Autowired
private PostRepository postRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private CommentRepository commentRepository;
private ErrorResponse errorResponse = new ErrorResponse();
private PostResponse postResponse = new PostResponse();
private PostListResponse postListResponse = new PostListResponse();
private CommentListResponse commentListResponse = new CommentListResponse();
private CommentResponse commentResponse = new CommentResponse();
private RepostListResponse repostListResponse = new RepostListResponse();
@GetMapping
public ResponseEntity<Response<?>> getAllPosts() {
postListResponse.set(this.postRepository.findAll());
return ResponseEntity.ok(postListResponse);
}
@GetMapping("{id}/reposts")
public ResponseEntity<Response<?>> getAllRepostsForPost(@PathVariable int id) {
Post postWithReposts = this.postRepository.findById(id).orElse(null);
if (postWithReposts == null) {
this.errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
this.repostListResponse.set(postWithReposts.getReposts());
return ResponseEntity.ok(repostListResponse);
}
@PostMapping
public ResponseEntity<Response<?>> createPost(@RequestBody Post post) {
post.setPosted(LocalDateTime.now());
post.setUpdated(LocalDateTime.now());
try {
postResponse.set(this.postRepository.save(post));
} catch (Exception e) {
errorResponse.set("Bad request");
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(postResponse, HttpStatus.CREATED);
}
@GetMapping("{id}")
public ResponseEntity<Response<?>> getOnePost(@PathVariable int id) {
Post post = this.postRepository.findById(id).orElse(null);
if (post == null) {
this.errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
this.postResponse.set(post);
return ResponseEntity.ok(postResponse);
}
@PutMapping("{id}")
public ResponseEntity<Response<?>> updatePost(@PathVariable int id, @RequestBody Post post) {
Post postToUpdate = this.postRepository.findById(id).orElse(null);
if (postToUpdate == null) {
this.errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
postToUpdate.setTitle(post.getTitle());
postToUpdate.setContent(post.getContent());
postToUpdate.setUpdated(LocalDateTime.now());
try {
postToUpdate = this.postRepository.save(postToUpdate);
} catch (Exception e) {
this.errorResponse.set("Bad request");
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}
this.postResponse.set(postToUpdate);
return new ResponseEntity<>(postResponse, HttpStatus.CREATED);
}
@DeleteMapping("{id}")
public ResponseEntity<Response<?>> deletePost(@PathVariable int id) {
Post postToDelete = this.postRepository.findById(id).orElse(null);
if (postToDelete == null) {
this.errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
this.postRepository.delete(postToDelete);
postResponse.set(postToDelete);
return ResponseEntity.ok(postResponse);
}
@GetMapping("{id}/comments")
public ResponseEntity<Response<?>> getAllComments(@PathVariable int id) {
Post postWithComments = this.postRepository.findById(id).orElse(null);
if (postWithComments == null) {
errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
this.commentListResponse.set(postWithComments.getComments());
return ResponseEntity.ok(commentListResponse);
}
@PostMapping("{id}/comments")
public ResponseEntity<Response<?>> addComment(@PathVariable int id, @RequestBody Comment comment) {
Post postWithComments = this.postRepository.findById(id).orElse(null);
if (postWithComments == null) {
this.errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
postWithComments.getComments().add(comment);
this.commentResponse.set(comment);
return ResponseEntity.ok(commentResponse);
}
@GetMapping("{post_id}/comments/{comment_id}")
public ResponseEntity<Response<?>> getOneComment(@PathVariable int postId, @PathVariable int commentId) {
Post post = this.postRepository.findById(postId).orElse(null);
Comment comment = this.commentRepository.findById(commentId).orElse(null);
if (post == null || comment == null) {
this.errorResponse.set("Post or comment not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
this.commentResponse.set(comment);
return ResponseEntity.ok(commentResponse);
}
@DeleteMapping("{post_id}/friends/{comment_id}")
public ResponseEntity<Response<?>> removeComment(@PathVariable int postId, @PathVariable int commentId) {
Post post = this.postRepository.findById(postId).orElse(null);
Comment comment = this.commentRepository.findById(commentId).orElse(null);
if (post == null || comment == null) {
this.errorResponse.set("Post or comment not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
post.getComments().remove(comment);
this.commentResponse.set(comment);
return ResponseEntity.ok(commentResponse);
}
}