Skip to content

Commit

Permalink
[refactor] 모임 신청 API registry, strategy 기반으로 클래스 분리 진행 (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
khyojun authored Jan 19, 2025
2 parents 37dd0e5 + 672cb80 commit b120887
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.ApplyGroupStrategy;
import com.ggang.be.api.lectureTimeSlot.service.LectureTimeSlotService;
import com.ggang.be.api.userEveryGroup.service.UserEveryGroupService;
import com.ggang.be.domain.common.SameSchoolValidator;
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.user.UserEntity;
import com.ggang.be.global.annotation.Strategy;
import lombok.RequiredArgsConstructor;

@Strategy
@RequiredArgsConstructor
public class ApplyEveryGroupStrategy implements ApplyGroupStrategy {

private final EveryGroupService everyGroupService;
private final SameSchoolValidator sameSchoolValidator;
private final UserEveryGroupService userEveryGroupService;
private final LectureTimeSlotService lectureTimeSlotService;

@Override
public boolean support(GroupType groupType) {
return groupType.equals(GroupType.WEEKLY);
}

@Override
public void applyGroup(UserEntity findUserEntity, GroupRequest request) {
EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId(request.groupId());
sameSchoolValidator.isUserReadMySchoolEveryGroup(findUserEntity, everyGroupEntity);
everyGroupService.validateApplyEveryGroup(findUserEntity, everyGroupEntity);
GroupVo groupVo = GroupVo.fromEveryGroup(EveryGroupVo.of(everyGroupEntity));

if(checkGroupsLectureTimeSlot(findUserEntity, groupVo))
userEveryGroupService.applyEveryGroup(findUserEntity, everyGroupEntity);
else throw new GongBaekException(ResponseError.TIME_SLOT_ALREADY_EXIST);
}

private boolean checkGroupsLectureTimeSlot(UserEntity findUserEntity, GroupVo groupVo) {
return lectureTimeSlotService.isActiveGroupsInLectureTimeSlot(
findUserEntity,
groupVo.startTime(),
groupVo.endTime(),
groupVo.weekDate()
);
}

}
32 changes: 10 additions & 22 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,6 +7,12 @@
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;
Expand All @@ -31,7 +37,6 @@
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 @@ -54,6 +59,7 @@ public class GroupFacade {
private final LatestGroupStrategyRegistry latestGroupStrategyRegistry;
private final ReadFillMemberStrategyRegistry readFillMemberStrategyRegistry;
private final GroupInfoStrategyRegistry groupInfoStrategyRegistry;
private final ApplyGroupStrategyRegistry applyGroupStrategyRegistry;

public GroupResponse getGroupInfo(GroupType groupType, Long groupId, long userId) {
UserEntity currentUser = userService.getUserById(userId);
Expand Down Expand Up @@ -121,28 +127,10 @@ public RegisterGongbaekResponse registerGongbaek(Long userId, RegisterGongbaekRe
public void applyGroup(Long userId, GroupRequest requestDto) {
UserEntity findUserEntity = userService.getUserById(userId);

switch (requestDto.groupType()){
case WEEKLY -> {
EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId(requestDto.groupId());
sameSchoolValidator.isUserReadMySchoolEveryGroup(findUserEntity, everyGroupEntity);
everyGroupService.validateApplyEveryGroup(findUserEntity, everyGroupEntity);
GroupVo groupVo = GroupVo.fromEveryGroup(EveryGroupVo.of(everyGroupEntity));

if(checkGroupsLectureTimeSlot(findUserEntity, groupVo))
userEveryGroupService.applyEveryGroup(findUserEntity, everyGroupEntity);
else throw new GongBaekException(ResponseError.TIME_SLOT_ALREADY_EXIST);
}
case ONCE -> {
OnceGroupEntity onceGroupEntity = onceGroupService.findOnceGroupEntityByGroupId(requestDto.groupId());
sameSchoolValidator.isUserReadMySchoolOnceGroup(findUserEntity, onceGroupEntity);
onceGroupService.validateApplyOnceGroup(findUserEntity, onceGroupEntity);
GroupVo groupVo = GroupVo.fromOnceGroup(OnceGroupVo.of(onceGroupEntity));
ApplyGroupStrategy applyGroupStrategy = applyGroupStrategyRegistry.getApplyGroupStrategy(
requestDto.groupType());

if(checkGroupsLectureTimeSlot(findUserEntity, groupVo))
userOnceGroupService.applyOnceGroup(findUserEntity, onceGroupEntity);
else throw new GongBaekException(ResponseError.TIME_SLOT_ALREADY_EXIST);
}
}
applyGroupStrategy.applyGroup(findUserEntity, requestDto);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.ApplyGroupStrategy;
import com.ggang.be.api.lectureTimeSlot.service.LectureTimeSlotService;
import com.ggang.be.api.userOnceGroup.service.UserOnceGroupService;
import com.ggang.be.domain.common.SameSchoolValidator;
import com.ggang.be.domain.constant.GroupType;
import com.ggang.be.domain.group.dto.GroupVo;
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.Strategy;
import lombok.RequiredArgsConstructor;

@Strategy
@RequiredArgsConstructor
public class ApplyOnceGroupStrategy implements ApplyGroupStrategy {

private final OnceGroupService onceGroupService;
private final SameSchoolValidator sameSchoolValidator;
private final UserOnceGroupService userOnceGroupService;
private final LectureTimeSlotService lectureTimeSlotService;

@Override
public boolean support(GroupType groupType) {
return groupType.equals(GroupType.ONCE);
}

@Override
public void applyGroup(UserEntity findUserEntity, GroupRequest request) {
OnceGroupEntity onceGroupEntity = onceGroupService.findOnceGroupEntityByGroupId(request.groupId());
sameSchoolValidator.isUserReadMySchoolOnceGroup(findUserEntity, onceGroupEntity);
onceGroupService.validateApplyOnceGroup(findUserEntity, onceGroupEntity);
GroupVo groupVo = GroupVo.fromOnceGroup(OnceGroupVo.of(onceGroupEntity));

if(checkGroupsLectureTimeSlot(findUserEntity, groupVo))
userOnceGroupService.applyOnceGroup(findUserEntity, onceGroupEntity);
else throw new GongBaekException(ResponseError.TIME_SLOT_ALREADY_EXIST);
}

private boolean checkGroupsLectureTimeSlot(UserEntity findUserEntity, GroupVo groupVo) {
return lectureTimeSlotService.isActiveGroupsInLectureTimeSlot(
findUserEntity,
groupVo.startTime(),
groupVo.endTime(),
groupVo.weekDate()
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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 ApplyGroupStrategy {

boolean support(GroupType groupType);

void applyGroup(UserEntity userEntity, GroupRequest request);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 ApplyGroupStrategyRegistry {

private final List<ApplyGroupStrategy> applyGroupStrategies;


public ApplyGroupStrategy getApplyGroupStrategy(GroupType groupType){
return applyGroupStrategies.stream()
.filter(applyGroupStrategy -> applyGroupStrategy.support(groupType))
.findFirst()
.orElseThrow(() -> new GongBaekException(ResponseError.BAD_REQUEST));
}




}

0 comments on commit b120887

Please sign in to comment.