-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/#2 투표 토글 및 투표 개수 조회 api 구현 #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pykido
wants to merge
6
commits into
develop
Choose a base branch
from
Feature/#2-투표_토글_및_투표_개수_조회_API_구현
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "Feature/#2-\uD22C\uD45C_\uD1A0\uAE00_\uBC0F_\uD22C\uD45C_\uAC1C\uC218_\uC870\uD68C_API_\uAD6C\uD604"
+1,092
−5
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
033264b
feat : 투표 토글 및 투표 개수 조회 API 구현 완료
pykido 8654878
feat : 투표 토글 및 투표 개수 조회 API 테스트 코드 구현 완료
pykido 59f306f
feat : 투표 토글 및 투표 개수 조회 API 테스트 코드 구현 완료
pykido 73c0cb5
refactor : 코파일럿 리뷰 반영 - 1
pykido aad916b
refactor : 지민님 & 성재님 리뷰 반영 완료
pykido 2153061
refactor : 테스트 코드 추가 및 Docs 동기화
pykido File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
src/main/java/com/opus/opus/docs/asciidoc/team-vote.adoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| ifndef::snippets[] | ||
| :snippets: ./build/generated-snippets | ||
| endif::[] | ||
|
|
||
| = TEAM VOTE API 문서 | ||
| :doctype: book | ||
| :icons: font | ||
| :source-highlighter: highlightjs | ||
| :toc: left | ||
| :toclevels: 3 | ||
| :sectnums: | ||
|
|
||
| == API 목록 | ||
|
|
||
| link:./opus.html[API 목록으로 돌아가기] | ||
|
|
||
| == `PUT`: 팀 투표 토글 | ||
|
|
||
| 해당 팀에 대해 투표 상태를 토글합니다. | ||
|
|
||
| * `isVoted: true` → 투표 등록 | ||
| * `isVoted: false` → 투표 취소 | ||
|
|
||
| NOTE: 대회 기간 중에는 투표만 보이고, 대회 기간이 아니라면 좋아요가 보입니다. | ||
|
|
||
| NOTE: 해당 대회의 최대 투표 개수 이상으로 투표를 등록할 수 없습니다. | ||
|
|
||
| .Path Parameters | ||
| include::{snippets}/toggle-team-vote/path-parameters.adoc[] | ||
|
|
||
| .HTTP Request Headers | ||
| include::{snippets}/toggle-team-vote/request-headers.adoc[] | ||
|
|
||
| .Request Fields | ||
| include::{snippets}/toggle-team-vote/request-fields.adoc[] | ||
|
|
||
| .Response Fields | ||
| include::{snippets}/toggle-team-vote/response-fields.adoc[] | ||
|
|
||
| === 시나리오 1: TeamVote 데이터가 없는 경우 | ||
|
|
||
| 특정 멤버가 특정 팀에 대해 투표 API를 처음 호출하면, TeamVote 테이블에 데이터가 새로 생성됩니다. | ||
|
|
||
| [cols="1,2,1"] | ||
| |=== | ||
| |Request isVoted |응답 메시지 |HTTP 상태 코드 | ||
|
|
||
| |true | ||
| |투표가 등록되었습니다. | ||
| |200 OK | ||
|
|
||
| |false | ||
| |아직 투표하지 않은 팀입니다. | ||
| |400 Bad Request | ||
| |=== | ||
|
|
||
| isVoted: true → 투표 등록 | ||
|
|
||
| include::{snippets}/toggle-team-vote/http-request.adoc[] | ||
|
|
||
| include::{snippets}/toggle-team-vote/http-response.adoc[] | ||
|
|
||
| === 시나리오 2: TeamVote 데이터가 있는 경우 | ||
|
|
||
| 이미 해당 팀에 대한 투표 기록이 있는 경우, 상태에 따라 토글됩니다. | ||
|
|
||
| [cols="1,1,2,1"] | ||
| |=== | ||
| |현재 isVoted |Request isVoted |응답 메시지 |HTTP 상태 코드 | ||
|
|
||
| |true | ||
| |true | ||
| |이미 투표한 팀입니다. | ||
| |400 Bad Request | ||
|
|
||
| |true | ||
| |false | ||
| |투표가 취소되었습니다. | ||
| |200 OK | ||
|
|
||
| |false | ||
| |true | ||
| |투표가 등록되었습니다. | ||
| |200 OK | ||
|
|
||
| |false | ||
| |false | ||
| |이미 투표를 취소한 팀입니다. | ||
| |400 Bad Request | ||
| |=== | ||
|
|
||
| 투표 취소 (isVoted: true → false) | ||
|
|
||
| include::{snippets}/cancel-team-vote/http-request.adoc[] | ||
|
|
||
| include::{snippets}/cancel-team-vote/http-response.adoc[] | ||
|
|
||
| === ⚠️ 실패 케이스 | ||
|
|
||
| .❌ Case 1: 존재하지 않는 팀 | ||
|
|
||
| [%collapsible] | ||
|
|
||
| ==== | ||
|
|
||
| include::{snippets}/toggle-team-vote-fail-not-found/http-request.adoc[] | ||
|
|
||
| include::{snippets}/toggle-team-vote-fail-not-found/http-response.adoc[] | ||
|
|
||
| ==== | ||
|
|
||
| .❌ Case 2: 이미 투표한 팀 | ||
|
|
||
| [%collapsible] | ||
|
|
||
| ==== | ||
|
|
||
| include::{snippets}/toggle-team-vote-fail-already-voted/http-request.adoc[] | ||
|
|
||
| include::{snippets}/toggle-team-vote-fail-already-voted/http-response.adoc[] | ||
|
|
||
| ==== | ||
|
|
||
| .❌ Case 3: 이미 투표 취소한 팀 | ||
|
|
||
| [%collapsible] | ||
|
|
||
| ==== | ||
|
|
||
| include::{snippets}/toggle-team-vote-fail-already-unvoted/http-request.adoc[] | ||
|
|
||
| include::{snippets}/toggle-team-vote-fail-already-unvoted/http-response.adoc[] | ||
|
|
||
| ==== | ||
|
|
||
| .❌ Case 4: 투표한 적 없는 팀에 취소 요청 | ||
|
|
||
| [%collapsible] | ||
|
|
||
| ==== | ||
|
|
||
| include::{snippets}/toggle-team-vote-fail-not-voted-yet/http-request.adoc[] | ||
|
|
||
| include::{snippets}/toggle-team-vote-fail-not-voted-yet/http-response.adoc[] | ||
|
|
||
| ==== | ||
|
|
||
| .❌ Case 5: 최대 투표 수 초과 | ||
|
|
||
| [%collapsible] | ||
|
|
||
| ==== | ||
|
|
||
| include::{snippets}/toggle-team-vote-fail-limit-exceeded/http-request.adoc[] | ||
|
|
||
| include::{snippets}/toggle-team-vote-fail-limit-exceeded/http-response.adoc[] | ||
|
|
||
| ==== | ||
|
|
||
| .❌ Case 6: 투표 기간이 아님 | ||
|
|
||
| [%collapsible] | ||
|
|
||
| ==== | ||
|
|
||
| include::{snippets}/toggle-team-vote-fail-not-vote-period/http-request.adoc[] | ||
|
|
||
| include::{snippets}/toggle-team-vote-fail-not-vote-period/http-response.adoc[] | ||
|
|
||
| ==== | ||
|
|
||
| == `GET`: 사용자 투표 개수 조회 | ||
|
|
||
| 현재 사용자가 특정 대회에서 남은 투표 가능 횟수와 최대 투표 허용 개수를 조회합니다. | ||
|
|
||
| .Query Parameters | ||
| include::{snippets}/get-member-vote-count/query-parameters.adoc[] | ||
|
|
||
| .HTTP Request Headers | ||
| include::{snippets}/get-member-vote-count/request-headers.adoc[] | ||
|
|
||
| .HTTP Request | ||
| include::{snippets}/get-member-vote-count/http-request.adoc[] | ||
|
|
||
| .HTTP Response | ||
| include::{snippets}/get-member-vote-count/http-response.adoc[] | ||
|
|
||
| .Response Fields | ||
| include::{snippets}/get-member-vote-count/response-fields.adoc[] | ||
|
|
||
| === ⚠️ 실패 케이스 | ||
|
|
||
| .❌ Case 1: 존재하지 않는 대회 | ||
|
|
||
| [%collapsible] | ||
|
|
||
| ==== | ||
|
|
||
| include::{snippets}/get-member-vote-count-fail-not-found/http-request.adoc[] | ||
|
|
||
| include::{snippets}/get-member-vote-count-fail-not-found/http-response.adoc[] | ||
|
|
||
| ==== | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/com/opus/opus/modules/team/api/TeamVoteController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.opus.opus.modules.team.api; | ||
|
|
||
| import com.opus.opus.global.security.annotation.LoginMember; | ||
| import com.opus.opus.modules.member.domain.Member; | ||
| import com.opus.opus.modules.team.application.TeamVoteCommandService; | ||
| import com.opus.opus.modules.team.application.dto.request.TeamVoteToggleRequest; | ||
| import com.opus.opus.modules.team.application.dto.response.MemberVoteCountResponse; | ||
| import com.opus.opus.modules.team.application.dto.response.TeamVoteToggleResponse; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.access.annotation.Secured; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| 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("/teams") | ||
| @Secured({"ROLE_회원", "ROLE_관리자"}) | ||
| public class TeamVoteController { | ||
|
|
||
| private final TeamVoteCommandService teamVoteCommandService; | ||
|
|
||
| @PutMapping("/{teamId}/votes") | ||
| public ResponseEntity<TeamVoteToggleResponse> toggleVote(@PathVariable Long teamId, | ||
| @RequestBody @Valid TeamVoteToggleRequest request, | ||
| @LoginMember Member member) { | ||
| TeamVoteToggleResponse response = teamVoteCommandService.toggleVote(member.getId(), teamId, request.isVoted()); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @GetMapping("/votes") | ||
| public ResponseEntity<MemberVoteCountResponse> getMemberVoteCount(@RequestParam Long contestId, | ||
| @LoginMember Member member) { | ||
| MemberVoteCountResponse response = teamVoteCommandService.getMemberVoteCount(member.getId(), contestId); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
| } |
110 changes: 110 additions & 0 deletions
110
src/main/java/com/opus/opus/modules/team/application/TeamVoteCommandService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package com.opus.opus.modules.team.application; | ||
|
|
||
| import static com.opus.opus.modules.team.exception.TeamVoteExceptionType.ALREADY_UNVOTED; | ||
| import static com.opus.opus.modules.team.exception.TeamVoteExceptionType.ALREADY_VOTED; | ||
| import static com.opus.opus.modules.team.exception.TeamVoteExceptionType.DUPLICATE_VOTE_REQUEST; | ||
| import static com.opus.opus.modules.team.exception.TeamVoteExceptionType.NOT_VOTED_YET; | ||
| import static com.opus.opus.modules.team.exception.TeamVoteExceptionType.VOTE_LIMIT_EXCEEDED; | ||
|
|
||
| import com.opus.opus.modules.contest.application.convenience.ContestConvenience; | ||
| import com.opus.opus.modules.contest.domain.Contest; | ||
| import com.opus.opus.modules.team.application.convenience.TeamConvenience; | ||
| import com.opus.opus.modules.team.application.dto.response.MemberVoteCountResponse; | ||
| import com.opus.opus.modules.team.application.dto.response.TeamVoteToggleResponse; | ||
| import com.opus.opus.modules.team.domain.Team; | ||
| import com.opus.opus.modules.team.domain.TeamVote; | ||
| import com.opus.opus.modules.team.domain.dao.TeamVoteRepository; | ||
| import com.opus.opus.modules.team.exception.TeamVoteException; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.dao.DataIntegrityViolationException; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional | ||
| public class TeamVoteCommandService { | ||
|
|
||
| private final TeamConvenience teamConvenience; | ||
| private final ContestConvenience contestConvenience; | ||
|
|
||
| private final TeamVoteRepository teamVoteRepository; | ||
|
|
||
| public TeamVoteToggleResponse toggleVote(Long memberId, Long teamId, Boolean isVoted) { | ||
| Team team = teamConvenience.getValidateExistTeam(teamId); | ||
| Contest contest = contestConvenience.getValidateExistContest(team.getContestId()); | ||
|
|
||
| contestConvenience.validateVotingPeriod(contest); | ||
|
|
||
| Optional<TeamVote> teamVoteOptional = teamVoteRepository.findByMemberIdAndTeam(memberId, team); | ||
|
|
||
| return teamVoteOptional.map(teamVote -> handleExistingVote(teamVote, isVoted, memberId, contest)) | ||
| .orElseGet(() -> handleFirstTimeVote(memberId, team, isVoted, contest)); | ||
pykido marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private TeamVoteToggleResponse handleFirstTimeVote(Long memberId, Team team, Boolean isVoted, Contest contest) { | ||
pykido marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!isVoted) { | ||
| throw new TeamVoteException(NOT_VOTED_YET); | ||
| } | ||
|
|
||
| long currentVoteCount = countCurrentMemberVotes(memberId, team.getContestId()); | ||
| int maxVotesLimit = contest.getMaxVotesLimit(); | ||
|
|
||
| validateVoteLimit(currentVoteCount, maxVotesLimit); | ||
| saveTeamVote(memberId, team, true); | ||
|
|
||
| return TeamVoteToggleResponse.of(team.getId(), true, "투표가 등록되었습니다.", currentVoteCount + 1, maxVotesLimit); | ||
| } | ||
|
|
||
| private TeamVoteToggleResponse handleExistingVote(final TeamVote teamVote, final Boolean isVoted, final Long memberId, final Contest contest) { | ||
| if (Objects.equals(teamVote.getIsVoted(), isVoted)) { | ||
| throw new TeamVoteException(isVoted ? ALREADY_VOTED : ALREADY_UNVOTED); | ||
| } | ||
|
|
||
| final long currentVoteCount = countCurrentMemberVotes(memberId, contest.getId()); | ||
| final int maxVotesLimit = contest.getMaxVotesLimit(); | ||
|
|
||
| if (isVoted) { | ||
| validateVoteLimit(currentVoteCount, maxVotesLimit); | ||
| } | ||
|
|
||
| final long updatedVoteCount = currentVoteCount + (isVoted ? 1 : -1); | ||
| teamVote.updateIsVoted(isVoted); | ||
|
|
||
| return TeamVoteToggleResponse.of(teamVote.getTeam().getId(), isVoted, isVoted ? "투표가 등록되었습니다." : "투표가 취소되었습니다.", updatedVoteCount, maxVotesLimit); | ||
| } | ||
|
|
||
| private long countCurrentMemberVotes(Long memberId, Long contestId) { | ||
| return teamVoteRepository.countMemberVotesInContest(memberId, contestId); | ||
| } | ||
|
|
||
| private void validateVoteLimit(long currentVoteCount, int maxVotesLimit) { | ||
| if (currentVoteCount >= maxVotesLimit) { | ||
| String message = String.format(VOTE_LIMIT_EXCEEDED.errorMessage(), maxVotesLimit); | ||
| throw new TeamVoteException(VOTE_LIMIT_EXCEEDED, message); | ||
| } | ||
| } | ||
|
|
||
| private void saveTeamVote(Long memberId, Team team, Boolean isVoted) { | ||
| try { | ||
| teamVoteRepository.save(TeamVote.builder() | ||
| .memberId(memberId) | ||
| .team(team) | ||
| .isVoted(isVoted) | ||
| .build()); | ||
| teamVoteRepository.flush(); | ||
| } catch (DataIntegrityViolationException e) { | ||
| throw new TeamVoteException(DUPLICATE_VOTE_REQUEST); | ||
| } | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public MemberVoteCountResponse getMemberVoteCount(Long memberId, Long contestId) { | ||
| Contest contest = contestConvenience.getValidateExistContest(contestId); | ||
| long currentVoteCount = teamVoteRepository.countMemberVotesInContest(memberId, contestId); | ||
| long remainingVotesCount = contest.getMaxVotesLimit() - currentVoteCount; | ||
| return new MemberVoteCountResponse(remainingVotesCount, (long) contest.getMaxVotesLimit()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.