From d8ea91aeb4d7f6e216aea5a8233dbf8fb6d08bc8 Mon Sep 17 00:00:00 2001 From: Khyojun Date: Mon, 20 Jan 2025 06:57:09 +0900 Subject: [PATCH] =?UTF-8?q?[refactor]=20#142=20school=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=20filtering=20=EA=B4=80=EB=A0=A8=ED=95=98=EC=97=AC=EC=84=9C=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=ED=95=B4=EC=A3=BC=EB=8A=94=20aggregator=20?= =?UTF-8?q?=ED=99=9C=EC=9A=A9=ED=95=98=EC=97=AC=20=EC=A7=84=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../group/ActiveGroupResponseAggregator.java | 59 +++++++++++++++++++ .../be/api/group/facade/GroupFacade.java | 56 +++++------------- 2 files changed, 74 insertions(+), 41 deletions(-) create mode 100644 src/main/java/com/ggang/be/api/group/ActiveGroupResponseAggregator.java diff --git a/src/main/java/com/ggang/be/api/group/ActiveGroupResponseAggregator.java b/src/main/java/com/ggang/be/api/group/ActiveGroupResponseAggregator.java new file mode 100644 index 0000000..302551d --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/ActiveGroupResponseAggregator.java @@ -0,0 +1,59 @@ +package com.ggang.be.api.group; + + +import com.ggang.be.api.group.dto.GroupResponsesDto; +import com.ggang.be.api.group.everyGroup.service.EveryGroupService; +import com.ggang.be.api.group.onceGroup.service.OnceGroupService; +import com.ggang.be.domain.group.dto.GroupVo; +import com.ggang.be.domain.group.everyGroup.EveryGroupEntity; +import com.ggang.be.domain.group.everyGroup.dto.EveryGroupVo; +import com.ggang.be.domain.group.onceGroup.OnceGroupEntity; +import com.ggang.be.domain.group.onceGroup.dto.OnceGroupVo; +import com.ggang.be.domain.user.UserEntity; +import java.util.List; +import java.util.stream.Stream; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class ActiveGroupResponseAggregator { + + private final EveryGroupService everyGroupService; + private final OnceGroupService onceGroupService; + + + public List aggregateActiveGroupResponses( + GroupResponsesDto groupResponsesDto, + UserEntity currentUser) { + List everyGroupResponses = groupResponsesDto.everyGroupResponses(); + List onceGroupResponses = groupResponsesDto.onceGroupResponses(); + return Stream.concat( + everyGroupResponses.stream().map(GroupVo::fromEveryGroup) + .filter(groupVo -> isSameSchoolEveryGroup(currentUser, groupVo)), + onceGroupResponses.stream().map(GroupVo::fromOnceGroup)) + .filter(groupVo -> isSameSchoolOnceGroup(currentUser, groupVo)) + .sorted((group1, group2) -> group2.createdAt().compareTo(group1.createdAt())) + .toList(); + } + + private boolean isSameSchoolOnceGroup(UserEntity currentUser, GroupVo groupVo) { + String userSchool = currentUser.getSchool().getSchoolName(); + OnceGroupEntity onceGroupEntity = onceGroupService.findOnceGroupEntityByGroupId( + groupVo.groupId()); + String groupCreatorSchool = onceGroupEntity.getUserEntity().getSchool().getSchoolName(); + + return userSchool.equals(groupCreatorSchool); + } + + private boolean isSameSchoolEveryGroup(UserEntity currentUser, GroupVo groupVo) { + String userSchool = currentUser.getSchool().getSchoolName(); + EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId( + groupVo.groupId()); + String groupCreatorSchool = everyGroupEntity.getUserEntity().getSchool().getSchoolName(); + + return userSchool.equals(groupCreatorSchool); + } + + +} 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 94589e4..41dd2c5 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 @@ -1,34 +1,10 @@ package com.ggang.be.api.group.facade; -import com.ggang.be.api.group.GroupVoAggregator; -import com.ggang.be.api.group.dto.ActiveGroupsResponse; -import com.ggang.be.api.group.dto.FillGroupFilterRequest; -import com.ggang.be.api.group.dto.GroupRequest; -import com.ggang.be.api.group.dto.GroupResponse; -import com.ggang.be.api.group.dto.GroupResponsesDto; -import com.ggang.be.api.group.dto.MyGroupResponse; -import com.ggang.be.api.group.dto.NearestGroupResponse; -import com.ggang.be.api.group.dto.PrepareRegisterInfo; -import com.ggang.be.api.group.dto.ReadFillMembersRequest; -import com.ggang.be.api.group.dto.ReadFillMembersResponse; -import com.ggang.be.api.group.dto.RegisterGongbaekRequest; -import com.ggang.be.api.group.dto.RegisterGongbaekResponse; -import com.ggang.be.api.group.registry.ApplyGroupStrategy; -import com.ggang.be.api.group.registry.ApplyGroupStrategyRegistry; -import com.ggang.be.api.group.registry.CancelGroupStrategy; -import com.ggang.be.api.group.registry.CancelGroupStrategyRegistry; -import com.ggang.be.api.group.registry.GroupInfoStrategy; -import com.ggang.be.api.group.registry.GroupInfoStrategyRegistry; -import com.ggang.be.api.group.registry.GroupUserInfoStrategy; -import com.ggang.be.api.group.registry.GroupUserInfoStrategyRegistry; -import com.ggang.be.api.group.registry.LatestGroupStrategy; -import com.ggang.be.api.group.registry.LatestGroupStrategyRegistry; -import com.ggang.be.api.group.registry.MyGroupStrategy; -import com.ggang.be.api.group.registry.MyGroupStrategyRegistry; -import com.ggang.be.api.group.registry.ReadFillMemberStrategy; -import com.ggang.be.api.group.registry.ReadFillMemberStrategyRegistry; -import com.ggang.be.api.group.registry.RegisterGroupStrategy; -import com.ggang.be.api.group.registry.RegisterGroupStrategyRegistry; +import com.ggang.be.api.group.ActiveGroupResponseAggregator; +import com.ggang.be.api.group.dto.*; +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.*; import com.ggang.be.api.lectureTimeSlot.service.LectureTimeSlotService; import com.ggang.be.api.mapper.GroupResponseMapper; import com.ggang.be.api.user.service.UserService; @@ -68,6 +44,7 @@ public class GroupFacade { private final CancelGroupStrategyRegistry cancelGroupStrategyRegistry; private final GroupUserInfoStrategyRegistry groupUserInfoStrategyRegistry; private final PrepareGroupResponsesFacade prepareGroupResponsesFacade; + private final ActiveGroupResponseAggregator activeGroupResponseAggregator; private final MyGroupStrategyRegistry myGroupStrategyRegistry; public GroupResponse getGroupInfo(GroupType groupType, Long groupId, long userId) { @@ -142,26 +119,23 @@ public void cancelMyApplication(Long userId, GroupRequest requestDto) { public List getMyGroups(long userId, FillGroupFilterRequest filterRequestDto) { UserEntity currentUser = userService.getUserById(userId); - MyGroupStrategy groupStrategy = myGroupStrategyRegistry.getGroupStrategy( - filterRequestDto.getFillGroupCategory()); + MyGroupStrategy groupStrategy = myGroupStrategyRegistry.getGroupStrategy(filterRequestDto.getFillGroupCategory()); - List groupResponses = groupStrategy.getGroups(currentUser, - filterRequestDto.status()); + 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) { UserEntity currentUser = userService.getUserById(userId); - GroupResponsesDto groupResponsesDto = prepareGroupResponsesFacade.prepareGroupResponses( currentUser, category); - List groupVos = GroupVoAggregator.aggregateAndSort( - groupResponsesDto.everyGroupResponses(), - groupResponsesDto.onceGroupResponses()); + + List groupVos = activeGroupResponseAggregator.aggregateActiveGroupResponses( + groupResponsesDto, currentUser); return groupVos.stream() .filter(groupVo -> lectureTimeSlotService.isActiveGroupsInLectureTimeSlot(currentUser, @@ -177,8 +151,7 @@ public List getLatestGroups(long userId, GroupType groupTy groupType); return latestGroupStrategy.getLatestGroups(currentUser).stream() - .filter(groupVo -> lectureTimeSlotService.isActiveGroupsInLectureTimeSlot(currentUser, - groupVo)) + .filter(groupVo -> lectureTimeSlotService.isActiveGroupsInLectureTimeSlot(currentUser, groupVo)) .sorted((group1, group2) -> group2.createdAt().compareTo(group1.createdAt())) .limit(5) .map(ActiveGroupsResponse::fromGroupVo) @@ -186,6 +159,7 @@ public List getLatestGroups(long userId, GroupType groupTy } + private NearestGroupResponse getNearestGroupFromDates(NearestEveryGroup nearestEveryGroup, NearestOnceGroup nearestOnceGroup) { LocalDate everyGroupDate = nearestEveryGroup.weekDate();