Skip to content

Commit

Permalink
[refactor] #138 registry, strategy 분리 진행 및 dto 준비 facade 분리해서 진행
Browse files Browse the repository at this point in the history
  • Loading branch information
khyojun committed Jan 19, 2025
1 parent b120887 commit 21c6b5a
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 94 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/ggang/be/api/group/dto/PrepareRegisterInfo.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}


}
191 changes: 97 additions & 94 deletions src/main/java/com/ggang/be/api/group/facade/GroupFacade.java

Large diffs are not rendered by default.

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


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


}
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.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);
}
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 java.util.List;
import lombok.RequiredArgsConstructor;

@Registry
@RequiredArgsConstructor
public class RegisterGroupStrategyRegistry {

private final List<RegisterGroupStrategy> registerGroupStrategies;

public RegisterGroupStrategy getRegisterGroupStrategy(GroupType groupType) {
return registerGroupStrategies.stream()
.filter(strategy -> strategy.support(groupType))
.findFirst()
.orElseThrow(() -> new GongBaekException(ResponseError.BAD_REQUEST));
}

}

0 comments on commit 21c6b5a

Please sign in to comment.