expandAdminFunction
Change-Id: If3b875b3017d1922b15150dd735ca2ce5a3a77f0
diff --git a/src/main/java/com/example/g8backend/controller/PostController.java b/src/main/java/com/example/g8backend/controller/PostController.java
index 6800666..41be09a 100644
--- a/src/main/java/com/example/g8backend/controller/PostController.java
+++ b/src/main/java/com/example/g8backend/controller/PostController.java
@@ -51,26 +51,12 @@
@GetMapping("/{postId}")
public ResponseEntity<ApiResponse<Post>> getPost(@PathVariable Long postId) {
+ Post post = postService.getById(postId);
long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
postService.recordViewHistory(userId, postId);
- Post post = postService.getById(postId);
return ResponseEntity.ok(ApiResponse.success(post));
}
- @DeleteMapping("/{postId}")
- public ResponseEntity<ApiResponse<String>> deletePost(@PathVariable Long postId) {
- long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
- Post post = postService.getById(postId);
- if (post == null) {
- return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found."));
- }
- if (post.getUserId() != userId) {
- return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to delete this post."));
- }
- postService.removeById(postId);
- return ResponseEntity.ok(ApiResponse.message("Post deleted successfully."));
- }
-
@GetMapping("/getAll")
public ResponseEntity<ApiResponse<List<Post>>> getAllPosts() {
return ResponseEntity.ok(ApiResponse.success(postService.list()));
@@ -209,4 +195,18 @@
}
}
+ @DeleteMapping("/{postId}")
+ public ResponseEntity<ApiResponse<String>> deletePost(@PathVariable Long postId) {
+ long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
+ Post post = postService.getById(postId);
+ if (post == null) {
+ return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found."));
+ }
+ if (post.getUserId() != userId) {
+ return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to delete this post."));
+ }
+ postService.removeById(postId);
+ return ResponseEntity.ok(ApiResponse.message("Post deleted successfully."));
+ }
+
}