|
1 | 1 | package com.ai.lawyer.domain.poll.controller; |
2 | 2 |
|
| 3 | +import com.ai.lawyer.domain.poll.dto.PollCreateDto; |
| 4 | +import com.ai.lawyer.domain.poll.dto.PollDto; |
| 5 | +import com.ai.lawyer.domain.poll.dto.PollVoteDto; |
| 6 | +import com.ai.lawyer.domain.poll.entity.PollVote; |
| 7 | +import com.ai.lawyer.domain.poll.entity.PollOptions; |
| 8 | +import com.ai.lawyer.domain.poll.entity.PollStatics; |
| 9 | +import com.ai.lawyer.domain.poll.service.PollService; |
| 10 | +import com.ai.lawyer.domain.post.dto.PostDetailDto; |
| 11 | +import com.ai.lawyer.domain.post.service.PostService; |
| 12 | +import com.ai.lawyer.global.response.ApiResponse; |
| 13 | +import io.swagger.v3.oas.annotations.Operation; |
| 14 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 15 | +import lombok.RequiredArgsConstructor; |
| 16 | +import org.springframework.http.ResponseEntity; |
| 17 | +import org.springframework.security.core.Authentication; |
| 18 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 19 | +import org.springframework.web.bind.annotation.*; |
| 20 | + |
| 21 | +import org.springframework.web.server.ResponseStatusException; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +@Tag(name = "Poll API", description = "투표 관련 API") |
| 26 | +@RestController |
| 27 | +@RequestMapping("/api/polls") |
| 28 | +@RequiredArgsConstructor |
3 | 29 | public class PollController { |
| 30 | + |
| 31 | + private final PollService pollService; |
| 32 | + private final PostService postService; |
| 33 | + |
| 34 | + @Operation(summary = "투표 단일 조회") |
| 35 | + @GetMapping("/{pollId}") |
| 36 | + public ResponseEntity<ApiResponse<PollDto>> getPoll(@PathVariable Long pollId) { |
| 37 | + PollDto poll = pollService.getPoll(pollId); |
| 38 | + return ResponseEntity.ok(new ApiResponse<>(200, "투표 단일 조회 성공", poll)); |
| 39 | + } |
| 40 | + |
| 41 | + @Operation(summary = "투표 옵션 목록 조회") |
| 42 | + @GetMapping("/{pollId}/options") |
| 43 | + public ResponseEntity<ApiResponse<List<PollOptions>>> getPollOptions(@PathVariable Long pollId) { |
| 44 | + List<PollOptions> options = pollService.getPollOptions(pollId); |
| 45 | + return ResponseEntity.ok(new ApiResponse<>(200, "투표 옵션 목록 조회 성공", options)); |
| 46 | + } |
| 47 | + |
| 48 | + @Operation(summary = "투표하기") |
| 49 | + @PostMapping("/{pollId}/vote") |
| 50 | + public ResponseEntity<ApiResponse<PollVoteDto>> vote(@PathVariable Long pollId, @RequestParam Long pollItemsId) { |
| 51 | + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 52 | + Long memberId = Long.parseLong(authentication.getName()); |
| 53 | + PollVoteDto result = pollService.vote(pollId, pollItemsId, memberId); |
| 54 | + return ResponseEntity.ok(new ApiResponse<>(200, "투표가 성공적으로 완료되었습니다.", result)); |
| 55 | + } |
| 56 | + |
| 57 | + @Operation(summary = "투표 통계 조회") |
| 58 | + @GetMapping("/{pollId}/statics") |
| 59 | + public ResponseEntity<ApiResponse<List<PollStatics>>> getPollStatics(@PathVariable Long pollId) { |
| 60 | + List<PollStatics> statics = pollService.getPollStatics(pollId); |
| 61 | + return ResponseEntity.ok(new ApiResponse<>(200, "투표 통계 조회 성공", statics)); |
| 62 | + } |
| 63 | + |
| 64 | + @Operation(summary = "투표 종료") |
| 65 | + @PutMapping("/{pollId}/close") |
| 66 | + public ResponseEntity<ApiResponse<Void>> closePoll(@PathVariable Long pollId) { |
| 67 | + pollService.closePoll(pollId); |
| 68 | + return ResponseEntity.ok(new ApiResponse<>(200, "투표가 종료되었습니다.", null)); |
| 69 | + } |
| 70 | + |
| 71 | + @Operation(summary = "투표 삭제") |
| 72 | + @DeleteMapping("/{pollId}") |
| 73 | + public ResponseEntity<ApiResponse<Void>> deletePoll(@PathVariable Long pollId) { |
| 74 | + pollService.deletePoll(pollId); |
| 75 | + return ResponseEntity.ok(new ApiResponse<>(200, "투표가 삭제되었습니다.", null)); |
| 76 | + } |
| 77 | + |
| 78 | + @Operation(summary = "진행중인 투표 Top 1 조회") |
| 79 | + @GetMapping("/top/ongoing") |
| 80 | + public ResponseEntity<ApiResponse<PollDto>> getTopOngoingPoll() { |
| 81 | + PollDto poll = pollService.getTopPollByStatus(PollDto.PollStatus.ONGOING); |
| 82 | + return ResponseEntity.ok(new ApiResponse<>(200, "진행중인 투표 Top 1 조회 성공", poll)); |
| 83 | + } |
| 84 | + |
| 85 | + @Operation(summary = "종료된 투표 Top 1 조회") |
| 86 | + @GetMapping("/top/closed") |
| 87 | + public ResponseEntity<ApiResponse<PollDto>> getTopClosedPoll() { |
| 88 | + PollDto poll = pollService.getTopPollByStatus(PollDto.PollStatus.CLOSED); |
| 89 | + return ResponseEntity.ok(new ApiResponse<>(200, "종료된 투표 Top 1 조회 성공", poll)); |
| 90 | + } |
| 91 | + |
| 92 | +// @Operation(summary = "진행중인 투표 상세 조회") |
| 93 | +// @GetMapping("/top/ongoing-detail") |
| 94 | +// public PostDetailDto getTopOngoingPollDetail() { |
| 95 | +// PollDto pollDto = pollService.getTopPollByStatus(PollDto.PollStatus.ONGOING); |
| 96 | +// return postService.getPostDetailById(pollDto.getPostId()); |
| 97 | +// } |
| 98 | +// |
| 99 | +// @Operation(summary = "종료된 투표 상세 조회") |
| 100 | +// @GetMapping("/top/closed-detail") |
| 101 | +// public PostDetailDto getTopClosedPollDetail() { |
| 102 | +// PollDto pollDto = pollService.getTopPollByStatus(PollDto.PollStatus.CLOSED); |
| 103 | +// return postService.getPostDetailById(pollDto.getPostId()); |
| 104 | +// } |
| 105 | + |
| 106 | + @Operation(summary = "투표 생성") |
| 107 | + @PostMapping("") |
| 108 | + public ResponseEntity<ApiResponse<PollDto>> createPoll(@RequestBody PollCreateDto pollCreateDto) { |
| 109 | + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 110 | + Long memberId = Long.parseLong(authentication.getName()); |
| 111 | + PollDto created = pollService.createPoll(pollCreateDto, memberId); |
| 112 | + return ResponseEntity.ok(new ApiResponse<>(201, "투표가 생성되었습니다.", created)); |
| 113 | + } |
| 114 | + |
| 115 | + @Operation(summary = "투표 수정") |
| 116 | + @PutMapping("/{pollId}") |
| 117 | + public ResponseEntity<ApiResponse<PollDto>> updatePoll(@PathVariable Long pollId, @RequestBody com.ai.lawyer.domain.poll.dto.PollUpdateDto pollUpdateDto) { |
| 118 | + PollDto updated = pollService.updatePoll(pollId, pollUpdateDto); |
| 119 | + return ResponseEntity.ok(new ApiResponse<>(200, "투표가 수정되었습니다.", updated)); |
| 120 | + } |
| 121 | + |
| 122 | + @Operation(summary = "진행중인 투표 전체 목록 조회") |
| 123 | + @GetMapping("/ongoing") |
| 124 | + public ResponseEntity<ApiResponse<List<PollDto>>> getOngoingPolls() { |
| 125 | + List<PollDto> polls = pollService.getPollsByStatus(PollDto.PollStatus.ONGOING); |
| 126 | + return ResponseEntity.ok(new ApiResponse<>(200, "진행중인 투표 전체 목록 조회 성공", polls)); |
| 127 | + } |
| 128 | + |
| 129 | + @Operation(summary = "종료된 투표 전체 목록 조회") |
| 130 | + @GetMapping("/closed") |
| 131 | + public ResponseEntity<ApiResponse<List<PollDto>>> getClosedPolls() { |
| 132 | + List<PollDto> polls = pollService.getPollsByStatus(PollDto.PollStatus.CLOSED); |
| 133 | + return ResponseEntity.ok(new ApiResponse<>(200, "종료된 투표 전체 목록 조회 성공", polls)); |
| 134 | + } |
| 135 | + |
| 136 | + @Operation(summary = "종료된 투표 Top N 조회") |
| 137 | + @GetMapping("/top/closed-list") //검색조건 : pi/polls/top/closed-list?size=3 |
| 138 | + public ResponseEntity<ApiResponse<List<PollDto>>> getTopClosedPolls(@RequestParam(defaultValue = "3") int size) { |
| 139 | + List<PollDto> polls = pollService.getTopNPollsByStatus(PollDto.PollStatus.CLOSED, size); |
| 140 | + String message = String.format("종료된 투표 Top %d 조회 성공", size); |
| 141 | + return ResponseEntity.ok(new ApiResponse<>(200, message, polls)); |
| 142 | + } |
| 143 | + |
| 144 | + @Operation(summary = "진행중인 투표 Top N 조회") |
| 145 | + @GetMapping("/top/ongoing-list") //검색조건 : api/polls/top/ongoing-list?size=3 |
| 146 | + public ResponseEntity<ApiResponse<List<PollDto>>> getTopOngoingPolls(@RequestParam(defaultValue = "3") int size) { |
| 147 | + List<PollDto> polls = pollService.getTopNPollsByStatus(PollDto.PollStatus.ONGOING, size); |
| 148 | + String message = String.format("진행중인 투표 Top %d 조회 성공", size); |
| 149 | + return ResponseEntity.ok(new ApiResponse<>(200, message, polls)); |
| 150 | + } |
| 151 | + |
| 152 | + @Operation(summary = "index(순번)로 투표하기 - Swagger 편의용") |
| 153 | + @PostMapping("/{pollId}/vote-by-index") |
| 154 | + public ResponseEntity<ApiResponse<PollVoteDto>> voteByIndex(@PathVariable Long pollId, @RequestParam int index) { |
| 155 | + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 156 | + Long memberId = Long.parseLong(authentication.getName()); |
| 157 | + List<PollOptions> options = pollService.getPollOptions(pollId); |
| 158 | + if (index < 1 || index > options.size()) { |
| 159 | + throw new ResponseStatusException(org.springframework.http.HttpStatus.BAD_REQUEST, "index가 옵션 범위를 벗어났습니다."); |
| 160 | + } |
| 161 | + Long pollItemsId = options.get(index - 1).getPollItemsId(); |
| 162 | + PollVoteDto result = pollService.vote(pollId, pollItemsId, memberId); |
| 163 | + return ResponseEntity.ok(new ApiResponse<>(200, "투표가 성공적으로 완료되었습니다.", result)); |
| 164 | + } |
| 165 | + |
| 166 | + @ExceptionHandler(ResponseStatusException.class) |
| 167 | + public ResponseEntity<ApiResponse<Void>> handleResponseStatusException(ResponseStatusException ex) { |
| 168 | + int code = ex.getStatusCode().value(); |
| 169 | + String message = ex.getReason(); |
| 170 | + return ResponseEntity.status(code).body(new ApiResponse<>(code, message, null)); |
| 171 | + } |
4 | 172 | } |
0 commit comments