-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUserBoardController.java
More file actions
181 lines (166 loc) · 9.69 KB
/
Copy pathUserBoardController.java
File metadata and controls
181 lines (166 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.homeaid.controller;
import com.homeaid.common.response.CommonApiResponse;
import com.homeaid.domain.UserBoard;
import com.homeaid.domain.enumerate.UserRole;
import com.homeaid.dto.request.CreateBoardRequestDto;
import com.homeaid.dto.request.UpdateBoardRequestDto;
import com.homeaid.dto.response.PagedResponseDto;
import com.homeaid.dto.response.UserBoardListResponseDto;
import com.homeaid.dto.response.UserBoardResponseDto;
import com.homeaid.security.CustomUserDetails;
import com.homeaid.service.UserBoardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/boards")
@Tag(name = "1:1 문의글", description = "사용자와 관리자 간 1:1 문의글 API")
@SecurityRequirement(name = "Bearer Authentication")
public class UserBoardController {
private final UserBoardService userBoardService;
@PostMapping("")
@Operation(summary = "1:1 문의글 작성", description = "고객과 매니저의 권한을 구분하여 관리자에게 1:1 문의글을 작성합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "문의글 작성 성공",
content = @Content(schema = @Schema(implementation = UserBoardResponseDto.class))),
@ApiResponse(responseCode = "400", description = "유효하지 않은 요청",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class))),
@ApiResponse(responseCode = "401", description = "인증되지 않은 사용자",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class)))
})
public ResponseEntity<CommonApiResponse<UserBoardResponseDto>> createBoard(
@RequestBody @Valid CreateBoardRequestDto createRequestDto,
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {
Long userId = customUserDetails.getUserId();
UserRole role = customUserDetails.getUserRole();
UserBoard board = CreateBoardRequestDto.toEntity(userId, role, createRequestDto);
return ResponseEntity.status(HttpStatus.CREATED)
.body(CommonApiResponse.success(
UserBoardResponseDto.toDto(userBoardService.createBoard(board))));
}
@PutMapping("/{boardId}")
@Operation(summary = "1:1 문의글 수정", description = "사용자는 본인이 작성한 문의글만 수정할 수 있습니다. ")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "문의글 수정 성공",
content = @Content(schema = @Schema(implementation = UserBoardResponseDto.class))),
@ApiResponse(responseCode = "401", description = "인증되지 않은 사용자",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class))),
@ApiResponse(responseCode = "403", description = "수정 권한이 없음",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class))),
@ApiResponse(responseCode = "404", description = "해당 문의글 존재하지 않음",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class)))
})
public ResponseEntity<CommonApiResponse<UserBoardResponseDto>> updateBoard(
@Parameter(description = "수정할 문의글 ID", example = "1")
@PathVariable(name = "boardId") Long id,
@RequestBody @Valid UpdateBoardRequestDto updateBoardRequestDto,
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {
Long userId = customUserDetails.getUserId();
UserRole role = customUserDetails.getUserRole();
UserBoard board = userBoardService.updateBoard(id, userId, role,
UpdateBoardRequestDto.toEntity(updateBoardRequestDto));
return ResponseEntity.status(HttpStatus.OK).body(
CommonApiResponse.success((UserBoardResponseDto.toDto(board)))
);
}
@DeleteMapping("/{boardId}")
@Operation(summary = "문의글 삭제", description = "사용자는 본인이 작성한 문의글만 삭제할 수 있습니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "문의글 삭제 성공",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class))),
@ApiResponse(responseCode = "401", description = "인증되지 않은 사용자",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class))),
@ApiResponse(responseCode = "403", description = "수정 권한이 없음",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class))),
@ApiResponse(responseCode = "404", description = "해당 문의글 존재하지 않음",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class)))
})
public ResponseEntity<CommonApiResponse<Void>> deleteBoard(
@Parameter(description = "삭제할 문의글 ID", example = "1")
@PathVariable(name = "boardId") Long id,
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {
Long userId = customUserDetails.getUserId();
UserRole role = customUserDetails.getUserRole();
userBoardService.deleteBoard(id, userId, role);
return ResponseEntity.ok(CommonApiResponse.success(null));
}
@GetMapping("/{boardId}")
@Operation(summary = "문의글 단건 조회", description = "사용자는 본인이 작성한 문의글만 조회할 수 있습니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "문의글 단건 조회 성공",
content = @Content(schema = @Schema(implementation = UserBoardResponseDto.class))),
@ApiResponse(responseCode = "401", description = "인증되지 않은 사용자",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class))),
@ApiResponse(responseCode = "403", description = "조회 권한이 없음",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class))),
@ApiResponse(responseCode = "404", description = "해당 문의글 존재하지 않음",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class)))
})
public ResponseEntity<CommonApiResponse<UserBoardResponseDto>> getBoard(
@Parameter(description = "조회할 문의글 ID", example = "1")
@PathVariable(name = "boardId") Long id,
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {
Long userId = customUserDetails.getUserId();
UserRole role = customUserDetails.getUserRole();
return ResponseEntity.ok(
CommonApiResponse.success(
UserBoardResponseDto.toDto(userBoardService.getBoard(id, userId, role))));
}
@GetMapping("")
@Operation(summary = "문의글 전체 조회 및 검색", description = "사용자는 본인이 작성한 모든 문의글을 조회합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "문의글 목록 조회 성공",
content = @Content(schema = @Schema(implementation = UserBoardListResponseDto.class))),
@ApiResponse(responseCode = "401", description = "인증되지 않은 사용자",
content = @Content(schema = @Schema(implementation = CommonApiResponse.class)))
})
public ResponseEntity<CommonApiResponse<PagedResponseDto<UserBoardListResponseDto>>> searchBoard(
@Parameter(description = "검색 키워드")
@RequestParam(name = "keyword", required = false) String keyword,
@Parameter(description = "페이지 번호 (0부터 시작)", example = "0")
@RequestParam(name = "page", defaultValue = "0") int page,
@Parameter(description = "페이지 크기", example = "10")
@RequestParam(name = "size", defaultValue = "10") int size,
@Parameter(description = "정렬 기준 필드", example = "createdAt")
@RequestParam(name = "sortBy", defaultValue = "createdAt") String sortBy,
@Parameter(description = "정렬 방향 (asc, desc)", example = "desc")
@RequestParam(name = "sortDirection", defaultValue = "desc") String sortDirection,
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {
Long userId = customUserDetails.getUserId();
UserRole role = customUserDetails.getUserRole();
Sort sort = sortDirection.equalsIgnoreCase("desc") ?
Sort.by(sortBy).descending() : Sort.by(sortBy).ascending();
Pageable pageable = PageRequest.of(page, size, sort);
return ResponseEntity.ok(
CommonApiResponse.success(userBoardService.searchBoard(keyword, pageable, userId, role)));
}
}