1+ package com .demo .controller .api ;
2+
3+ import com .demo .domain .Student ;
4+ import com .demo .dto .request .PostCreateRequest ;
5+ import com .demo .dto .request .PostUpdateRequest ;
6+ import com .demo .dto .response .PostResponse ;
7+ import com .demo .domain .Post ;
8+ import com .demo .service .PostService ;
9+ import lombok .RequiredArgsConstructor ;
10+ import lombok .extern .slf4j .Slf4j ;
11+ import org .springframework .data .domain .Page ;
12+ import org .springframework .security .access .prepost .PreAuthorize ;
13+ import org .springframework .security .core .annotation .AuthenticationPrincipal ;
14+ import org .springframework .security .core .userdetails .UserDetails ;
15+ import org .springframework .web .bind .annotation .*;
16+ import org .springframework .data .domain .PageRequest ;
17+ import org .springframework .data .domain .Pageable ;
18+
19+ import java .util .List ;
20+ import java .util .stream .Collectors ;
21+
22+ @ Slf4j
23+ @ RestController
24+ @ RequiredArgsConstructor
25+ @ RequestMapping ("/api/v1/posts" )
26+ public class PostApiController {
27+
28+ private final PostService postService ;
29+
30+ @ PostMapping
31+ @ PreAuthorize ("hasRole('USER')" )
32+ public PostResponse createPost (@ RequestBody PostCreateRequest request , @ AuthenticationPrincipal UserDetails userDetails ) {
33+ String studentId = userDetails .getUsername ();
34+ log .info ("Creating post: Student ID: {}, Post Title: {}" , studentId , request .getTitle ());
35+ PostResponse response = postService .createPost (request , studentId );
36+ log .info ("Post created successfully: Post ID: {}" , response .getId ());
37+ return response ;
38+ }
39+
40+ @ GetMapping ("/{postId}" )
41+ public PostResponse getPost (@ PathVariable Long postId ) {
42+ log .info ("Fetching post with ID: {}" , postId );
43+ PostResponse response = postService .getPost (postId );
44+ log .info ("Post fetched successfully: Post ID: {}" , postId );
45+ return response ;
46+ }
47+
48+ @ PutMapping ("/{postId}" )
49+ @ PreAuthorize ("hasAnyRole('USER', 'ADMIN')" )
50+ public PostResponse updatePost (@ PathVariable Long postId , @ RequestBody PostUpdateRequest request , @ AuthenticationPrincipal UserDetails userDetails ) {
51+ String studentId = userDetails .getUsername ();
52+ log .info ("Updating post: Post ID: {}, Student ID: {}, New Title: {} " , postId , studentId , request .getTitle ());
53+ PostResponse response = postService .updatePost (postId , request , studentId );
54+ log .info ("Post updated successfully: Post ID: {}" , postId );
55+ return response ;
56+ }
57+
58+ @ DeleteMapping ("/{postId}" )
59+ @ PreAuthorize ("hasAnyRole('USER', 'ADMIN')" )
60+ public void deletePost (@ PathVariable Long postId , @ AuthenticationPrincipal UserDetails userDetails ) {
61+ String studentId = userDetails .getUsername ();
62+ log .info ("Deleting post: Post ID: {}, Student ID: {}" , postId , studentId );
63+ postService .deletePost (postId , studentId );
64+ log .info ("Post deleted successfully: Post ID: {}, " , postId );
65+ }
66+
67+ @ GetMapping
68+ public Page <PostResponse > getPostList (@ RequestParam (required = false ) String titleSearch ,
69+ @ RequestParam (defaultValue = "0" ) int page ,
70+ @ RequestParam (defaultValue = "20" ) int size ) {
71+ Pageable pageable = PageRequest .of (page , size ); // 페이지 정보 설정
72+ log .info ("Fetching post list: Search Query: {}, Page: {}, Size: {}" , titleSearch , page , size );
73+
74+ Page <PostResponse > postPage = (titleSearch == null )
75+ ? postService .getAllPosts (pageable )
76+ : postService .searchPosts (titleSearch , pageable );
77+
78+ log .info ("Fetched {} posts." , postPage .getTotalElements ());
79+ return postPage ;
80+ }
81+ }
0 commit comments