diff --git a/src/main/java/com/ggang/be/api/group/dto/PrepareRegisterInfo.java b/src/main/java/com/ggang/be/api/group/dto/PrepareRegisterInfo.java new file mode 100644 index 0000000..c39879d --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/dto/PrepareRegisterInfo.java @@ -0,0 +1,15 @@ +package com.ggang.be.api.group.dto; + +import com.ggang.be.domain.group.dto.RegisterGroupServiceRequest; +import com.ggang.be.domain.timslot.gongbaekTimeSlot.GongbaekTimeSlotEntity; +import com.ggang.be.domain.user.UserEntity; + +public record PrepareRegisterInfo(UserEntity findUserEntity, + GongbaekTimeSlotEntity gongbaekTimeSlotEntity, + RegisterGroupServiceRequest request) { + + public static PrepareRegisterInfo of(UserEntity findUserEntity, GongbaekTimeSlotEntity gongbaekTimeSlotEntity, + RegisterGroupServiceRequest serviceRequest) { + return new PrepareRegisterInfo(findUserEntity, gongbaekTimeSlotEntity, serviceRequest); + } +} diff --git a/src/main/java/com/ggang/be/api/group/everyGroup/strategy/RegisterEveryGroupStrategy.java b/src/main/java/com/ggang/be/api/group/everyGroup/strategy/RegisterEveryGroupStrategy.java new file mode 100644 index 0000000..54ac9ab --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/everyGroup/strategy/RegisterEveryGroupStrategy.java @@ -0,0 +1,44 @@ +package com.ggang.be.api.group.everyGroup.strategy; + +import com.ggang.be.api.group.dto.PrepareRegisterInfo; +import com.ggang.be.api.group.dto.RegisterGongbaekResponse; +import com.ggang.be.api.group.everyGroup.service.EveryGroupService; +import com.ggang.be.api.group.registry.RegisterGroupStrategy; +import com.ggang.be.api.userEveryGroup.service.UserEveryGroupService; +import com.ggang.be.domain.constant.GroupType; +import com.ggang.be.domain.group.dto.RegisterGroupServiceRequest; +import com.ggang.be.domain.group.everyGroup.EveryGroupEntity; +import com.ggang.be.domain.timslot.gongbaekTimeSlot.GongbaekTimeSlotEntity; +import com.ggang.be.domain.user.UserEntity; +import com.ggang.be.global.annotation.Strategy; +import lombok.RequiredArgsConstructor; + +@Strategy +@RequiredArgsConstructor +public class RegisterEveryGroupStrategy implements RegisterGroupStrategy { + + private final EveryGroupService everyGroupService; + private final UserEveryGroupService userEveryGroupService; + + + @Override + public boolean support(GroupType groupType) { + return groupType.equals(GroupType.WEEKLY); + } + + @Override + public RegisterGongbaekResponse registerGroup(PrepareRegisterInfo prepareRegisterInfo) { + UserEntity findUserEntity = prepareRegisterInfo.findUserEntity(); + RegisterGroupServiceRequest serviceRequest = prepareRegisterInfo.request(); + GongbaekTimeSlotEntity gongbaekTimeSlotEntity = prepareRegisterInfo.gongbaekTimeSlotEntity(); + + + EveryGroupEntity everyGroupEntity = everyGroupService.registerEveryGroup(serviceRequest, + gongbaekTimeSlotEntity); + userEveryGroupService.applyEveryGroup(findUserEntity, everyGroupEntity); + return RegisterGongbaekResponse.of( + everyGroupEntity.getId()); + } + + +} 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 2f6e0bd..392e8c3 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 @@ -2,17 +2,29 @@ import com.ggang.be.api.common.ResponseError; import com.ggang.be.api.exception.GongBaekException; -import com.ggang.be.api.gongbaekTimeSlot.service.GongbaekTimeSlotService; -import com.ggang.be.api.group.dto.*; +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.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.everyGroup.service.EveryGroupService; import com.ggang.be.api.group.onceGroup.service.OnceGroupService; -import com.ggang.be.api.group.registry.*; import com.ggang.be.api.group.registry.ApplyGroupStrategy; import com.ggang.be.api.group.registry.ApplyGroupStrategyRegistry; +import com.ggang.be.api.group.registry.GroupInfoStrategy; +import com.ggang.be.api.group.registry.GroupInfoStrategyRegistry; import com.ggang.be.api.group.registry.LatestGroupStrategy; import com.ggang.be.api.group.registry.LatestGroupStrategyRegistry; 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.lectureTimeSlot.service.LectureTimeSlotService; import com.ggang.be.api.mapper.GroupResponseMapper; import com.ggang.be.api.user.service.UserService; @@ -22,44 +34,43 @@ 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.dto.RegisterGroupServiceRequest; 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.timslot.gongbaekTimeSlot.GongbaekTimeSlotEntity; -import com.ggang.be.domain.timslot.gongbaekTimeSlot.dto.GongbaekTimeSlotRequest; 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.Comparator; 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; - private final GongbaekTimeSlotService gongbaekTimeSlotService; private final LectureTimeSlotService lectureTimeSlotService; private final SameSchoolValidator sameSchoolValidator; private final LatestGroupStrategyRegistry latestGroupStrategyRegistry; private final ReadFillMemberStrategyRegistry readFillMemberStrategyRegistry; private final GroupInfoStrategyRegistry groupInfoStrategyRegistry; private final ApplyGroupStrategyRegistry applyGroupStrategyRegistry; + private final RegisterGroupStrategyRegistry registerGroupStrategyRegistry; + private final PrepareRegisterGongbaekFacade prepareRegisterGongbaekFacade; public GroupResponse getGroupInfo(GroupType groupType, Long groupId, long userId) { UserEntity currentUser = userService.getUserById(userId); @@ -71,7 +82,8 @@ public GroupResponse getGroupInfo(GroupType groupType, Long groupId, long userId public NearestGroupResponse getNearestGroupInfo(long userId) { UserEntity currentUser = userService.getUserById(userId); - NearestEveryGroup nearestEveryGroup = userEveryGroupService.getMyNearestEveryGroup(currentUser); + NearestEveryGroup nearestEveryGroup = userEveryGroupService.getMyNearestEveryGroup( + currentUser); NearestOnceGroup nearestOnceGroup = userOnceGroupService.getMyNearestOnceGroup(currentUser); if (nearestEveryGroup == null && nearestOnceGroup == null) { @@ -98,29 +110,14 @@ public UserInfo getGroupUserInfo(GroupType groupType, Long groupId) { @Transactional public RegisterGongbaekResponse registerGongbaek(Long userId, RegisterGongbaekRequest dto) { - UserEntity findUserEntity = userService.getUserById(userId); - GongbaekTimeSlotRequest gongbaekDto = convertDtoToGongbaekDto(dto, findUserEntity); - RegisterGroupServiceRequest serviceRequest = convertDtoToServiceDto(dto, findUserEntity); + PrepareRegisterInfo prepareRegisterInfo = prepareRegisterGongbaekFacade.prepareInfo(userId, + dto); - GongbaekTimeSlotEntity gongbaekTimeSlotEntity = gongbaekTimeSlotService.registerGongbaekTimeSlot( - findUserEntity, gongbaekDto); - - if (dto.groupType() == GroupType.WEEKLY) { - EveryGroupEntity everyGroupEntity = everyGroupService.registerEveryGroup(serviceRequest, - gongbaekTimeSlotEntity); - userEveryGroupService.applyEveryGroup(findUserEntity, everyGroupEntity); - return RegisterGongbaekResponse.of( - everyGroupEntity.getId()); - } - if (dto.groupType() == GroupType.ONCE) { - OnceGroupEntity onceGroupEntity = onceGroupService.registerOnceGroup(serviceRequest, - gongbaekTimeSlotEntity); - userOnceGroupService.applyOnceGroup(findUserEntity, onceGroupEntity); - return RegisterGongbaekResponse.of(onceGroupEntity.getId()); - } + RegisterGroupStrategy registerGroupStrategy = registerGroupStrategyRegistry.getRegisterGroupStrategy( + dto.groupType()); - throw new GongBaekException(ResponseError.BAD_REQUEST); + return registerGroupStrategy.registerGroup(prepareRegisterInfo); } @Transactional @@ -137,34 +134,28 @@ public void applyGroup(Long userId, GroupRequest requestDto) { public void cancelMyApplication(Long userId, GroupRequest requestDto) { UserEntity findUserEntity = userService.getUserById(userId); - switch (requestDto.groupType()){ + switch (requestDto.groupType()) { case WEEKLY -> { - EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId(requestDto.groupId()); - if(everyGroupService.validateCancelEveryGroup(findUserEntity, everyGroupEntity)) + EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId( + requestDto.groupId()); + if (everyGroupService.validateCancelEveryGroup(findUserEntity, everyGroupEntity)) { userEveryGroupService.cancelEveryGroup(findUserEntity, everyGroupEntity); - else throw new GongBaekException(ResponseError.GROUP_CANCEL_NOT_FOUND); + } else { + throw new GongBaekException(ResponseError.GROUP_CANCEL_NOT_FOUND); + } } case ONCE -> { - OnceGroupEntity onceGroupEntity = onceGroupService.findOnceGroupEntityByGroupId(requestDto.groupId()); - if(onceGroupService.validateCancelOnceGroup(findUserEntity, onceGroupEntity)) + OnceGroupEntity onceGroupEntity = onceGroupService.findOnceGroupEntityByGroupId( + requestDto.groupId()); + if (onceGroupService.validateCancelOnceGroup(findUserEntity, onceGroupEntity)) { userOnceGroupService.cancelOnceGroup(findUserEntity, onceGroupEntity); - else throw new GongBaekException(ResponseError.GROUP_CANCEL_NOT_FOUND); + } else { + throw new GongBaekException(ResponseError.GROUP_CANCEL_NOT_FOUND); + } } } } - private RegisterGroupServiceRequest convertDtoToServiceDto( - RegisterGongbaekRequest dto, UserEntity findUserEntity - ) { - return RegisterGongbaekRequest.toServiceRequest(findUserEntity, dto); - } - - private GongbaekTimeSlotRequest convertDtoToGongbaekDto( - RegisterGongbaekRequest dto, UserEntity findUserEntity - ) { - return RegisterGongbaekRequest.toGongbaekTimeSlotRequest(findUserEntity, dto); - } - public List getMyGroups(long userId, FillGroupFilterRequest filterRequestDto) { UserEntity currentUser = userService.getUserById(userId); @@ -174,8 +165,8 @@ public List getMyGroups(long userId, FillGroupFilterRequest fil }; return groupResponses.stream() - .map(MyGroupResponse::fromGroupVo) - .collect(Collectors.toList()); + .map(MyGroupResponse::fromGroupVo) + .collect(Collectors.toList()); } public List getFillGroups(long userId, Category category) { @@ -186,35 +177,37 @@ public List getFillGroups(long userId, Category 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(); + 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(); return allGroups.stream() - .filter(groupVo -> checkGroupsLectureTimeSlot(currentUser, groupVo)) - .map(ActiveGroupsResponse::fromGroupVo) - .collect(Collectors.toList()); + .filter(groupVo -> checkGroupsLectureTimeSlot(currentUser, groupVo)) + .map(ActiveGroupsResponse::fromGroupVo) + .collect(Collectors.toList()); } public List getLatestGroups(long userId, GroupType groupType) { UserEntity currentUser = userService.getUserById(userId); - LatestGroupStrategy latestGroupStrategy = latestGroupStrategyRegistry.getGroupStrategy(groupType); + LatestGroupStrategy latestGroupStrategy = latestGroupStrategyRegistry.getGroupStrategy( + groupType); return latestGroupStrategy.getLatestGroups(currentUser).stream() - .filter(groupVo -> checkGroupsLectureTimeSlot(currentUser, groupVo)) - .sorted((group1, group2) -> group2.createdAt().compareTo(group1.createdAt())) - .limit(5) - .map(ActiveGroupsResponse::fromGroupVo) - .collect(Collectors.toList()); + .filter(groupVo -> checkGroupsLectureTimeSlot(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()); + OnceGroupEntity onceGroupEntity = onceGroupService.findOnceGroupEntityByGroupId( + groupVo.groupId()); String groupCreatorSchool = onceGroupEntity.getUserEntity().getSchool().getSchoolName(); return userSchool.equals(groupCreatorSchool); @@ -222,7 +215,8 @@ private boolean isSameSchoolOnceGroup(UserEntity currentUser, GroupVo groupVo) { private boolean isSameSchoolEveryGroup(UserEntity currentUser, GroupVo groupVo) { String userSchool = currentUser.getSchool().getSchoolName(); - EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId(groupVo.groupId()); + EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId( + groupVo.groupId()); String groupCreatorSchool = everyGroupEntity.getUserEntity().getSchool().getSchoolName(); return userSchool.equals(groupCreatorSchool); @@ -238,14 +232,15 @@ private List getActiveOnceGroups(UserEntity currentUser, Category c private boolean checkGroupsLectureTimeSlot(UserEntity findUserEntity, GroupVo groupVo) { return lectureTimeSlotService.isActiveGroupsInLectureTimeSlot( - findUserEntity, - groupVo.startTime(), - groupVo.endTime(), - groupVo.weekDate() + findUserEntity, + groupVo.startTime(), + groupVo.endTime(), + groupVo.weekDate() ); } - private NearestGroupResponse getNearestGroupFromDates(NearestEveryGroup nearestEveryGroup, NearestOnceGroup nearestOnceGroup) { + private NearestGroupResponse getNearestGroupFromDates(NearestEveryGroup nearestEveryGroup, + NearestOnceGroup nearestOnceGroup) { LocalDate everyGroupDate = nearestEveryGroup.weekDate(); LocalDate onceGroupDate = nearestOnceGroup.weekDate(); @@ -259,40 +254,48 @@ 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(); + 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() - ); + 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(); + 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() - ); + everyGroupResponses.stream().map(GroupVo::fromEveryGroup), + onceGroupResponses.stream().map(GroupVo::fromOnceGroup)) + .sorted((group1, group2) -> group2.createdAt().compareTo(group1.createdAt())) + .collect(Collectors.toList() + ); } - private void sameSchoolValidateByEveryList(UserEntity userEntity, List everyGroupResponses){ + private void sameSchoolValidateByEveryList(UserEntity userEntity, + List everyGroupResponses) { for (EveryGroupVo everyGroupVo : everyGroupResponses) { - EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId(everyGroupVo.groupId()); + EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId( + everyGroupVo.groupId()); sameSchoolValidator.isUserReadMySchoolEveryGroup(userEntity, everyGroupEntity); } } - private void sameSchoolValidateByOnceList(UserEntity userEntity, List onceGroupResponses){ + private void sameSchoolValidateByOnceList(UserEntity userEntity, + List onceGroupResponses) { for (OnceGroupVo onceGroupVo : onceGroupResponses) { - OnceGroupEntity onceGroupEntity = onceGroupService.findOnceGroupEntityByGroupId(onceGroupVo.groupId()); + OnceGroupEntity onceGroupEntity = onceGroupService.findOnceGroupEntityByGroupId( + onceGroupVo.groupId()); sameSchoolValidator.isUserReadMySchoolOnceGroup(userEntity, onceGroupEntity); } } diff --git a/src/main/java/com/ggang/be/api/group/facade/PrepareRegisterGongbaekFacade.java b/src/main/java/com/ggang/be/api/group/facade/PrepareRegisterGongbaekFacade.java new file mode 100644 index 0000000..2edbe63 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/facade/PrepareRegisterGongbaekFacade.java @@ -0,0 +1,46 @@ +package com.ggang.be.api.group.facade; + +import com.ggang.be.api.gongbaekTimeSlot.service.GongbaekTimeSlotService; +import com.ggang.be.api.group.dto.PrepareRegisterInfo; +import com.ggang.be.api.group.dto.RegisterGongbaekRequest; +import com.ggang.be.api.user.service.UserService; +import com.ggang.be.domain.group.dto.RegisterGroupServiceRequest; +import com.ggang.be.domain.timslot.gongbaekTimeSlot.GongbaekTimeSlotEntity; +import com.ggang.be.domain.timslot.gongbaekTimeSlot.dto.GongbaekTimeSlotRequest; +import com.ggang.be.domain.user.UserEntity; +import com.ggang.be.global.annotation.Facade; +import lombok.RequiredArgsConstructor; + +@Facade +@RequiredArgsConstructor +public class PrepareRegisterGongbaekFacade { + + private final UserService userService; + private final GongbaekTimeSlotService gongbaekTimeSlotService; + + public PrepareRegisterInfo prepareInfo(Long userId, RegisterGongbaekRequest dto){ + UserEntity findUserEntity = userService.getUserById(userId); + + GongbaekTimeSlotRequest gongbaekDto = convertDtoToGongbaekDto(dto, findUserEntity); + RegisterGroupServiceRequest serviceRequest = convertDtoToServiceDto(dto, findUserEntity); + + GongbaekTimeSlotEntity gongbaekTimeSlotEntity = gongbaekTimeSlotService.registerGongbaekTimeSlot( + findUserEntity, gongbaekDto); + + return PrepareRegisterInfo.of(findUserEntity, gongbaekTimeSlotEntity, serviceRequest); + } + + private RegisterGroupServiceRequest convertDtoToServiceDto( + RegisterGongbaekRequest dto, UserEntity findUserEntity + ) { + return RegisterGongbaekRequest.toServiceRequest(findUserEntity, dto); + } + + private GongbaekTimeSlotRequest convertDtoToGongbaekDto( + RegisterGongbaekRequest dto, UserEntity findUserEntity + ) { + return RegisterGongbaekRequest.toGongbaekTimeSlotRequest(findUserEntity, dto); + } + + +} diff --git a/src/main/java/com/ggang/be/api/group/onceGroup/strategy/RegisterOnceGroupStrategy.java b/src/main/java/com/ggang/be/api/group/onceGroup/strategy/RegisterOnceGroupStrategy.java new file mode 100644 index 0000000..3c59e88 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/onceGroup/strategy/RegisterOnceGroupStrategy.java @@ -0,0 +1,41 @@ +package com.ggang.be.api.group.onceGroup.strategy; + +import com.ggang.be.api.group.dto.PrepareRegisterInfo; +import com.ggang.be.api.group.dto.RegisterGongbaekResponse; +import com.ggang.be.api.group.onceGroup.service.OnceGroupService; +import com.ggang.be.api.group.registry.RegisterGroupStrategy; +import com.ggang.be.api.userOnceGroup.service.UserOnceGroupService; +import com.ggang.be.domain.constant.GroupType; +import com.ggang.be.domain.group.dto.RegisterGroupServiceRequest; +import com.ggang.be.domain.group.onceGroup.OnceGroupEntity; +import com.ggang.be.domain.timslot.gongbaekTimeSlot.GongbaekTimeSlotEntity; +import com.ggang.be.domain.user.UserEntity; +import com.ggang.be.global.annotation.Strategy; +import lombok.RequiredArgsConstructor; + +@Strategy +@RequiredArgsConstructor +public class RegisterOnceGroupStrategy implements RegisterGroupStrategy { + + private final OnceGroupService onceGroupService; + private final UserOnceGroupService userOnceGroupService; + + @Override + public boolean support(GroupType groupType) { + return groupType.equals(GroupType.ONCE); + } + + @Override + public RegisterGongbaekResponse registerGroup(PrepareRegisterInfo prepareRegisterInfo) { + UserEntity findUserEntity = prepareRegisterInfo.findUserEntity(); + RegisterGroupServiceRequest serviceRequest = prepareRegisterInfo.request(); + GongbaekTimeSlotEntity gongbaekTimeSlotEntity = prepareRegisterInfo.gongbaekTimeSlotEntity(); + + OnceGroupEntity onceGroupEntity = onceGroupService.registerOnceGroup(serviceRequest, + gongbaekTimeSlotEntity); + userOnceGroupService.applyOnceGroup(findUserEntity, onceGroupEntity); + return RegisterGongbaekResponse.of(onceGroupEntity.getId()); + } + + +} 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 new file mode 100644 index 0000000..642dcf4 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/registry/RegisterGroupStrategy.java @@ -0,0 +1,13 @@ +package com.ggang.be.api.group.registry; + +import com.ggang.be.api.group.dto.PrepareRegisterInfo; +import com.ggang.be.api.group.dto.RegisterGongbaekResponse; +import com.ggang.be.domain.constant.GroupType; + +public interface RegisterGroupStrategy { + + boolean support(GroupType groupType); + + + RegisterGongbaekResponse registerGroup(PrepareRegisterInfo prepareRegisterInfo); +} diff --git a/src/main/java/com/ggang/be/api/group/registry/RegisterGroupStrategyRegistry.java b/src/main/java/com/ggang/be/api/group/registry/RegisterGroupStrategyRegistry.java new file mode 100644 index 0000000..6926a90 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/registry/RegisterGroupStrategyRegistry.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.GroupType; +import com.ggang.be.global.annotation.Registry; +import java.util.List; +import lombok.RequiredArgsConstructor; + +@Registry +@RequiredArgsConstructor +public class RegisterGroupStrategyRegistry { + + private final List registerGroupStrategies; + + public RegisterGroupStrategy getRegisterGroupStrategy(GroupType groupType) { + return registerGroupStrategies.stream() + .filter(strategy -> strategy.support(groupType)) + .findFirst() + .orElseThrow(() -> new GongBaekException(ResponseError.BAD_REQUEST)); + } + +}