1+ package com .back .domain .board .controller ;
2+
3+ import com .back .domain .board .dto .*;
4+ import com .back .domain .board .service .PostService ;
5+ import com .back .global .common .dto .RsData ;
6+ import com .back .global .security .user .CustomUserDetails ;
7+ import jakarta .validation .Valid ;
8+ import lombok .RequiredArgsConstructor ;
9+ import org .springframework .data .domain .Pageable ;
10+ import org .springframework .data .domain .Sort ;
11+ import org .springframework .data .web .PageableDefault ;
12+ import org .springframework .http .HttpStatus ;
13+ import org .springframework .http .ResponseEntity ;
14+ import org .springframework .security .core .annotation .AuthenticationPrincipal ;
15+ import org .springframework .web .bind .annotation .*;
16+
17+ @ RestController
18+ @ RequestMapping ("/api/posts" )
19+ @ RequiredArgsConstructor
20+ public class PostController implements PostControllerDocs {
21+ private final PostService postService ;
22+
23+ // 게시글 생성
24+ @ PostMapping
25+ public ResponseEntity <RsData <PostResponse >> createPost (
26+ @ RequestBody @ Valid PostRequest request ,
27+ @ AuthenticationPrincipal CustomUserDetails user
28+ ) {
29+ PostResponse response = postService .createPost (request , user .getUserId ());
30+ return ResponseEntity
31+ .status (HttpStatus .CREATED )
32+ .body (RsData .success (
33+ "게시글이 생성되었습니다." ,
34+ response
35+ ));
36+ }
37+
38+ // 게시글 다건 조회
39+ @ GetMapping
40+ public ResponseEntity <RsData <PageResponse <PostListResponse >>> getPosts (
41+ @ PageableDefault (sort = "createdAt" , direction = Sort .Direction .DESC ) Pageable pageable ,
42+ @ RequestParam (required = false ) String keyword ,
43+ @ RequestParam (required = false ) String searchType ,
44+ @ RequestParam (required = false ) Long categoryId
45+ ) {
46+ PageResponse <PostListResponse > response = postService .getPosts (keyword , searchType , categoryId , pageable );
47+ return ResponseEntity
48+ .status (HttpStatus .OK )
49+ .body (RsData .success (
50+ "게시글 목록이 조회되었습니다." ,
51+ response
52+ ));
53+ }
54+
55+ // 게시글 단건 조회
56+ @ GetMapping ("/{postId}" )
57+ public ResponseEntity <RsData <PostDetailResponse >> getPost (
58+ @ PathVariable Long postId
59+ ) {
60+ PostDetailResponse response = postService .getPost (postId );
61+ return ResponseEntity
62+ .status (HttpStatus .OK )
63+ .body (RsData .success (
64+ "게시글이 조회되었습니다." ,
65+ response
66+ ));
67+ }
68+
69+ // 게시글 수정
70+ @ PutMapping ("/{postId}" )
71+ public ResponseEntity <RsData <PostResponse >> updatePost (
72+ @ PathVariable Long postId ,
73+ @ RequestBody @ Valid PostRequest request ,
74+ @ AuthenticationPrincipal CustomUserDetails user
75+ ) {
76+ PostResponse response = postService .updatePost (postId , request , user .getUserId ());
77+ return ResponseEntity
78+ .status (HttpStatus .OK )
79+ .body (RsData .success (
80+ "게시글이 수정되었습니다." ,
81+ response
82+ ));
83+ }
84+
85+ // 게시글 삭제
86+ @ DeleteMapping ("/{postId}" )
87+ public ResponseEntity <RsData <Void >> deletePost (
88+ @ PathVariable Long postId ,
89+ @ AuthenticationPrincipal CustomUserDetails user
90+ ) {
91+ postService .deletePost (postId , user .getUserId ());
92+ return ResponseEntity
93+ .status (HttpStatus .OK )
94+ .body (RsData .success (
95+ "게시글이 삭제되었습니다." ,
96+ null
97+ ));
98+ }
99+ }
0 commit comments