From caf2fc9fbba80ce61c2ef7636fbbcdd89610dff5 Mon Sep 17 00:00:00 2001 From: 2hyunjinn Date: Mon, 20 Jan 2025 05:58:54 +0900 Subject: [PATCH] =?UTF-8?q?[refactor]=20#144=20=EB=82=98=EC=9D=98=20?= =?UTF-8?q?=EB=AA=A8=EC=9E=84=20=EC=A0=84=EC=B2=B4=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?API=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../be/api/group/facade/GroupFacade.java | 41 +++---------------- .../api/group/registry/MyGroupStrategy.java | 13 ++++++ .../registry/MyGroupStrategyRegistry.java | 23 +++++++++++ .../group/registry/RegisterGroupStrategy.java | 1 - .../group/strategy/MyApplyGroupStrategy.java | 36 ++++++++++++++++ .../strategy/MyRegisterGroupStrategy.java | 36 ++++++++++++++++ .../be/global/util/GroupVoAggregator.java | 21 ++++++++++ 7 files changed, 135 insertions(+), 36 deletions(-) create mode 100644 src/main/java/com/ggang/be/api/group/registry/MyGroupStrategy.java create mode 100644 src/main/java/com/ggang/be/api/group/registry/MyGroupStrategyRegistry.java create mode 100644 src/main/java/com/ggang/be/api/group/strategy/MyApplyGroupStrategy.java create mode 100644 src/main/java/com/ggang/be/api/group/strategy/MyRegisterGroupStrategy.java create mode 100644 src/main/java/com/ggang/be/global/util/GroupVoAggregator.java diff --git a/src/main/java/com/ggang/be/api/group/facade/GroupFacade.java b/src/main/java/com/ggang/be/api/group/facade/GroupFacade.java index 1eec4ee..8a77af2 100644 --- a/src/main/java/com/ggang/be/api/group/facade/GroupFacade.java +++ b/src/main/java/com/ggang/be/api/group/facade/GroupFacade.java @@ -26,7 +26,6 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; -import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -51,6 +50,7 @@ public class GroupFacade { private final PrepareRegisterGongbaekFacade prepareRegisterGongbaekFacade; private final CancelGroupStrategyRegistry cancelGroupStrategyRegistry; private final GroupUserInfoStrategyRegistry groupUserInfoStrategyRegistry; + private final MyGroupStrategyRegistry myGroupStrategyRegistry; public GroupResponse getGroupInfo(GroupType groupType, Long groupId, long userId) { UserEntity currentUser = userService.getUserById(userId); @@ -122,14 +122,13 @@ public void cancelMyApplication(Long userId, GroupRequest requestDto) { public List getMyGroups(long userId, FillGroupFilterRequest filterRequestDto) { UserEntity currentUser = userService.getUserById(userId); - List groupResponses = switch (filterRequestDto.getFillGroupCategory()) { - case REGISTER -> getGroupsRegister(currentUser, filterRequestDto.status()); - case APPLY -> getGroupsApply(currentUser, filterRequestDto.status()); - }; + MyGroupStrategy groupStrategy = myGroupStrategyRegistry.getGroupStrategy(filterRequestDto.getFillGroupCategory()); + + List groupResponses = groupStrategy.getGroups(currentUser, filterRequestDto.status()); return groupResponses.stream() - .map(MyGroupResponse::fromGroupVo) - .collect(Collectors.toList()); + .map(MyGroupResponse::fromGroupVo) + .collect(Collectors.toList()); } public List getFillGroups(long userId, Category category) { @@ -217,34 +216,6 @@ private NearestGroupResponse getNearestGroupFromDates(NearestEveryGroup nearestE } } - private List getGroupsRegister(UserEntity currentUser, boolean status) { - List everyGroupResponses = everyGroupService.getMyRegisteredGroups( - currentUser, status).groups(); - List onceGroupResponses = onceGroupService.getMyRegisteredGroups(currentUser, - status).groups(); - - return Stream.concat( - everyGroupResponses.stream().map(GroupVo::fromEveryGroup), - onceGroupResponses.stream().map(GroupVo::fromOnceGroup)) - .sorted(Comparator.comparing(GroupVo::createdAt).reversed()) - .collect(Collectors.toList() - ); - } - - private List getGroupsApply(UserEntity currentUser, boolean status) { - List everyGroupResponses = userEveryGroupService.getMyAppliedGroups( - currentUser, status).groups(); - List onceGroupResponses = userOnceGroupService.getMyAppliedGroups(currentUser, - status).groups(); - - return Stream.concat( - everyGroupResponses.stream().map(GroupVo::fromEveryGroup), - onceGroupResponses.stream().map(GroupVo::fromOnceGroup)) - .sorted((group1, group2) -> group2.createdAt().compareTo(group1.createdAt())) - .collect(Collectors.toList() - ); - } - public ReadFillMembersResponse getGroupUsersInfo(Long userId, ReadFillMembersRequest dto) { UserEntity findUserEntity = userService.getUserById(userId); diff --git a/src/main/java/com/ggang/be/api/group/registry/MyGroupStrategy.java b/src/main/java/com/ggang/be/api/group/registry/MyGroupStrategy.java new file mode 100644 index 0000000..1873044 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/registry/MyGroupStrategy.java @@ -0,0 +1,13 @@ +package com.ggang.be.api.group.registry; + +import com.ggang.be.domain.constant.FillGroupType; +import com.ggang.be.domain.group.dto.GroupVo; +import com.ggang.be.domain.user.UserEntity; + +import java.util.List; + +public interface MyGroupStrategy { + boolean supports(FillGroupType category); + + List getGroups(UserEntity userEntity, boolean status); +} diff --git a/src/main/java/com/ggang/be/api/group/registry/MyGroupStrategyRegistry.java b/src/main/java/com/ggang/be/api/group/registry/MyGroupStrategyRegistry.java new file mode 100644 index 0000000..5df950e --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/registry/MyGroupStrategyRegistry.java @@ -0,0 +1,23 @@ +package com.ggang.be.api.group.registry; + +import com.ggang.be.api.common.ResponseError; +import com.ggang.be.api.exception.GongBaekException; +import com.ggang.be.domain.constant.FillGroupType; +import com.ggang.be.global.annotation.Registry; +import lombok.RequiredArgsConstructor; + +import java.util.List; + +@Registry +@RequiredArgsConstructor +public class MyGroupStrategyRegistry { + + private final List groupStrategies; + + public MyGroupStrategy getGroupStrategy(FillGroupType category) { + return groupStrategies.stream() + .filter(strategy -> strategy.supports(category)) + .findFirst() + .orElseThrow(() -> new GongBaekException(ResponseError.BAD_REQUEST)); + } +} diff --git a/src/main/java/com/ggang/be/api/group/registry/RegisterGroupStrategy.java b/src/main/java/com/ggang/be/api/group/registry/RegisterGroupStrategy.java index 642dcf4..df56b4f 100644 --- a/src/main/java/com/ggang/be/api/group/registry/RegisterGroupStrategy.java +++ b/src/main/java/com/ggang/be/api/group/registry/RegisterGroupStrategy.java @@ -8,6 +8,5 @@ public interface RegisterGroupStrategy { boolean support(GroupType groupType); - RegisterGongbaekResponse registerGroup(PrepareRegisterInfo prepareRegisterInfo); } diff --git a/src/main/java/com/ggang/be/api/group/strategy/MyApplyGroupStrategy.java b/src/main/java/com/ggang/be/api/group/strategy/MyApplyGroupStrategy.java new file mode 100644 index 0000000..034c60d --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/strategy/MyApplyGroupStrategy.java @@ -0,0 +1,36 @@ +package com.ggang.be.api.group.strategy; + +import com.ggang.be.api.group.registry.MyGroupStrategy; +import com.ggang.be.api.userEveryGroup.service.UserEveryGroupService; +import com.ggang.be.api.userOnceGroup.service.UserOnceGroupService; +import com.ggang.be.domain.constant.FillGroupType; +import com.ggang.be.domain.group.dto.GroupVo; +import com.ggang.be.domain.group.everyGroup.dto.EveryGroupVo; +import com.ggang.be.domain.group.onceGroup.dto.OnceGroupVo; +import com.ggang.be.domain.user.UserEntity; +import com.ggang.be.global.util.GroupVoAggregator; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@RequiredArgsConstructor +public class MyApplyGroupStrategy implements MyGroupStrategy { + + private final UserEveryGroupService userEveryGroupService; + private final UserOnceGroupService userOnceGroupService; + + @Override + public boolean supports(FillGroupType category) { + return category == FillGroupType.APPLY; + } + + @Override + public List getGroups(UserEntity userEntity, boolean status) { + List everyGroupResponses = userEveryGroupService.getMyAppliedGroups(userEntity, status).groups(); + List onceGroupResponses = userOnceGroupService.getMyAppliedGroups(userEntity, status).groups(); + + return GroupVoAggregator.aggregateAndSort(everyGroupResponses, onceGroupResponses); + } +} diff --git a/src/main/java/com/ggang/be/api/group/strategy/MyRegisterGroupStrategy.java b/src/main/java/com/ggang/be/api/group/strategy/MyRegisterGroupStrategy.java new file mode 100644 index 0000000..9a50607 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/strategy/MyRegisterGroupStrategy.java @@ -0,0 +1,36 @@ +package com.ggang.be.api.group.strategy; + +import com.ggang.be.api.group.everyGroup.service.EveryGroupService; +import com.ggang.be.api.group.onceGroup.service.OnceGroupService; +import com.ggang.be.api.group.registry.MyGroupStrategy; +import com.ggang.be.domain.constant.FillGroupType; +import com.ggang.be.domain.group.dto.GroupVo; +import com.ggang.be.domain.group.everyGroup.dto.EveryGroupVo; +import com.ggang.be.domain.group.onceGroup.dto.OnceGroupVo; +import com.ggang.be.domain.user.UserEntity; +import com.ggang.be.global.util.GroupVoAggregator; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@RequiredArgsConstructor +public class MyRegisterGroupStrategy implements MyGroupStrategy { + + private final EveryGroupService everyGroupService; + private final OnceGroupService onceGroupService; + + @Override + public boolean supports(FillGroupType category) { + return category == FillGroupType.REGISTER; + } + + @Override + public List getGroups(UserEntity userEntity, boolean status) { + List everyGroupResponses = everyGroupService.getMyRegisteredGroups(userEntity, status).groups(); + List onceGroupResponses = onceGroupService.getMyRegisteredGroups(userEntity, status).groups(); + + return GroupVoAggregator.aggregateAndSort(everyGroupResponses, onceGroupResponses); + } +} diff --git a/src/main/java/com/ggang/be/global/util/GroupVoAggregator.java b/src/main/java/com/ggang/be/global/util/GroupVoAggregator.java new file mode 100644 index 0000000..8996509 --- /dev/null +++ b/src/main/java/com/ggang/be/global/util/GroupVoAggregator.java @@ -0,0 +1,21 @@ +package com.ggang.be.global.util; + +import com.ggang.be.domain.group.dto.GroupVo; +import com.ggang.be.domain.group.everyGroup.dto.EveryGroupVo; +import com.ggang.be.domain.group.onceGroup.dto.OnceGroupVo; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class GroupVoAggregator { + + public static List aggregateAndSort(List everyGroupResponses, List onceGroupResponses) { + return Stream.concat( + everyGroupResponses.stream().map(GroupVo::fromEveryGroup), + onceGroupResponses.stream().map(GroupVo::fromOnceGroup) + ) + .sorted((group1, group2) -> group2.createdAt().compareTo(group1.createdAt())) + .collect(Collectors.toList()); + } +}