1+ package com .webservice .algorithmchef .controller ;
2+
3+ import com .webservice .algorithmchef .dto .board .BoardCommentRequest ;
4+ import com .webservice .algorithmchef .dto .board .BoardPostResponse ;
5+ import com .webservice .algorithmchef .dto .board .BoardPostListResponse ;
6+ import com .webservice .algorithmchef .dto .board .BoardPostRequest ;
7+ import com .webservice .algorithmchef .dto .board .CommentReplyListResponse ;
8+ import com .webservice .algorithmchef .service .BoardService ;
9+ import lombok .RequiredArgsConstructor ;
10+ import lombok .extern .slf4j .Slf4j ;
11+ import org .springframework .http .HttpStatus ;
12+ import org .springframework .http .ResponseEntity ;
13+ import org .springframework .security .core .annotation .AuthenticationPrincipal ;
14+ import org .springframework .security .core .userdetails .UserDetails ;
15+ import org .springframework .web .bind .annotation .*;
16+
17+ import java .util .Map ;
18+
19+ @ Slf4j
20+ @ RestController
21+ @ RequestMapping ("/board" )
22+ @ RequiredArgsConstructor
23+ public class BoardController {
24+
25+ private final BoardService boardService ;
26+
27+ // 게시글 목록 조회(게시판)
28+ @ GetMapping ("/posts" )
29+ public ResponseEntity <BoardPostListResponse > getPostList (
30+ @ RequestParam (defaultValue = "0" ) int page ,
31+ @ RequestParam (defaultValue = "20" ) int size ,
32+ @ RequestParam (defaultValue = "createdAt,desc" ) String sort ,
33+ @ RequestParam (required = false ) String filter
34+ ) {
35+ BoardPostListResponse response = boardService .getPostList (page , size , sort , filter );
36+ return ResponseEntity .ok (response );
37+ }
38+
39+ // 게시글 작성
40+ @ PostMapping ("/post" )
41+ public ResponseEntity <Map <String , String >> createPost (
42+ @ RequestBody BoardPostRequest requestDto ,
43+ @ AuthenticationPrincipal UserDetails userDetails
44+ ) {
45+ if (userDetails == null ) {
46+ return ResponseEntity .status (HttpStatus .UNAUTHORIZED ).build ();
47+ }
48+
49+ boardService .createPost (requestDto , userDetails .getUsername ());
50+
51+ return ResponseEntity .status (HttpStatus .CREATED )
52+ .body (Map .of ("message" , "게시글이 작성되었습니다." ));
53+ }
54+
55+ // 게시글 조회
56+ @ GetMapping ("/post/{postId}" )
57+ public ResponseEntity <BoardPostResponse > getPostDetail (
58+ @ PathVariable Long postId ,
59+ @ RequestParam (defaultValue = "0" ) int page ,
60+ @ RequestParam (defaultValue = "20" ) int size ,
61+ @ RequestParam (defaultValue = "createdAt,asc" ) String sort
62+ ) {
63+ try {
64+ BoardPostResponse response = boardService .getPostDetail (postId , page , size , sort );
65+ return ResponseEntity .ok (response );
66+ } catch (IllegalArgumentException e ) {
67+ return ResponseEntity .status (HttpStatus .NOT_FOUND ).build ();
68+ }
69+ }
70+
71+ // 댓글 작성
72+ @ PostMapping ("/post/{postId}/comment" )
73+ public ResponseEntity <Map <String , String >> createComment (
74+ @ PathVariable Long postId ,
75+ @ RequestBody BoardCommentRequest requestDto ,
76+ @ AuthenticationPrincipal UserDetails userDetails
77+ ) {
78+ if (userDetails == null ) {
79+ return ResponseEntity .status (HttpStatus .UNAUTHORIZED ).build ();
80+ }
81+
82+ try {
83+ boardService .createComment (postId , requestDto , userDetails .getUsername ());
84+ return ResponseEntity .status (HttpStatus .CREATED )
85+ .body (Map .of ("message" , "댓글 작성완료" ));
86+ } catch (IllegalArgumentException e ) {
87+ return ResponseEntity .badRequest ().body (Map .of ("error" , e .getMessage ()));
88+ }
89+ }
90+
91+ // 대댓글 조회
92+ @ GetMapping ("/comments/{commentId}/replies" )
93+ public ResponseEntity <CommentReplyListResponse > getReplies (
94+ @ PathVariable Long commentId ,
95+ @ RequestParam (defaultValue = "0" ) int page ,
96+ @ RequestParam (defaultValue = "20" ) int size ,
97+ @ RequestParam (defaultValue = "createdAt,asc" ) String sort
98+ ) {
99+ try {
100+ CommentReplyListResponse response = boardService .getReplies (commentId , page , size , sort );
101+ return ResponseEntity .ok (response );
102+ } catch (IllegalArgumentException e ) {
103+ return ResponseEntity .status (HttpStatus .NOT_FOUND ).build ();
104+ }
105+ }
106+
107+ // 게시글 수정
108+ @ PutMapping ("/post/{postId}" )
109+ public ResponseEntity <Map <String , String >> updatePost (
110+ @ PathVariable Long postId ,
111+ @ RequestBody BoardPostRequest requestDto ,
112+ @ AuthenticationPrincipal UserDetails userDetails
113+ ) {
114+ if (userDetails == null ) {
115+ return ResponseEntity .status (HttpStatus .UNAUTHORIZED ).build ();
116+ // 401 Unauthorized
117+ }
118+
119+ try {
120+ // userDetails.getUsername()은 JWT에서 추출한 userId(String)
121+ boardService .updatePost (postId , requestDto , userDetails .getUsername ());
122+
123+ return ResponseEntity .ok (Map .of ("message" , "게시글이 수정되었습니다." ));
124+ } catch (IllegalArgumentException e ) {
125+ // 게시글이 없거나, 권한이 없는 경우
126+ return ResponseEntity .badRequest ().body (Map .of ("error" , e .getMessage ()));
127+ } catch (Exception e ) {
128+ log .error ("게시글 수정 오류" , e );
129+ return ResponseEntity .internalServerError ().body (Map .of ("error" , "서버 오류가 발생했습니다." ));
130+ }
131+ }
132+
133+ // 게시글 삭제
134+ @ DeleteMapping ("/post/{postId}" )
135+ public ResponseEntity <Map <String , String >> deletePost (
136+ @ PathVariable Long postId ,
137+ @ AuthenticationPrincipal UserDetails userDetails
138+ ) {
139+ if (userDetails == null ) {
140+ return ResponseEntity .status (HttpStatus .UNAUTHORIZED ).build ();
141+ }
142+
143+ try {
144+ boardService .deletePost (postId , userDetails .getUsername ());
145+
146+ return ResponseEntity .ok (Map .of ("message" , "게시글이 삭제되었습니다." ));
147+ } catch (IllegalArgumentException e ) {
148+ // 게시글이 없거나, 권한이 없는 경우
149+ return ResponseEntity .badRequest ().body (Map .of ("error" , e .getMessage ()));
150+ } catch (Exception e ) {
151+ log .error ("게시글 삭제 오류" , e );
152+ return ResponseEntity .internalServerError ().body (Map .of ("error" , "서버 오류가 발생했습니다." ));
153+ }
154+ }
155+ }
0 commit comments