Skip to content

Commit

Permalink
feat(vote-paper) get
Browse files Browse the repository at this point in the history
fix: ResponseBody Payload에 is_voted(투표 여부) 추가
  • Loading branch information
habinkim committed May 25, 2024
1 parent de76c93 commit a7b486c
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ResponseEntity<Response<ScrollVotePaperResponse>> getVotePapers(@RequestP
@GetMapping(value = Uris.VOTE_PAPER_ROOT + "/{votePaperId}")
public ResponseEntity<Response<FullVotePaper>> getVotePaper(@PathVariable("votePaperId") @NotNull(message = "{vote.paper.id.not_null}") final Long votePaperId,
@CurrentUser final User user) {
FullVotePaper votePaper = getVotePaperUseCase.getVotePaper(votePaperId);
FullVotePaper votePaper = getVotePaperUseCase.getVotePaper(votePaperId, user);
return responseMapper.ok(MessageCode.SUCCESS, votePaper);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public List<Long> loadVoterIdsByVotePaperUuid(final String uuid) {
}

@Override
public Optional<RegisteredVote> loadVoteByVoterAndVotePaperId(String voter, Long voteChoiceId) {
return voteRepository.findByVoterUsernameAndVotePaperId(voter, voteChoiceId);
public Optional<RegisteredVote> loadVoteByVoterAndVotePaperId(final String voter, final Long votePaperId) {
return voteRepository.findByVoterUsernameAndVotePaperId(voter, votePaperId);
}

@Override
public void saveVote(Long voteChoiceId, String username) {
public void saveVote(final Long voteChoiceId, final String username) {
VoteChoiceJpaEntity voteChoice = voteChoiceRepository.findById(voteChoiceId)
.orElseThrow(() -> new IllegalArgumentException("VoteChoice not found"));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.tune_fun.v1.vote.application.port.input.usecase;

import com.tune_fun.v1.vote.domain.value.FullVotePaper;
import org.springframework.security.core.userdetails.User;

@FunctionalInterface
public interface GetVotePaperUseCase {

FullVotePaper getVotePaper(final Long votePaperId);
FullVotePaper getVotePaper(final Long votePaperId, final User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
public interface LoadVotePort {
List<Long> loadVoterIdsByVotePaperUuid(final String uuid);

Optional<RegisteredVote> loadVoteByVoterAndVotePaperId(final String voter, final Long voteChoiceId);
Optional<RegisteredVote> loadVoteByVoterAndVotePaperId(final String voter, final Long votePaperId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import com.tune_fun.v1.vote.application.port.input.usecase.GetVotePaperUseCase;
import com.tune_fun.v1.vote.application.port.output.LoadVoteChoicePort;
import com.tune_fun.v1.vote.application.port.output.LoadVotePaperPort;
import com.tune_fun.v1.vote.application.port.output.LoadVotePort;
import com.tune_fun.v1.vote.domain.value.FullVotePaper;
import com.tune_fun.v1.vote.domain.value.RegisteredVote;
import com.tune_fun.v1.vote.domain.value.RegisteredVoteChoice;
import com.tune_fun.v1.vote.domain.value.RegisteredVotePaper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

import static com.tune_fun.v1.common.response.MessageCode.VOTE_PAPER_NOT_FOUND;

Expand All @@ -22,15 +26,17 @@ public class GetVotePaperService implements GetVotePaperUseCase {

private final LoadVotePaperPort loadVotePaperPort;
private final LoadVoteChoicePort loadVoteChoicePort;
private final LoadVotePort loadVotePort;

private final VoteValueMapper voteValueMapper;

@Override
public FullVotePaper getVotePaper(final Long votePaperId) {
public FullVotePaper getVotePaper(final Long votePaperId, final User user) {
RegisteredVotePaper registeredVotePaper = loadVotePaperPort.loadRegisteredVotePaper(votePaperId)
.orElseThrow(() -> new CommonApplicationException(VOTE_PAPER_NOT_FOUND));
List<RegisteredVoteChoice> registeredVoteChoices = loadVoteChoicePort.loadRegisteredVoteChoice(votePaperId);
Optional<RegisteredVote> registeredVote = loadVotePort.loadVoteByVoterAndVotePaperId(user.getUsername(), votePaperId);

return voteValueMapper.fullVotePaper(registeredVotePaper, registeredVoteChoices);
return voteValueMapper.fullVotePaper(registeredVotePaper, registeredVoteChoices, registeredVote.isPresent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RegisterVoteService implements RegisterVoteUseCase {
private final SaveVotePort saveVotePort;

@Override
public void register(Long votePaperId, final Long voteChoiceId, final User user) {
public void register(final Long votePaperId, final Long voteChoiceId, final User user) {
if (loadVotePort.loadVoteByVoterAndVotePaperId(user.getUsername(), votePaperId).isPresent())
throw new CommonApplicationException(VOTE_POLICY_ONE_VOTE_PER_USER);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ public abstract class VoteValueMapper {
@Mapping(target = "content", source = "votePaper.content")
@Mapping(target = "option", source = "votePaper.option")
@Mapping(target = "videoUrl", source = "votePaper.videoUrl")
@Mapping(target = "isVoted", source = "isVoted")
@Mapping(target = "voteStartAt", source = "votePaper.voteStartAt")
@Mapping(target = "voteEndAt", source = "votePaper.voteEndAt")
@Mapping(target = "deliveryAt", source = "votePaper.deliveryAt")
@Mapping(target = "createdAt", source = "votePaper.createdAt")
@Mapping(target = "updatedAt", source = "votePaper.updatedAt")
@Mapping(target = "choices", source = "voteChoices")
public abstract FullVotePaper fullVotePaper(final RegisteredVotePaper votePaper, final List<RegisteredVoteChoice> voteChoices);
public abstract FullVotePaper fullVotePaper(final RegisteredVotePaper votePaper, final List<RegisteredVoteChoice> voteChoices, Boolean isVoted);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public record FullVotePaper(
VotePaperOption option,
String videoUrl,

Boolean isVoted,

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime voteStartAt,

Expand All @@ -30,7 +32,7 @@ public record FullVotePaper(

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime updatedAt,

Set<RegisteredVoteChoice> choices
) implements BasePayload {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ void getVotePapersSuccess() throws Exception {
@Order(2)
@DisplayName("투표 게시물 상세 조회, 성공")
void getVotePaperSuccess() throws Exception {
dummyService.initAndLogin();
dummyService.initArtistAndLogin();

dummyService.initVotePaper();

dummyService.initAndLogin();
dummyService.registerVote();

Long votePaperId = dummyService.getDefaultVotePaper().getId();
Expand All @@ -192,6 +192,7 @@ void getVotePaperSuccess() throws Exception {
fieldWithPath("data.content").description("투표 게시물 내용").attributes(constraint("NOT NULL")),
fieldWithPath("data.option").description("투표 종류").attributes(constraint("NOT NULL, allow-add-choices || deny-add-choices")),
fieldWithPath("data.video_url").description("투표 게시물 영상 URL").attributes(constraint("NULL or NOT NULL")),
fieldWithPath("data.is_voted").description("투표 여부").attributes(constraint("NOT NULL")),
fieldWithPath("data.vote_start_at").description("투표 시작 시간").attributes(constraint("NOT NULL")),
fieldWithPath("data.vote_end_at").description("투표 종료 시간").attributes(constraint("NOT NULL")),
fieldWithPath("data.delivery_at").description("투표 게시물 영상 제공일").attributes(constraint("NULL or NOT NULL")),
Expand Down Expand Up @@ -219,6 +220,7 @@ void getVotePaperSuccess() throws Exception {
.andExpect(jsonPath("data.content", notNullValue()))
.andExpect(jsonPath("data.option", notNullValue()))
.andExpect(jsonPath("data.video_url", nullValue()))
.andExpectAll(jsonPath("data.is_voted", notNullValue()), jsonPath("data.is_voted", is(true)))
.andExpect(jsonPath("data.vote_start_at", notNullValue()))
.andExpect(jsonPath("data.vote_end_at", notNullValue()))
.andExpect(jsonPath("data.delivery_at", nullValue()))
Expand Down

0 comments on commit a7b486c

Please sign in to comment.