Skip to content

Commit

Permalink
[refactor] 모임 신청 취소하기 API 클래스 분리 (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
khyojun authored Jan 19, 2025
2 parents b120887 + 22d908b commit 5727df9
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
46 changes: 9 additions & 37 deletions src/main/java/com/ggang/be/api/group/facade/GroupFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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(
Expand Down Expand Up @@ -283,20 +269,6 @@ private List<GroupVo> getGroupsApply (UserEntity currentUser, boolean status) {
);
}

private void sameSchoolValidateByEveryList(UserEntity userEntity, List<EveryGroupVo> everyGroupResponses){
for (EveryGroupVo everyGroupVo : everyGroupResponses) {
EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId(everyGroupVo.groupId());
sameSchoolValidator.isUserReadMySchoolEveryGroup(userEntity, everyGroupEntity);
}
}

private void sameSchoolValidateByOnceList(UserEntity userEntity, List<OnceGroupVo> 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);

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

public CancelGroupStrategy getCancelGroupStrategy(GroupType groupType){
return cancelGroupStrategies.stream()
.filter(applyGroupStrategy -> applyGroupStrategy.support(groupType))
.findFirst()
.orElseThrow(() -> new GongBaekException(ResponseError.BAD_REQUEST));
}

}

0 comments on commit 5727df9

Please sign in to comment.