Skip to content

Commit

Permalink
[refactor] #142 school 추가 filtering 관련하여서 처리해주는 aggregator 활용하여 진행
Browse files Browse the repository at this point in the history
  • Loading branch information
khyojun committed Jan 19, 2025
1 parent 9ea0dfa commit d8ea91a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -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<GroupVo> aggregateActiveGroupResponses(
GroupResponsesDto groupResponsesDto,
UserEntity currentUser) {
List<EveryGroupVo> everyGroupResponses = groupResponsesDto.everyGroupResponses();
List<OnceGroupVo> 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);
}


}
56 changes: 15 additions & 41 deletions src/main/java/com/ggang/be/api/group/facade/GroupFacade.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -142,26 +119,23 @@ public void cancelMyApplication(Long userId, GroupRequest requestDto) {
public List<MyGroupResponse> getMyGroups(long userId, FillGroupFilterRequest filterRequestDto) {
UserEntity currentUser = userService.getUserById(userId);

MyGroupStrategy groupStrategy = myGroupStrategyRegistry.getGroupStrategy(
filterRequestDto.getFillGroupCategory());
MyGroupStrategy groupStrategy = myGroupStrategyRegistry.getGroupStrategy(filterRequestDto.getFillGroupCategory());

List<GroupVo> groupResponses = groupStrategy.getGroups(currentUser,
filterRequestDto.status());
List<GroupVo> groupResponses = groupStrategy.getGroups(currentUser, filterRequestDto.status());

return groupResponses.stream()
.map(MyGroupResponse::fromGroupVo)
.collect(Collectors.toList());
.map(MyGroupResponse::fromGroupVo)
.collect(Collectors.toList());
}

public List<ActiveGroupsResponse> getFillGroups(long userId, Category category) {
UserEntity currentUser = userService.getUserById(userId);

GroupResponsesDto groupResponsesDto = prepareGroupResponsesFacade.prepareGroupResponses(
currentUser, category);

List<GroupVo> groupVos = GroupVoAggregator.aggregateAndSort(
groupResponsesDto.everyGroupResponses(),
groupResponsesDto.onceGroupResponses());

List<GroupVo> groupVos = activeGroupResponseAggregator.aggregateActiveGroupResponses(
groupResponsesDto, currentUser);

return groupVos.stream()
.filter(groupVo -> lectureTimeSlotService.isActiveGroupsInLectureTimeSlot(currentUser,
Expand All @@ -177,15 +151,15 @@ public List<ActiveGroupsResponse> 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)
.collect(Collectors.toList());
}



private NearestGroupResponse getNearestGroupFromDates(NearestEveryGroup nearestEveryGroup,
NearestOnceGroup nearestOnceGroup) {
LocalDate everyGroupDate = nearestEveryGroup.weekDate();
Expand Down

0 comments on commit d8ea91a

Please sign in to comment.