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()); + } +}