Skip to content

Commit

Permalink
Fix/poke logic structure (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
yummygyudon authored Dec 19, 2023
2 parents b587a6a + 88fecb8 commit a506246
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 135 deletions.
21 changes: 9 additions & 12 deletions src/main/java/org/sopt/app/application/poke/FriendService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ public List<Long> findAllFriendIdsByUserIdRandomly(Long userId, int limitNum) {
return friendRepository.getFriendRandom(userId, limitNum);
}

public List<Long> findAllFriendsByFriendship(Long userId, Integer lowerLimit, Integer upperLimit) {
public List<Friend> findAllFriendsByFriendship(Long userId, Integer lowerLimit, Integer upperLimit) {
return friendRepository.findAllByUserIdAndPokeCountBetweenOrderByPokeCountDesc(
userId, lowerLimit, upperLimit).stream()
.map(Friend::getFriendUserId)
.toList();
userId, lowerLimit, upperLimit);
}
public Page<Friend> findAllFriendsByFriendship(Long userId, Integer lowerLimit, Integer upperLimit, Pageable pageable) {
return friendRepository.findAllByUserIdAndPokeCountBetweenOrderByPokeCountDesc(
Expand All @@ -37,18 +35,17 @@ public Page<Friend> findAllFriendsByFriendship(Long userId, Integer lowerLimit,


public void createRelation(Long pokerId, Long pokedId) {
registerFriendshipOf(pokerId, pokedId);
registerFriendshipOf(pokedId, pokerId);
}

private void registerFriendshipOf(Long userId, Long friendId) {
Friend createdRelationUserToFriend = Friend.builder()
.userId(pokerId)
.friendUserId(pokedId)
.pokeCount(1)
.build();
Friend createdRelationFriendToUser = Friend.builder()
.userId(pokedId)
.friendUserId(pokerId)
.userId(userId)
.friendUserId(friendId)
.pokeCount(1)
.build();
friendRepository.save(createdRelationUserToFriend);
friendRepository.save(createdRelationFriendToUser);
}

public void applyPokeCount(Long pokerId, Long pokedId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public Boolean isNewPoker(Long userId) {
return pokeHistoryRepository.findAllByPokerId(userId).isEmpty();
}

public Boolean isUserPokeFriendBefore(Long userId, Long friendId) {
return pokeHistoryRepository.findAllByPokerIdAndPokedId(userId, friendId).isEmpty();
public List<PokeHistory> getAllOfPokeBetween(Long userId, Long friendId) {
return pokeHistoryRepository.findAllWithFriendOrderByCreatedAtDesc(userId, friendId);
}

public List<Long> getPokedFriendIds(Long userId) {
Expand All @@ -41,11 +41,11 @@ public List<Long> getPokeFriendIds(Long userId) {
.toList();
}

public List<PokeHistory> getPokeFriendIdsInOrderByMostRecent(Long userId) {
return pokeHistoryRepository.findAllByPokedId(userId);
public List<PokeHistory> getAllPokedHistoryOrderByMostRecent(Long userId) {
return pokeHistoryRepository.findAllByPokedIdAndIsReplyIsFalseOrderByCreatedAtDesc(userId);
}
public Page<PokeHistory> getPokeFriendIdsInOrderByMostRecent(Long userId, Pageable pageable) {
return pokeHistoryRepository.findAllByPokedId(userId, pageable);
public Page<PokeHistory> getAllPokedHistoryOrderByMostRecent(Long userId, Pageable pageable) {
return pokeHistoryRepository.findAllByPokedIdAndIsReplyIsFalseOrderByCreatedAtDesc(userId, pageable);
}

public void checkUserOverDailyPokeLimit(Long userId) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/sopt/app/application/poke/PokeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static class PokedUserInfo {
@ToString
public static class PokeDetail {
private final Long id;
private final Long pokerId;
private final Long pokedId;
private final String message;
private final Boolean isReply;

Expand Down
43 changes: 14 additions & 29 deletions src/main/java/org/sopt/app/application/poke/PokeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import org.sopt.app.common.event.Events;
import org.sopt.app.common.exception.NotFoundException;
import org.sopt.app.common.response.ErrorCode;
import org.sopt.app.domain.entity.Friend;
import org.sopt.app.domain.entity.PokeHistory;
import org.sopt.app.domain.entity.User;
import org.sopt.app.interfaces.postgres.FriendRepository;
import org.sopt.app.interfaces.postgres.PokeHistoryRepository;
import org.sopt.app.interfaces.postgres.UserRepository;
import org.springframework.stereotype.Service;
Expand All @@ -21,62 +19,49 @@
public class PokeService {

private final UserRepository userRepository;
private final FriendRepository friendRepository;
private final PokeHistoryRepository historyRepository;

@Transactional(readOnly = true)
public Boolean isReplyPoke(Long pokerId, Long pokedId) {
Optional<PokeHistory> latestPokeFromPokedIsReplyFalse = historyRepository.findByPokerIdAndPokedIdAndIsReplyIsFalse(pokedId, pokerId);
return latestPokeFromPokedIsReplyFalse.isPresent();
}

@Transactional(readOnly = true)
public PokeInfo.PokeDetail getPokeDetail(Long pokerId, Long pokedId) {
PokeHistory latestPokeHistory = historyRepository.findAllByPokerIdAndPokedIdOrderByCreatedAtDesc(pokerId, pokedId).stream()
.distinct()
.findFirst()
public PokeInfo.PokeDetail getPokeDetail(Long pokeHistoryId) {
PokeHistory latestPokeHistory = historyRepository.findById(pokeHistoryId)
.orElseThrow(() -> new NotFoundException(ErrorCode.POKE_HISTORY_NOT_FOUND.getMessage()));
return PokeInfo.PokeDetail.builder()
.id(latestPokeHistory.getId())
.pokerId(latestPokeHistory.getPokerId())
.pokedId(latestPokeHistory.getPokedId())
.message(latestPokeHistory.getMessage())
.isReply(latestPokeHistory.getIsReply())
.build();
}

@Transactional
public void poke(Long pokerUserId, Long pokedUserId, String pokeMessage) {
public PokeHistory poke(Long pokerUserId, Long pokedUserId, String pokeMessage) {
User pokedUser = userRepository.findUserById(pokedUserId)
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND.getMessage()));

createPokeByApplyingReply(pokerUserId, pokedUserId, pokeMessage);
PokeHistory pokeByApplyingReply = createPokeByApplyingReply(pokerUserId, pokedUserId, pokeMessage);

Events.raise(PokeEvent.of(pokedUser.getPlaygroundId()));
return pokeByApplyingReply;
}

private void createPokeByApplyingReply(Long pokerUserId, Long pokedUserId, String pokeMessage) {
private PokeHistory createPokeByApplyingReply(Long pokerUserId, Long pokedUserId, String pokeMessage) {
boolean currentPokeReply = false;
if (isReplyPoke(pokerUserId, pokedUserId)) {
Optional<PokeHistory> latestPokeFromPokedIsReplyFalse
= historyRepository.findByPokerIdAndPokedIdAndIsReplyIsFalse(pokedUserId, pokerUserId);
if (latestPokeFromPokedIsReplyFalse.isPresent()) {
currentPokeReply = true;
PokeHistory recentPokeFromPokedUser = historyRepository.findByPokerIdAndPokedIdAndIsReplyIsFalse(pokerUserId, pokedUserId).get();
recentPokeFromPokedUser.activateReply();
applyPokeCountBoth(pokerUserId, pokedUserId);
latestPokeFromPokedIsReplyFalse.get().activateReply();
}
PokeHistory createdPoke = PokeHistory.builder()
.pokerId(pokerUserId)
.pokedId(pokedUserId)
.message(pokeMessage)
.isReply(currentPokeReply)
.build();
historyRepository.save(createdPoke);
return historyRepository.save(createdPoke);
}

private void applyPokeCountBoth(Long pokerId, Long pokedId) {
Friend pokerToPoked = friendRepository.findByUserIdAndFriendUserId(pokerId, pokedId)
.orElseThrow(() -> new NotFoundException(ErrorCode.FRIENDSHIP_NOT_FOUND.getMessage()));
Friend pokedToPoker = friendRepository.findByUserIdAndFriendUserId(pokerId, pokedId)
.orElseThrow(() -> new NotFoundException(ErrorCode.FRIENDSHIP_NOT_FOUND.getMessage()));
pokerToPoked.addPokeCount();
pokedToPoker.addPokeCount();
}


}
Loading

0 comments on commit a506246

Please sign in to comment.