-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 리액션 API 추가 및 추천리스트 API, 리스트 상세조회 API 재구현 #304
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시
viewName
은 어떠신가요..? ㅎCategoryType에서 viewName을 사용하고 있는데 통일시키는게 좋을 것 같아서요 !!