Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] 채우기 모임 전체 조회 API 클래스 분리 진행 #146

Merged
merged 5 commits into from
Jan 20, 2025
Original file line number Diff line number Diff line change
@@ -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<EveryGroupVo> everyGroupResponses = everyGroupService.getActiveEveryGroups(currentUser,
category).groups().
stream().filter(groupVo -> isSameSchoolEveryGroup(currentUser, groupVo)).toList();

List<OnceGroupVo> 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);
}


}
16 changes: 16 additions & 0 deletions src/main/java/com/ggang/be/api/group/dto/CombinedGroupVos.java
Original file line number Diff line number Diff line change
@@ -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<EveryGroupVo> everyGroupVos,
List<OnceGroupVo> onceGroupVos
) {

public static CombinedGroupVos of(List<EveryGroupVo> everyGroupResponses, List<OnceGroupVo> onceGroupResponses) {
return new CombinedGroupVos(everyGroupResponses, onceGroupResponses);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down
84 changes: 21 additions & 63 deletions src/main/java/com/ggang/be/api/group/facade/GroupFacade.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,32 +12,24 @@
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
@RequiredArgsConstructor
@Slf4j
public class GroupFacade {

private final EveryGroupService everyGroupService;
private final OnceGroupService onceGroupService;
private final UserEveryGroupService userEveryGroupService;
private final UserOnceGroupService userOnceGroupService;
private final UserService userService;
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -133,21 +128,17 @@ public List<MyGroupResponse> getMyGroups(long userId, FillGroupFilterRequest fil

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

List<EveryGroupVo> everyGroupResponses = getActiveEveryGroups(currentUser, category);
List<OnceGroupVo> onceGroupResponses = getActiveOnceGroups(currentUser, category);

// TODO 분기처리 하기!!!
List<GroupVo> 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<GroupVo> 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());
}
Expand All @@ -159,47 +150,14 @@ public List<ActiveGroupsResponse> 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<EveryGroupVo> getActiveEveryGroups(UserEntity currentUser, Category category) {
return everyGroupService.getActiveEveryGroups(currentUser, category).groups();
}

private List<OnceGroupVo> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<LectureTimeSlotEntity> readUserTime(UserEntity userById);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading