diff --git a/src/main/java/com/ggang/be/api/group/ActivceCombinedGroupVoPreparer.java b/src/main/java/com/ggang/be/api/group/ActivceCombinedGroupVoPreparer.java new file mode 100644 index 0000000..6a80ea9 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/ActivceCombinedGroupVoPreparer.java @@ -0,0 +1,55 @@ +package com.ggang.be.api.group; + +import com.ggang.be.api.group.dto.CombinedGroupVos; +import com.ggang.be.api.group.everyGroup.service.EveryGroupService; +import com.ggang.be.api.group.onceGroup.service.OnceGroupService; +import com.ggang.be.domain.constant.Category; +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 com.ggang.be.global.annotation.Facade; +import java.util.List; +import lombok.RequiredArgsConstructor; + +@Facade +@RequiredArgsConstructor +public class ActivceCombinedGroupVoPreparer { + + private final EveryGroupService everyGroupService; + private final OnceGroupService onceGroupService; + + public CombinedGroupVos prepareGroupVos(UserEntity currentUser, Category category) { + List everyGroupResponses = everyGroupService.getActiveEveryGroups(currentUser, + category).groups(). + stream().filter(groupVo -> isSameSchoolEveryGroup(currentUser, groupVo)).toList(); + + List onceGroupResponses = onceGroupService.getActiveOnceGroups(currentUser, + category).groups().stream() + .filter(groupVo -> isSameSchoolOnceGroup(currentUser, groupVo)).toList(); + + return CombinedGroupVos.of(everyGroupResponses, onceGroupResponses); + } + + + private boolean isSameSchoolOnceGroup(UserEntity currentUser, OnceGroupVo 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, EveryGroupVo 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/dto/CombinedGroupVos.java b/src/main/java/com/ggang/be/api/group/dto/CombinedGroupVos.java new file mode 100644 index 0000000..f4a5117 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/dto/CombinedGroupVos.java @@ -0,0 +1,16 @@ +package com.ggang.be.api.group.dto; + +import com.ggang.be.domain.group.everyGroup.dto.EveryGroupVo; +import com.ggang.be.domain.group.onceGroup.dto.OnceGroupVo; +import java.util.List; + +public record CombinedGroupVos( + List everyGroupVos, + List onceGroupVos +) { + + public static CombinedGroupVos of(List everyGroupResponses, List onceGroupResponses) { + return new CombinedGroupVos(everyGroupResponses, onceGroupResponses); + } + +} diff --git a/src/main/java/com/ggang/be/api/group/everyGroup/strategy/ApplyEveryGroupStrategy.java b/src/main/java/com/ggang/be/api/group/everyGroup/strategy/ApplyEveryGroupStrategy.java index feba307..9a67e79 100644 --- a/src/main/java/com/ggang/be/api/group/everyGroup/strategy/ApplyEveryGroupStrategy.java +++ b/src/main/java/com/ggang/be/api/group/everyGroup/strategy/ApplyEveryGroupStrategy.java @@ -45,9 +45,7 @@ public void applyGroup(UserEntity findUserEntity, GroupRequest request) { private boolean checkGroupsLectureTimeSlot(UserEntity findUserEntity, GroupVo groupVo) { return lectureTimeSlotService.isActiveGroupsInLectureTimeSlot( findUserEntity, - groupVo.startTime(), - groupVo.endTime(), - groupVo.weekDate() + groupVo ); } 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 8a77af2..623a8d0 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,8 +1,8 @@ package com.ggang.be.api.group.facade; +import com.ggang.be.api.group.ActivceCombinedGroupVoPreparer; +import com.ggang.be.api.group.GroupVoAggregator; 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; @@ -12,23 +12,17 @@ import com.ggang.be.domain.constant.Category; import com.ggang.be.domain.constant.GroupType; 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 com.ggang.be.domain.user.dto.UserInfo; import com.ggang.be.domain.userEveryGroup.dto.NearestEveryGroup; import com.ggang.be.domain.userOnceGroup.dto.NearestOnceGroup; import com.ggang.be.global.annotation.Facade; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.transaction.annotation.Transactional; - import java.time.LocalDate; import java.util.List; import java.util.stream.Collectors; -import java.util.stream.Stream; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.transaction.annotation.Transactional; @Facade @@ -36,8 +30,6 @@ @Slf4j public class GroupFacade { - private final EveryGroupService everyGroupService; - private final OnceGroupService onceGroupService; private final UserEveryGroupService userEveryGroupService; private final UserOnceGroupService userOnceGroupService; private final UserService userService; @@ -50,6 +42,7 @@ public class GroupFacade { private final PrepareRegisterGongbaekFacade prepareRegisterGongbaekFacade; private final CancelGroupStrategyRegistry cancelGroupStrategyRegistry; private final GroupUserInfoStrategyRegistry groupUserInfoStrategyRegistry; + private final ActivceCombinedGroupVoPreparer activceCombinedGroupVoPreparer; private final MyGroupStrategyRegistry myGroupStrategyRegistry; public GroupResponse getGroupInfo(GroupType groupType, Long groupId, long userId) { @@ -80,9 +73,11 @@ public NearestGroupResponse getNearestGroupInfo(long userId) { } public UserInfo getGroupUserInfo(GroupType groupType, Long groupId) { - GroupUserInfoStrategy groupUserInfoStrategy = groupUserInfoStrategyRegistry.getGroupUserInfo(groupType); + GroupUserInfoStrategy groupUserInfoStrategy = groupUserInfoStrategyRegistry.getGroupUserInfo( + groupType); - return UserInfo.of(userService.getUserById(groupUserInfoStrategy.getGroupUserInfo(groupId))); + return UserInfo.of( + userService.getUserById(groupUserInfoStrategy.getGroupUserInfo(groupId))); } @Transactional @@ -113,7 +108,7 @@ public void cancelMyApplication(Long userId, GroupRequest requestDto) { UserEntity findUserEntity = userService.getUserById(userId); CancelGroupStrategy cancelGroupStrategy = cancelGroupStrategyRegistry.getCancelGroupStrategy( - requestDto.groupType() + requestDto.groupType() ); cancelGroupStrategy.cancelGroup(findUserEntity, requestDto); @@ -133,21 +128,17 @@ public List getMyGroups(long userId, FillGroupFilterRequest fil public List getFillGroups(long userId, Category category) { UserEntity currentUser = userService.getUserById(userId); + CombinedGroupVos preparedGroupVo = activceCombinedGroupVoPreparer.prepareGroupVos( + currentUser, category); - List everyGroupResponses = getActiveEveryGroups(currentUser, category); - List onceGroupResponses = getActiveOnceGroups(currentUser, category); - - // TODO 분기처리 하기!!! - List allGroups = 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(); + List groupVos = GroupVoAggregator.aggregateAndSort( + preparedGroupVo.everyGroupVos(), + preparedGroupVo.onceGroupVos() + ); - return allGroups.stream() - .filter(groupVo -> checkGroupsLectureTimeSlot(currentUser, groupVo)) + return groupVos.stream() + .filter(groupVo -> lectureTimeSlotService.isActiveGroupsInLectureTimeSlot(currentUser, + groupVo)) .map(ActiveGroupsResponse::fromGroupVo) .collect(Collectors.toList()); } @@ -159,47 +150,14 @@ public List getLatestGroups(long userId, GroupType groupTy groupType); return latestGroupStrategy.getLatestGroups(currentUser).stream() - .filter(groupVo -> checkGroupsLectureTimeSlot(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 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); - } - - private List getActiveEveryGroups(UserEntity currentUser, Category category) { - return everyGroupService.getActiveEveryGroups(currentUser, category).groups(); - } - - private List getActiveOnceGroups(UserEntity currentUser, Category category) { - return onceGroupService.getActiveOnceGroups(currentUser, category).groups(); - } - - private boolean checkGroupsLectureTimeSlot(UserEntity findUserEntity, GroupVo groupVo) { - return lectureTimeSlotService.isActiveGroupsInLectureTimeSlot( - findUserEntity, - groupVo.startTime(), - groupVo.endTime(), - groupVo.weekDate() - ); - } private NearestGroupResponse getNearestGroupFromDates(NearestEveryGroup nearestEveryGroup, NearestOnceGroup nearestOnceGroup) { diff --git a/src/main/java/com/ggang/be/api/group/onceGroup/strategy/ApplyOnceGroupStrategy.java b/src/main/java/com/ggang/be/api/group/onceGroup/strategy/ApplyOnceGroupStrategy.java index 1a387f7..4bc8c63 100644 --- a/src/main/java/com/ggang/be/api/group/onceGroup/strategy/ApplyOnceGroupStrategy.java +++ b/src/main/java/com/ggang/be/api/group/onceGroup/strategy/ApplyOnceGroupStrategy.java @@ -45,9 +45,7 @@ public void applyGroup(UserEntity findUserEntity, GroupRequest request) { private boolean checkGroupsLectureTimeSlot(UserEntity findUserEntity, GroupVo groupVo) { return lectureTimeSlotService.isActiveGroupsInLectureTimeSlot( findUserEntity, - groupVo.startTime(), - groupVo.endTime(), - groupVo.weekDate() + groupVo ); } diff --git a/src/main/java/com/ggang/be/api/lectureTimeSlot/service/LectureTimeSlotService.java b/src/main/java/com/ggang/be/api/lectureTimeSlot/service/LectureTimeSlotService.java index 6e79026..df0a9e9 100644 --- a/src/main/java/com/ggang/be/api/lectureTimeSlot/service/LectureTimeSlotService.java +++ b/src/main/java/com/ggang/be/api/lectureTimeSlot/service/LectureTimeSlotService.java @@ -1,6 +1,7 @@ package com.ggang.be.api.lectureTimeSlot.service; import com.ggang.be.domain.constant.WeekDate; +import com.ggang.be.domain.group.dto.GroupVo; import com.ggang.be.domain.timslot.lectureTimeSlot.LectureTimeSlotEntity; import com.ggang.be.domain.timslot.lectureTimeSlot.dto.LectureTimeSlotRequest; import com.ggang.be.domain.timslot.lectureTimeSlot.vo.LectureTimeSlotVo; @@ -13,7 +14,7 @@ public interface LectureTimeSlotService { void isExistInLectureTImeSlot(UserEntity findUserEntity, LectureTimeSlotRequest dto); - boolean isActiveGroupsInLectureTimeSlot(UserEntity findUserEntity, double startTime, double endTime, WeekDate weekDate); + boolean isActiveGroupsInLectureTimeSlot(UserEntity findUserEntity, GroupVo groupVo); List readUserTime(UserEntity userById); } diff --git a/src/main/java/com/ggang/be/domain/timslot/lectureTimeSlot/application/LectureTimeSlotServiceImpl.java b/src/main/java/com/ggang/be/domain/timslot/lectureTimeSlot/application/LectureTimeSlotServiceImpl.java index a9c834e..6c12ef4 100644 --- a/src/main/java/com/ggang/be/domain/timslot/lectureTimeSlot/application/LectureTimeSlotServiceImpl.java +++ b/src/main/java/com/ggang/be/domain/timslot/lectureTimeSlot/application/LectureTimeSlotServiceImpl.java @@ -3,7 +3,7 @@ import com.ggang.be.api.common.ResponseError; import com.ggang.be.api.exception.GongBaekException; import com.ggang.be.api.lectureTimeSlot.service.LectureTimeSlotService; -import com.ggang.be.domain.constant.WeekDate; +import com.ggang.be.domain.group.dto.GroupVo; import com.ggang.be.domain.timslot.lectureTimeSlot.LectureTimeSlotEntity; import com.ggang.be.domain.timslot.lectureTimeSlot.dto.LectureTimeSlotRequest; import com.ggang.be.domain.timslot.lectureTimeSlot.infra.LectureTimeSlotRepository; @@ -38,8 +38,12 @@ public void isExistInLectureTImeSlot(UserEntity findUserEntity, LectureTimeSlotR } @Override - public boolean isActiveGroupsInLectureTimeSlot(UserEntity findUserEntity, double startTime, double endTime, WeekDate weekDate) { - return !lectureTimeSlotRepository.isInTime(startTime, endTime, findUserEntity, weekDate); + public boolean isActiveGroupsInLectureTimeSlot(UserEntity findUserEntity, GroupVo groupVo) { + return !lectureTimeSlotRepository.isInTime( + groupVo.startTime(), + groupVo.endTime(), + findUserEntity, + groupVo.weekDate()); } @Override