From ffb6b39ec42c9137faa3cb4c67da5674be09945a Mon Sep 17 00:00:00 2001 From: woosung1223 Date: Thu, 3 Oct 2024 00:59:17 +0900 Subject: [PATCH] =?UTF-8?q?:bug:=20fix:=20=EA=B7=B8=EB=A3=B9=20=EC=9D=BC?= =?UTF-8?q?=EB=B6=80=20=EC=82=AD=EC=A0=9C=20=EC=8B=9C=20=EB=A1=A4=EB=A7=81?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=ED=8D=BC=EB=A5=BC=20=EC=86=8C=EC=9C=A0?= =?UTF-8?q?=ED=95=98=EA=B3=A0=20=EC=9E=88=EB=8B=A4=EB=A9=B4=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=EA=B0=80=20=EB=B0=9C=EC=83=9D=ED=95=98=EB=8D=98=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../toyouserver/application/UserService.java | 55 +++++--- .../persistance/GroupRepository.java | 1 + .../persistance/MemberRepository.java | 2 + .../application/UserServiceTest.java | 130 +++++++++++++++++- 4 files changed, 166 insertions(+), 22 deletions(-) diff --git a/src/main/java/slvtwn/khu/toyouserver/application/UserService.java b/src/main/java/slvtwn/khu/toyouserver/application/UserService.java index 811578c..21a29c8 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/UserService.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/UserService.java @@ -4,6 +4,7 @@ import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -76,9 +77,7 @@ private List findUsersWithSpecificGroup(User user, String search, public void updateUser(Long userId, UserUpdateRequest request) { User user = userRepository.findById(userId) .orElseThrow(() -> new ToyouException(ResponseType.USER_NOT_FOUND)); - changeUserGroupsIfChanged(request, user); - user.updateInfo(request.name(), request.birthday(), request.introduction(), request.imageUrl()); createBirthdayEvents(user); } @@ -87,37 +86,57 @@ private void changeUserGroupsIfChanged(UserUpdateRequest request, User user) { List requestGroups = findGroupsFromRequest(request); List userGroups = findGroupsFromUser(user); - if (!requestGroups.equals(userGroups)) { - replaceUserGroups(user, request.groups()); + if (isGroupUpdatedNeeded(requestGroups, userGroups)) { + List removedGroups = findRemoveGroups(requestGroups, userGroups); + List newGroups = findNewGroups(requestGroups, userGroups); + deleteRollingPaperAndMembers(user, removedGroups); + saveMembersWithNewGroups(user, newGroups); } } + private void deleteRollingPaperAndMembers(User user, List removedGroups) { + List members = memberRepository.findByUser(user); + + rollingPaperRepository.deleteAllByMemberIn(members); + memberRepository.deleteByUserAndGroupIn(user, removedGroups); + } + private List findGroupsFromRequest(UserUpdateRequest request) { - return request.groups().stream() - .map(each -> groupRepository.findById(each.id()) - .orElseThrow(() -> new ToyouException(ResponseType.BAD_REQUEST))) - .toList(); + List groupIds = request.groups().stream() + .map(GroupRequest::id) + .collect(Collectors.toList()); + + return groupRepository.findAllByIdIn(groupIds); } private List findGroupsFromUser(User user) { return memberRepository.findByUser(user).stream() .map(Member::getGroup) - .toList(); + .collect(Collectors.toList()); } - private void replaceUserGroups(User user, List groupRequests) { - List members = memberRepository.findByUser(user); + private boolean isGroupUpdatedNeeded(List requestGroups, List userGroups) { + if (requestGroups.size() > userGroups.size()) { + return true; + } + userGroups.removeAll(requestGroups); + return !userGroups.isEmpty(); + } - rollingPaperRepository.deleteAllByMemberIn(members); - memberRepository.deleteAll(members); + private List findRemoveGroups(List requestGroups, List userGroups) { + return userGroups.stream() + .filter(each -> !requestGroups.contains(each)) + .toList(); + } - saveMembersWithNewGroups(user, groupRequests); + private List findNewGroups(List requestGroups, List userGroups) { + return requestGroups.stream() + .filter(each -> !userGroups.contains(each)) + .toList(); } - private void saveMembersWithNewGroups(User user, List groupRequests) { - List newMembers = groupRequests.stream() - .map(each -> groupRepository.findById(each.id()) - .orElseThrow(() -> new ToyouException(ResponseType.BAD_REQUEST))) + private void saveMembersWithNewGroups(User user, List groups) { + List newMembers = groups.stream() .map(each -> new Member(user, each)) .map(memberRepository::save) .toList(); diff --git a/src/main/java/slvtwn/khu/toyouserver/persistance/GroupRepository.java b/src/main/java/slvtwn/khu/toyouserver/persistance/GroupRepository.java index 637a794..a97a747 100644 --- a/src/main/java/slvtwn/khu/toyouserver/persistance/GroupRepository.java +++ b/src/main/java/slvtwn/khu/toyouserver/persistance/GroupRepository.java @@ -7,4 +7,5 @@ public interface GroupRepository extends JpaRepository { List findTop30ByNameLike(String keyword); + List findAllByIdIn(List groupIds); } diff --git a/src/main/java/slvtwn/khu/toyouserver/persistance/MemberRepository.java b/src/main/java/slvtwn/khu/toyouserver/persistance/MemberRepository.java index 17656cd..9a429f0 100644 --- a/src/main/java/slvtwn/khu/toyouserver/persistance/MemberRepository.java +++ b/src/main/java/slvtwn/khu/toyouserver/persistance/MemberRepository.java @@ -18,4 +18,6 @@ public interface MemberRepository extends JpaRepository { List findByGroupAndUserNameLike(Group group, String pattern); Optional findByUserIdAndGroupId(Long userId, Long groupId); + + void deleteByUserAndGroupIn(User user, List group); } diff --git a/src/test/java/slvtwn/khu/toyouserver/application/UserServiceTest.java b/src/test/java/slvtwn/khu/toyouserver/application/UserServiceTest.java index 26aeecd..0bc0d84 100644 --- a/src/test/java/slvtwn/khu/toyouserver/application/UserServiceTest.java +++ b/src/test/java/slvtwn/khu/toyouserver/application/UserServiceTest.java @@ -6,6 +6,7 @@ import jakarta.persistence.PersistenceContext; import java.time.LocalDate; import java.util.List; +import org.assertj.core.api.SoftAssertions; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; @@ -14,6 +15,7 @@ import org.springframework.transaction.annotation.Transactional; import slvtwn.khu.toyouserver.domain.Group; import slvtwn.khu.toyouserver.domain.Member; +import slvtwn.khu.toyouserver.domain.RollingPaper; import slvtwn.khu.toyouserver.domain.User; import slvtwn.khu.toyouserver.dto.GroupRequest; import slvtwn.khu.toyouserver.dto.UserResponse; @@ -80,7 +82,7 @@ class UserServiceTest { } @Test - void 유저_정보를_업데이트_할_수_있다() { + void 유저_정보_업데이트_시_새로_그룹을_추가할_수_있다() { // given User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null); Group group1 = new Group("name"); @@ -102,9 +104,129 @@ class UserServiceTest { // then User expectedUser = new User(request.name(), request.birthday(), request.introduction(), request.imageUrl(), null); + List userRelatedMembers = entityManager.createQuery("select m from Member m where user = :user") + .setParameter("user", user) + .getResultList(); + + SoftAssertions.assertSoftly(softly -> { + softly.assertThat(user) + .usingRecursiveComparison() + .ignoringFields("id", "createdDate", "lastModifiedDate") + .isEqualTo(expectedUser); + + softly.assertThat(userRelatedMembers) + .extracting(Member::getGroup) + .containsExactlyInAnyOrder(group1, group2); + }); + } + + @Test + void 유저_정보_업데이트_시_그룹을_대체할_수_있다() { + // given + User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null); + Group group1 = new Group("name"); + Group group2 = new Group("name"); + Member member = new Member(user, group1); + + entityManager.persist(user); + entityManager.persist(group1); + entityManager.persist(group2); + entityManager.persist(member); + + UserUpdateRequest request = new UserUpdateRequest(user.getId(), LocalDate.now().plusDays(1), + "new_name", "new_introduction", "new_image_url", List.of( + new GroupRequest(group2.getId(), group2.getName()))); + + // when + userService.updateUser(user.getId(), request); + + // then + User expectedUser = new User(request.name(), request.birthday(), request.introduction(), request.imageUrl(), null); + List userRelatedMembers = entityManager.createQuery("select m from Member m where user = :user") + .setParameter("user", user) + .getResultList(); + + SoftAssertions.assertSoftly(softly -> { + softly.assertThat(user) + .usingRecursiveComparison() + .ignoringFields("id", "createdDate", "lastModifiedDate") + .isEqualTo(expectedUser); + + softly.assertThat(userRelatedMembers) + .extracting(Member::getGroup) + .containsExactlyInAnyOrder(group2); + }); + } + + @Test + void 유저_정보_업데이트_시_그룹을_삭제할_수_있다() { + // given + User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null); + Group group1 = new Group("name"); + Group group2 = new Group("name"); + Member member1 = new Member(user, group1); + Member member2 = new Member(user, group2); + + entityManager.persist(user); + entityManager.persist(group1); + entityManager.persist(group2); + entityManager.persist(member1); + entityManager.persist(member2); + + UserUpdateRequest request = new UserUpdateRequest(user.getId(), LocalDate.now().plusDays(1), + "new_name", "new_introduction", "new_image_url", List.of()); + + // when + userService.updateUser(user.getId(), request); + + // then + User expectedUser = new User(request.name(), request.birthday(), request.introduction(), request.imageUrl(), null); + List userRelatedMembers = entityManager.createQuery("select m from Member m where user = :user") + .setParameter("user", user) + .getResultList(); + + SoftAssertions.assertSoftly(softly -> { + softly.assertThat(user) + .usingRecursiveComparison() + .ignoringFields("id", "createdDate", "lastModifiedDate") + .isEqualTo(expectedUser); + + softly.assertThat(userRelatedMembers) + .hasSize(0); + }); + } + + @Test + void 유저_정보_업데이트_시_그룹을_삭제하면_관련_롤링페이퍼도_삭제된다() { + // given + User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null); + Group group = new Group("name"); + Member member = new Member(user, group); + RollingPaper rollingPaper = new RollingPaper("image_url", "title", "content", + null, member, null); + + entityManager.persist(user); + entityManager.persist(group); + entityManager.persist(member); + entityManager.persist(rollingPaper); + + UserUpdateRequest request = new UserUpdateRequest(user.getId(), LocalDate.now().plusDays(1), + "new_name", "new_introduction", "new_image_url", List.of()); + + // when + userService.updateUser(user.getId(), request); + + // then + User expectedUser = new User(request.name(), request.birthday(), request.introduction(), request.imageUrl(), null); + + SoftAssertions.assertSoftly(softly -> { + softly.assertThat(user) + .usingRecursiveComparison() + .ignoringFields("id", "createdDate", "lastModifiedDate") + .isEqualTo(expectedUser); - assertThat(user).usingRecursiveComparison() - .ignoringFields("id", "createdDate", "lastModifiedDate") - .isEqualTo(expectedUser); + softly.assertThat(entityManager.contains(rollingPaper)) + .isFalse(); + }); } }