11package com .debatetimer .domainrepository .poll ;
22
3+ import com .debatetimer .domain .poll .ParticipateCode ;
4+ import com .debatetimer .domain .poll .Vote ;
35import com .debatetimer .domain .poll .VoteInfo ;
46import com .debatetimer .domain .poll .VoteTeam ;
7+ import com .debatetimer .entity .poll .PollEntity ;
58import com .debatetimer .entity .poll .VoteEntity ;
9+ import com .debatetimer .exception .custom .DTClientErrorException ;
10+ import com .debatetimer .exception .decoder .RepositoryErrorDecoder ;
11+ import com .debatetimer .exception .errorcode .ClientErrorCode ;
12+ import com .debatetimer .repository .poll .PollRepository ;
613import com .debatetimer .repository .poll .VoteRepository ;
714import java .util .List ;
815import java .util .Map ;
916import java .util .stream .Collectors ;
1017import lombok .RequiredArgsConstructor ;
18+ import org .springframework .dao .DataIntegrityViolationException ;
1119import org .springframework .stereotype .Repository ;
1220
1321@ Repository
1422@ RequiredArgsConstructor
1523public class VoteDomainRepository {
1624
25+ private final PollRepository pollRepository ;
1726 private final VoteRepository voteRepository ;
27+ private final RepositoryErrorDecoder errorDecoder ;
1828
1929 public VoteInfo findVoteInfoByPollId (long pollId ) {
2030 List <VoteEntity > pollVotes = voteRepository .findAllByPollId (pollId );
@@ -28,4 +38,22 @@ private VoteInfo countVotes(long pollId, List<VoteEntity> voteEntities) {
2838 long consCount = teamCount .getOrDefault (VoteTeam .CONS , 0L );
2939 return new VoteInfo (pollId , prosCount , consCount );
3040 }
41+
42+ public boolean isExists (long pollId , ParticipateCode code ) {
43+ return voteRepository .existsByPollIdAndParticipateCode (pollId , code .getValue ());
44+ }
45+
46+ public Vote save (Vote vote ) {
47+ try {
48+ PollEntity pollEntity = pollRepository .getById (vote .getPollId ());
49+ VoteEntity voteEntity = new VoteEntity (vote , pollEntity );
50+ return voteRepository .save (voteEntity )
51+ .toDomain ();
52+ } catch (DataIntegrityViolationException exception ) {
53+ if (errorDecoder .isUniqueConstraintViolation (exception )) {
54+ throw new DTClientErrorException (ClientErrorCode .ALREADY_VOTED_PARTICIPANT );
55+ }
56+ throw exception ;
57+ }
58+ }
3159}
0 commit comments