| [700e2f9] | 1 | package com.finki.icare.controller;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.dto.*;
|
|---|
| 4 | import com.finki.icare.service.BlogService;
|
|---|
| 5 | import com.finki.icare.service.CommentService;
|
|---|
| 6 | import com.finki.icare.utils.AuthUtils;
|
|---|
| 7 | import lombok.RequiredArgsConstructor;
|
|---|
| 8 | import org.springframework.http.HttpStatus;
|
|---|
| 9 | import org.springframework.http.ResponseEntity;
|
|---|
| 10 | import org.springframework.security.core.Authentication;
|
|---|
| 11 | import org.springframework.web.bind.annotation.*;
|
|---|
| 12 |
|
|---|
| 13 | import java.util.List;
|
|---|
| 14 |
|
|---|
| 15 | @RestController
|
|---|
| 16 | @RequestMapping("/api/blogs")
|
|---|
| 17 | @CrossOrigin(origins = "http://localhost:3000")
|
|---|
| 18 | @RequiredArgsConstructor
|
|---|
| 19 | public class BlogController {
|
|---|
| 20 |
|
|---|
| 21 | private final BlogService blogService;
|
|---|
| 22 | private final CommentService commentService;
|
|---|
| 23 |
|
|---|
| 24 | @GetMapping
|
|---|
| 25 | public ResponseEntity<List<BlogDTO>> getAllBlogs(Authentication authentication) {
|
|---|
| 26 | Integer currentUserId = AuthUtils.getUserId(authentication);
|
|---|
| 27 | List<BlogDTO> blogs = blogService.getAllBlogs(currentUserId);
|
|---|
| 28 | return ResponseEntity.ok(blogs);
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | @GetMapping("/{id}")
|
|---|
| 32 | public ResponseEntity<BlogDTO> getBlogById(
|
|---|
| 33 | @PathVariable Integer id,
|
|---|
| 34 | Authentication authentication
|
|---|
| 35 | ) {
|
|---|
| 36 |
|
|---|
| 37 | Integer userId = AuthUtils.getUserId(authentication);
|
|---|
| 38 | BlogDTO blog = blogService.getBlogById(id, userId);
|
|---|
| 39 | return ResponseEntity.ok(blog);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | @PostMapping
|
|---|
| 43 | public ResponseEntity<BlogDTO> createBlog(
|
|---|
| 44 | @RequestBody CreateBlogRequest request,
|
|---|
| 45 | Authentication authentication
|
|---|
| 46 | ) {
|
|---|
| 47 |
|
|---|
| 48 | Integer userId = AuthUtils.getUserId(authentication);
|
|---|
| 49 | BlogDTO createdBlog = blogService.createBlog(request, userId);
|
|---|
| 50 | return ResponseEntity.status(HttpStatus.CREATED).body(createdBlog);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | @PutMapping("/{id}")
|
|---|
| 54 | public ResponseEntity<BlogDTO> updateBlog(
|
|---|
| 55 | @PathVariable Integer id,
|
|---|
| 56 | @RequestBody UpdateBlogRequest request,
|
|---|
| 57 | Authentication authentication
|
|---|
| 58 | ) {
|
|---|
| 59 |
|
|---|
| 60 | Integer userId = AuthUtils.getUserId(authentication);
|
|---|
| 61 | BlogDTO updatedBlog = blogService.updateBlog(id, request, userId);
|
|---|
| 62 | return ResponseEntity.ok(updatedBlog);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | @DeleteMapping("/{id}")
|
|---|
| 66 | public ResponseEntity<Void> deleteBlog(
|
|---|
| 67 | @PathVariable Integer id,
|
|---|
| 68 | Authentication authentication
|
|---|
| 69 | ) {
|
|---|
| 70 |
|
|---|
| 71 | Integer userId = AuthUtils.getUserId(authentication);
|
|---|
| 72 | blogService.deleteBlog(id, userId);
|
|---|
| 73 | return ResponseEntity.noContent().build();
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | @PostMapping("/{id}/like")
|
|---|
| 77 | public ResponseEntity<Void> toggleLike(
|
|---|
| 78 | @PathVariable Integer id,
|
|---|
| 79 | Authentication authentication
|
|---|
| 80 | ) {
|
|---|
| 81 |
|
|---|
| 82 | Integer userId = AuthUtils.getUserId(authentication);
|
|---|
| 83 | blogService.toggleLike(id, userId);
|
|---|
| 84 | return ResponseEntity.ok().build();
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | @PostMapping("/{id}/comments")
|
|---|
| 88 | public ResponseEntity<CommentDTO> addComment(
|
|---|
| 89 | @PathVariable Integer id,
|
|---|
| 90 | @RequestBody CreateCommentRequest request,
|
|---|
| 91 | Authentication authentication
|
|---|
| 92 | ) {
|
|---|
| 93 |
|
|---|
| 94 | Integer userId = AuthUtils.getUserId(authentication);
|
|---|
| 95 | CommentDTO comment = commentService.createComment(id, request, userId);
|
|---|
| 96 | return ResponseEntity.status(HttpStatus.CREATED).body(comment);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | @PutMapping("/comments/{commentId}")
|
|---|
| 100 | public ResponseEntity<CommentDTO> updateComment(
|
|---|
| 101 | @PathVariable Integer commentId,
|
|---|
| 102 | @RequestBody CreateCommentRequest request,
|
|---|
| 103 | Authentication authentication
|
|---|
| 104 | ) {
|
|---|
| 105 | Integer userId = AuthUtils.getUserId(authentication);
|
|---|
| 106 | CommentDTO updatedComment = commentService.updateComment(commentId, request.getContent(), userId);
|
|---|
| 107 | return ResponseEntity.ok(updatedComment);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | @DeleteMapping("/comments/{commentId}")
|
|---|
| 111 | public ResponseEntity<Void> deleteComment(
|
|---|
| 112 | @PathVariable Integer commentId,
|
|---|
| 113 | Authentication authentication
|
|---|
| 114 | ) {
|
|---|
| 115 | Integer userId = AuthUtils.getUserId(authentication);
|
|---|
| 116 | commentService.deleteComment(commentId, userId);
|
|---|
| 117 | return ResponseEntity.noContent().build();
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|