Skip to content

Commit

Permalink
YEL-214 [fix] 투표시 갑자기 탈퇴한 회원 투표는 반영되지 않도록 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonjeongs committed Feb 28, 2024
1 parent d8b09d0 commit a888fe5
Showing 1 changed file with 68 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
package com.yello.server.domain.vote.service;

import static com.yello.server.global.common.ErrorCode.DUPLICATE_VOTE_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.INVALID_VOTE_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.LACK_POINT_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.LACK_USER_EXCEPTION;
import static com.yello.server.global.common.factory.WeightedRandomFactory.randomPoint;
import static com.yello.server.global.common.util.ConstantUtil.KEYWORD_HINT_POINT;
import static com.yello.server.global.common.util.ConstantUtil.NAME_HINT_DEFAULT;
import static com.yello.server.global.common.util.ConstantUtil.NAME_HINT_POINT;
import static com.yello.server.global.common.util.ConstantUtil.NO_FRIEND_COUNT;
import static com.yello.server.global.common.util.ConstantUtil.RANDOM_COUNT;
import static com.yello.server.global.common.util.ConstantUtil.VOTE_COUNT;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_FEMALE;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_MALE;

import com.yello.server.domain.friend.dto.response.FriendShuffleResponse;
import com.yello.server.domain.friend.entity.Friend;
import com.yello.server.domain.friend.exception.FriendException;
Expand All @@ -33,17 +19,19 @@
import com.yello.server.domain.vote.exception.VoteForbiddenException;
import com.yello.server.domain.vote.exception.VoteNotFoundException;
import com.yello.server.domain.vote.repository.VoteRepository;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

import static com.yello.server.global.common.ErrorCode.*;
import static com.yello.server.global.common.factory.WeightedRandomFactory.randomPoint;
import static com.yello.server.global.common.util.ConstantUtil.*;

@Builder
@Component
@RequiredArgsConstructor
Expand All @@ -66,27 +54,32 @@ public List<Vote> createVotes(Long senderId, List<VoteAnswer> voteAnswers) {
final User sender = userRepository.getById(senderId);

IntStream.range(0, voteAnswers.size())
.forEach(index -> {
VoteAnswer currentVote = voteAnswers.get(index);
.filter(index -> {
User receiver = userRepository.getById(voteAnswers.get(index).friendId());
return Objects.isNull(receiver.getDeletedAt());
})
.forEach(index -> {
VoteAnswer currentVote = voteAnswers.get(index);

if (isDuplicatedVote(index, voteAnswers)) {
throw new VoteForbiddenException(DUPLICATE_VOTE_EXCEPTION);
}

if (isDuplicatedVote(index, voteAnswers)) {
throw new VoteForbiddenException(DUPLICATE_VOTE_EXCEPTION);
}
User receiver = userRepository.getById(currentVote.friendId());
Question question = questionRepository.getById(currentVote.questionId());

User receiver = userRepository.getById(currentVote.friendId());
Question question = questionRepository.getById(currentVote.questionId());

Vote newVote = Vote.createVote(
currentVote.keywordName(),
sender,
receiver,
question,
currentVote.colorIndex()
);
Vote newVote = Vote.createVote(
currentVote.keywordName(),
sender,
receiver,
question,
currentVote.colorIndex()
);

Vote savedVote = voteRepository.save(newVote);
votes.add(savedVote);
});
Vote savedVote = voteRepository.save(newVote);
votes.add(savedVote);
});

return votes;
}
Expand All @@ -97,15 +90,15 @@ public List<QuestionForVoteResponse> generateVoteQuestion(User user, List<Questi
Collections.shuffle(questionList);

return questionList.stream()
.map(question -> QuestionForVoteResponse.builder()
.friendList(getShuffledFriends(user))
.keywordList(getShuffledKeywords(question))
.question(QuestionVO.of(question))
.questionPoint(randomPoint())
.subscribe(user.getSubscribe().toString())
.build())
.limit(VOTE_COUNT)
.toList();
.map(question -> QuestionForVoteResponse.builder()
.friendList(getShuffledFriends(user))
.keywordList(getShuffledKeywords(question))
.question(QuestionVO.of(question))
.questionPoint(randomPoint())
.subscribe(user.getSubscribe().toString())
.build())
.limit(VOTE_COUNT)
.toList();
}

@Override
Expand Down Expand Up @@ -146,18 +139,18 @@ public KeywordCheckResponse useKeywordHint(User user, Vote vote) {
public void makeGreetingVote(User user) {
final User sender = userManager.getOfficialUser(user.getGender());
final Question greetingQuestion = questionRepository.findByQuestionContent(
null,
GREETING_NAME_FOOT,
null,
GREETING_KEYWORD_FOOT
null,
GREETING_NAME_FOOT,
null,
GREETING_KEYWORD_FOOT
).orElseGet(() ->
questionRepository.save(
Question.of(
null,
GREETING_NAME_FOOT,
null,
GREETING_KEYWORD_FOOT)
)
questionRepository.save(
Question.of(
null,
GREETING_NAME_FOOT,
null,
GREETING_KEYWORD_FOOT)
)
);

voteRepository.save(createFirstVote(sender, user, greetingQuestion));
Expand All @@ -177,19 +170,19 @@ public List<FriendShuffleResponse> getShuffledFriends(User user) {

if (friends.size() > NO_FRIEND_COUNT && friends.size() < RANDOM_COUNT) {
return friendList.stream()
.map(FriendShuffleResponse::of)
.toList();
.map(FriendShuffleResponse::of)
.toList();
}

return friendList.stream()
.map(FriendShuffleResponse::of)
.limit(RANDOM_COUNT)
.toList();
.map(FriendShuffleResponse::of)
.limit(RANDOM_COUNT)
.toList();
}

private boolean isDuplicatedVote(int index, List<VoteAnswer> voteAnswers) {
return index > 0 && voteAnswers.get(index - 1).questionId()
.equals(voteAnswers.get(index).questionId());
.equals(voteAnswers.get(index).questionId());
}

private List<String> getShuffledKeywords(Question question) {
Expand All @@ -198,24 +191,24 @@ private List<String> getShuffledKeywords(Question question) {
Collections.shuffle(keywordList);

return keywordList.stream()
.map(Keyword::getKeywordName)
.limit(RANDOM_COUNT)
.toList();
.map(Keyword::getKeywordName)
.limit(RANDOM_COUNT)
.toList();
}

private Vote createFirstVote(User sender, User receiver, Question question) {
final ThreadLocalRandom random = ThreadLocalRandom.current();
final String answer = "널 기다렸어";

return Vote.builder()
.answer(answer)
.nameHint(-3)
.isAnswerRevealed(true)
.isRead(false)
.sender(sender)
.receiver(receiver)
.question(question)
.colorIndex(random.nextInt(12) + 1)
.build();
.answer(answer)
.nameHint(-3)
.isAnswerRevealed(true)
.isRead(false)
.sender(sender)
.receiver(receiver)
.question(question)
.colorIndex(random.nextInt(12) + 1)
.build();
}
}

0 comments on commit a888fe5

Please sign in to comment.