-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 코드리뷰에 의한 네이밍 변경 및 리액션 패키지구조 별도 추출
- Loading branch information
Showing
19 changed files
with
346 additions
and
246 deletions.
There are no files selected for viewing
This file contains 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 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 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
8 changes: 0 additions & 8 deletions
8
src/main/java/com/listywave/list/presentation/dto/request/ReactionRequest.java
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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
29 changes: 20 additions & 9 deletions
29
...ication/domain/reaction/UserReaction.java → ...tion/application/domain/UserReaction.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...cation/dto/response/ReactionResponse.java → ...cation/dto/response/ReactionResponse.java
This file contains 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
92 changes: 92 additions & 0 deletions
92
src/main/java/com/listywave/reaction/application/service/ReactionService.java
This file contains 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,92 @@ | ||
package com.listywave.reaction.application.service; | ||
|
||
import com.listywave.list.application.domain.list.ListEntity; | ||
import com.listywave.list.repository.list.ListRepository; | ||
import com.listywave.reaction.application.domain.Reaction; | ||
import com.listywave.reaction.application.domain.ReactionStats; | ||
import com.listywave.reaction.application.domain.UserReaction; | ||
import com.listywave.reaction.application.dto.response.ReactionResponse; | ||
import com.listywave.reaction.repository.ReactionStatsRepository; | ||
import com.listywave.reaction.repository.UserReactionRepository; | ||
import com.listywave.user.application.domain.User; | ||
import com.listywave.user.application.service.UserService; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class ReactionService { | ||
|
||
private final UserReactionRepository userReactionRepository; | ||
private final ReactionStatsRepository reactionStatsRepository; | ||
private final ListRepository listRepository; | ||
private final UserService userService; | ||
|
||
public void react(Long userId, Long listId, Reaction reaction) { | ||
User user = userService.getById(userId); | ||
ListEntity list = listRepository.getById(listId); | ||
|
||
if (userReactionRepository.existsByUserIdAndListAndReaction(user.getId(), list, reaction)) { | ||
userReactionRepository.deleteByUserIdAndListAndReaction(user.getId(), list, reaction); | ||
updateReactionStats(list, reaction, -1); | ||
} else { | ||
//TODO: 리액션할 때 알림 필요 | ||
UserReaction newReaction = UserReaction.create(user.getId(), list, reaction); | ||
userReactionRepository.save(newReaction); | ||
updateReactionStats(list, reaction, 1); | ||
} | ||
} | ||
|
||
public void updateReactionStats(ListEntity list, Reaction reaction, int changeCount) { | ||
ReactionStats stats = reactionStatsRepository.findByListAndReaction(list, reaction) | ||
.orElseGet(() -> { | ||
ReactionStats newStats = new ReactionStats(list, reaction, 0); | ||
reactionStatsRepository.save(newStats); | ||
return newStats; | ||
}); | ||
stats.updateCount(changeCount); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<ReactionResponse> createReactionResponses(ListEntity list, User user, boolean isOwner) { | ||
Map<Reaction, Integer> reactionStatsMap = toReactionStatsMap(list); | ||
Set<Reaction> userReactionSet = toUserReactionSet(list, user); | ||
|
||
return Arrays.stream(Reaction.values()) | ||
.map(reaction -> toReactionResponse(reaction, reactionStatsMap, userReactionSet, isOwner)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private Map<Reaction, Integer> toReactionStatsMap(ListEntity list) { | ||
return reactionStatsRepository.findByList(list).stream() | ||
.collect(Collectors.toMap(ReactionStats::getReaction, ReactionStats::getCount)); | ||
} | ||
|
||
private Set<Reaction> toUserReactionSet(ListEntity list, User user) { | ||
if (user == null) { | ||
return Collections.emptySet(); | ||
} | ||
return userReactionRepository.findByUserIdAndList(user.getId(), list).stream() | ||
.map(UserReaction::getReaction) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
private ReactionResponse toReactionResponse( | ||
Reaction reaction, | ||
Map<Reaction, Integer> reactionStatsMap, | ||
Set<Reaction> userReactionSet, | ||
boolean isOwner | ||
) { | ||
int count = reactionStatsMap.getOrDefault(reaction, 0); | ||
boolean isReacted = userReactionSet.contains(reaction); | ||
return ReactionResponse.of(reaction.name(), isOwner ? count : null, isReacted); | ||
} | ||
} |
Oops, something went wrong.