Skip to content

Commit

Permalink
refactor: 코드리뷰에 의한 네이밍 변경 및 리액션 패키지구조 별도 추출
Browse files Browse the repository at this point in the history
  • Loading branch information
pparkjs committed Oct 27, 2024
1 parent e1783ed commit 4b443a3
Show file tree
Hide file tree
Showing 19 changed files with 346 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.listywave.list.application.domain.item.Item;
import com.listywave.list.application.domain.label.Label;
import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.reaction.application.dto.response.ReactionResponse;
import com.listywave.user.application.domain.User;
import jakarta.annotation.Nullable;
import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@
import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.list.application.domain.list.ListTitle;
import com.listywave.list.application.domain.list.SortType;
import com.listywave.list.application.domain.reaction.Reaction;
import com.listywave.list.application.domain.reaction.ReactionStats;
import com.listywave.list.application.domain.reaction.UserReaction;
import com.listywave.list.application.dto.response.*;
import com.listywave.list.application.dto.response.ListCreateResponse;
import com.listywave.list.application.dto.response.ListDetailResponse;
import com.listywave.list.application.dto.response.ListRecentResponse;
import com.listywave.list.application.dto.response.ListSearchResponse;
import com.listywave.list.application.dto.response.RecommendedListResponse;
import com.listywave.list.presentation.dto.request.ItemCreateRequest;
import com.listywave.list.presentation.dto.request.ListCreateRequest;
import com.listywave.list.presentation.dto.request.ListUpdateRequest;
import com.listywave.list.repository.ItemRepository;
import com.listywave.list.repository.comment.CommentRepository;
import com.listywave.list.repository.label.LabelRepository;
import com.listywave.list.repository.list.ListRepository;
import com.listywave.list.repository.reaction.ReactionStatsRepository;
import com.listywave.list.repository.reaction.UserReactionRepository;
import com.listywave.list.repository.reply.ReplyRepository;
import com.listywave.reaction.application.dto.response.ReactionResponse;
import com.listywave.reaction.application.service.ReactionService;
import com.listywave.user.application.domain.Follow;
import com.listywave.user.application.domain.User;
import com.listywave.user.application.dto.FindFeedListResponse;
Expand All @@ -51,7 +52,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Pageable;
Expand All @@ -75,10 +75,9 @@ public class ListService {
private final LabelRepository labelRepository;
private final CollectionRepository collectionRepository;
private final AlarmRepository alarmRepository;
private final UserReactionRepository userReactionRepository;
private final ReactionStatsRepository reactionStatsRepository;
private final CollaboratorService collaboratorService;
private final HistoryService historyService;
private final ReactionService reactionService;
private final ApplicationEventPublisher applicationEventPublisher;

public ListCreateResponse listCreate(ListCreateRequest request, Long loginUserId) {
Expand Down Expand Up @@ -142,7 +141,7 @@ public ListDetailResponse getListDetail(Long listId, Long loginUserId) {
boolean isCollected = checkIsCollected(list, user);
boolean isOwner = checkIsOwner(list, user);

List<ReactionResponse> reactions = createReactionResponses(list, user, isOwner);
List<ReactionResponse> reactions = reactionService.createReactionResponses(list, user, isOwner);
long totalCommentCount = commentRepository.countCommentsByList(list);
Comment newestComment = commentRepository.findFirstByListOrderByCreatedDateDesc(list);
Long totalReplyCount = replyRepository.countByComment(newestComment);
Expand Down Expand Up @@ -329,31 +328,6 @@ public void changeVisibility(Long loginUserId, Long listId) {
list.updateVisibility();
}

public void handleReaction(Long loginUserId, Long listId, Reaction reaction) {
User user = userRepository.getById(loginUserId);
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);
}

private User getUserIfLoggedIn(Long loginUserId) {
return (loginUserId != null) ? userRepository.getById(loginUserId) : null;
}
Expand All @@ -365,38 +339,4 @@ private boolean checkIsCollected(ListEntity list, User user) {
private boolean checkIsOwner(ListEntity list, User user) {
return (user != null) && list.isOwner(user);
}

private 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.listywave.list.presentation.dto.request.ListCreateRequest;
import com.listywave.list.presentation.dto.request.ListUpdateRequest;
import com.listywave.list.presentation.dto.request.ListsDeleteRequest;
import com.listywave.list.presentation.dto.request.ReactionRequest;
import com.listywave.user.application.dto.FindFeedListResponse;
import java.time.LocalDateTime;
import java.util.List;
Expand Down Expand Up @@ -142,14 +141,4 @@ ResponseEntity<Void> changeVisibility(
listService.changeVisibility(loginUserId, listId);
return ResponseEntity.noContent().build();
}

@PostMapping("/lists/{listId}/reaction")
public ResponseEntity<String> handleReaction(
@Auth Long loginUserId,
@PathVariable("listId") Long listId,
@RequestBody ReactionRequest request
) {
listService.handleReaction(loginUserId, listId, request.reaction());
return ResponseEntity.noContent().build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import static com.listywave.list.application.domain.category.CategoryType.ENTIRE;
import static com.listywave.list.application.domain.item.QItem.item;
import static com.listywave.list.application.domain.list.QListEntity.listEntity;
import static com.listywave.list.application.domain.reaction.QReactionStats.reactionStats;
import static com.listywave.reaction.application.domain.QReactionStats.reactionStats;
import static com.listywave.user.application.domain.QUser.user;

import com.listywave.common.exception.CustomException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.listywave.list.application.domain.reaction;
package com.listywave.reaction.application.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -9,8 +9,7 @@ public enum Reaction {

COOL("멋져요"),
AGREE("공감해요"),
THANKS("감사해요")
;
THANKS("감사해요");

private final String name;
private final String displayName;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.listywave.list.application.domain.reaction;
package com.listywave.reaction.application.domain;

import static jakarta.persistence.FetchType.LAZY;
import static lombok.AccessLevel.PROTECTED;

import com.listywave.common.BaseEntity;
import com.listywave.list.application.domain.list.ListEntity;
import jakarta.persistence.*;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import static jakarta.persistence.FetchType.LAZY;
import static lombok.AccessLevel.PROTECTED;

@Getter
@Entity
@Builder
Expand All @@ -32,7 +39,7 @@ public class ReactionStats extends BaseEntity {
@Column(nullable = false)
private int count;

public void updateCount(int changeCount) {
public synchronized void updateCount(int changeCount) {
this.count += changeCount;
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
package com.listywave.list.application.domain.reaction;
package com.listywave.reaction.application.domain;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.TemporalType.TIMESTAMP;
import static lombok.AccessLevel.PROTECTED;

import com.listywave.list.application.domain.list.ListEntity;
import jakarta.persistence.*;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.Temporal;
import jakarta.persistence.UniqueConstraint;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.TemporalType.TIMESTAMP;
import static lombok.AccessLevel.PROTECTED;

@Getter
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = PROTECTED)
@EntityListeners(AuditingEntityListener.class)
@Table(name = "user_reaction",
uniqueConstraints= {
uniqueConstraints = {
@UniqueConstraint(
name = "UniqueIdAndUserIdAndReaction",
columnNames = {"id", "user_id", "reaction"}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.listywave.list.application.dto.response;
package com.listywave.reaction.application.dto.response;

import lombok.Builder;

Expand Down
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);
}
}
Loading

0 comments on commit 4b443a3

Please sign in to comment.