diff --git a/src/main/java/com/ggang/be/api/group/everyGroup/strategy/CancelEveryGroupStrategy.java b/src/main/java/com/ggang/be/api/group/everyGroup/strategy/CancelEveryGroupStrategy.java new file mode 100644 index 0000000..6944da8 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/everyGroup/strategy/CancelEveryGroupStrategy.java @@ -0,0 +1,33 @@ +package com.ggang.be.api.group.everyGroup.strategy; + +import com.ggang.be.api.common.ResponseError; +import com.ggang.be.api.exception.GongBaekException; +import com.ggang.be.api.group.dto.GroupRequest; +import com.ggang.be.api.group.everyGroup.service.EveryGroupService; +import com.ggang.be.api.group.registry.CancelGroupStrategy; +import com.ggang.be.api.userEveryGroup.service.UserEveryGroupService; +import com.ggang.be.domain.constant.GroupType; +import com.ggang.be.domain.group.everyGroup.EveryGroupEntity; +import com.ggang.be.domain.user.UserEntity; +import com.ggang.be.global.annotation.Strategy; +import lombok.RequiredArgsConstructor; + +@Strategy +@RequiredArgsConstructor +public class CancelEveryGroupStrategy implements CancelGroupStrategy { + private final EveryGroupService everyGroupService; + private final UserEveryGroupService userEveryGroupService; + + @Override + public boolean support(GroupType groupType) { + return groupType.equals(GroupType.WEEKLY); + } + + @Override + public void cancelGroup(UserEntity userEntity, GroupRequest request) { + EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId(request.groupId()); + if (everyGroupService.validateCancelEveryGroup(userEntity, everyGroupEntity)) + userEveryGroupService.cancelEveryGroup(userEntity, everyGroupEntity); + else throw new GongBaekException(ResponseError.GROUP_CANCEL_NOT_FOUND); + } +} 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..8e4c727 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 @@ -7,18 +7,11 @@ 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.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.lectureTimeSlot.service.LectureTimeSlotService; import com.ggang.be.api.mapper.GroupResponseMapper; import com.ggang.be.api.user.service.UserService; import com.ggang.be.api.userEveryGroup.service.UserEveryGroupService; import com.ggang.be.api.userOnceGroup.service.UserOnceGroupService; -import com.ggang.be.domain.common.SameSchoolValidator; import com.ggang.be.domain.constant.Category; import com.ggang.be.domain.constant.GroupType; import com.ggang.be.domain.group.dto.GroupVo; @@ -37,6 +30,7 @@ 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; @@ -55,11 +49,11 @@ public class GroupFacade { 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 CancelGroupStrategyRegistry cancelGroupStrategyRegistry; public GroupResponse getGroupInfo(GroupType groupType, Long groupId, long userId) { UserEntity currentUser = userService.getUserById(userId); @@ -128,7 +122,8 @@ public void applyGroup(Long userId, GroupRequest requestDto) { UserEntity findUserEntity = userService.getUserById(userId); ApplyGroupStrategy applyGroupStrategy = applyGroupStrategyRegistry.getApplyGroupStrategy( - requestDto.groupType()); + requestDto.groupType() + ); applyGroupStrategy.applyGroup(findUserEntity, requestDto); } @@ -137,20 +132,11 @@ public void applyGroup(Long userId, GroupRequest requestDto) { public void cancelMyApplication(Long userId, GroupRequest requestDto) { UserEntity findUserEntity = userService.getUserById(userId); - switch (requestDto.groupType()){ - case WEEKLY -> { - EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId(requestDto.groupId()); - if(everyGroupService.validateCancelEveryGroup(findUserEntity, everyGroupEntity)) - userEveryGroupService.cancelEveryGroup(findUserEntity, everyGroupEntity); - else throw new GongBaekException(ResponseError.GROUP_CANCEL_NOT_FOUND); - } - case ONCE -> { - OnceGroupEntity onceGroupEntity = onceGroupService.findOnceGroupEntityByGroupId(requestDto.groupId()); - if(onceGroupService.validateCancelOnceGroup(findUserEntity, onceGroupEntity)) - userOnceGroupService.cancelOnceGroup(findUserEntity, onceGroupEntity); - else throw new GongBaekException(ResponseError.GROUP_CANCEL_NOT_FOUND); - } - } + CancelGroupStrategy cancelGroupStrategy = cancelGroupStrategyRegistry.getCancelGroupStrategy( + requestDto.groupType() + ); + + cancelGroupStrategy.cancelGroup(findUserEntity, requestDto); } private RegisterGroupServiceRequest convertDtoToServiceDto( @@ -283,20 +269,6 @@ private List getGroupsApply (UserEntity currentUser, boolean status) { ); } - private void sameSchoolValidateByEveryList(UserEntity userEntity, List everyGroupResponses){ - for (EveryGroupVo everyGroupVo : everyGroupResponses) { - EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId(everyGroupVo.groupId()); - sameSchoolValidator.isUserReadMySchoolEveryGroup(userEntity, everyGroupEntity); - } - } - - private void sameSchoolValidateByOnceList(UserEntity userEntity, List onceGroupResponses){ - for (OnceGroupVo onceGroupVo : onceGroupResponses) { - OnceGroupEntity onceGroupEntity = onceGroupService.findOnceGroupEntityByGroupId(onceGroupVo.groupId()); - sameSchoolValidator.isUserReadMySchoolOnceGroup(userEntity, onceGroupEntity); - } - } - public ReadFillMembersResponse getGroupUsersInfo(Long userId, ReadFillMembersRequest dto) { UserEntity findUserEntity = userService.getUserById(userId); diff --git a/src/main/java/com/ggang/be/api/group/onceGroup/strategy/CancelOnceGroupStrategy.java b/src/main/java/com/ggang/be/api/group/onceGroup/strategy/CancelOnceGroupStrategy.java new file mode 100644 index 0000000..1d674e8 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/onceGroup/strategy/CancelOnceGroupStrategy.java @@ -0,0 +1,33 @@ +package com.ggang.be.api.group.onceGroup.strategy; + +import com.ggang.be.api.common.ResponseError; +import com.ggang.be.api.exception.GongBaekException; +import com.ggang.be.api.group.dto.GroupRequest; +import com.ggang.be.api.group.onceGroup.service.OnceGroupService; +import com.ggang.be.api.group.registry.CancelGroupStrategy; +import com.ggang.be.api.userOnceGroup.service.UserOnceGroupService; +import com.ggang.be.domain.constant.GroupType; +import com.ggang.be.domain.group.onceGroup.OnceGroupEntity; +import com.ggang.be.domain.user.UserEntity; +import com.ggang.be.global.annotation.Strategy; +import lombok.RequiredArgsConstructor; + +@Strategy +@RequiredArgsConstructor +public class CancelOnceGroupStrategy implements CancelGroupStrategy { + private final OnceGroupService onceGroupService; + private final UserOnceGroupService userOnceGroupService; + + @Override + public boolean support(GroupType groupType) { + return groupType.equals(GroupType.ONCE); + } + + @Override + public void cancelGroup(UserEntity userEntity, GroupRequest request) { + OnceGroupEntity onceGroupEntity = onceGroupService.findOnceGroupEntityByGroupId(request.groupId()); + if (onceGroupService.validateCancelOnceGroup(userEntity, onceGroupEntity)) + userOnceGroupService.cancelOnceGroup(userEntity, onceGroupEntity); + else throw new GongBaekException(ResponseError.GROUP_CANCEL_NOT_FOUND); + } +} diff --git a/src/main/java/com/ggang/be/api/group/registry/CancelGroupStrategy.java b/src/main/java/com/ggang/be/api/group/registry/CancelGroupStrategy.java new file mode 100644 index 0000000..0e7c4e8 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/registry/CancelGroupStrategy.java @@ -0,0 +1,11 @@ +package com.ggang.be.api.group.registry; + +import com.ggang.be.api.group.dto.GroupRequest; +import com.ggang.be.domain.constant.GroupType; +import com.ggang.be.domain.user.UserEntity; + +public interface CancelGroupStrategy { + boolean support(GroupType groupType); + + void cancelGroup(UserEntity userEntity, GroupRequest request); +} diff --git a/src/main/java/com/ggang/be/api/group/registry/CancelGroupStrategyRegistry.java b/src/main/java/com/ggang/be/api/group/registry/CancelGroupStrategyRegistry.java new file mode 100644 index 0000000..e615f38 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/registry/CancelGroupStrategyRegistry.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 lombok.RequiredArgsConstructor; + +import java.util.List; + +@Registry +@RequiredArgsConstructor +public class CancelGroupStrategyRegistry { + private final List cancelGroupStrategies; + + public CancelGroupStrategy getCancelGroupStrategy(GroupType groupType){ + return cancelGroupStrategies.stream() + .filter(applyGroupStrategy -> applyGroupStrategy.support(groupType)) + .findFirst() + .orElseThrow(() -> new GongBaekException(ResponseError.BAD_REQUEST)); + } + +}