Skip to content

Commit

Permalink
[refactor] 등록자 프로필 조회 API 클래스 분리 (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
khyojun authored Jan 19, 2025
2 parents 5727df9 + 7393cc0 commit b682e72
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.ggang.be.api.group.everyGroup.strategy;

import com.ggang.be.api.group.everyGroup.service.EveryGroupService;
import com.ggang.be.api.group.registry.GroupUserInfoStrategy;
import com.ggang.be.domain.constant.GroupType;
import com.ggang.be.global.annotation.Strategy;
import lombok.RequiredArgsConstructor;

@Strategy
@RequiredArgsConstructor
public class EveryGroupUserInfoStrategy implements GroupUserInfoStrategy {

private final EveryGroupService everyGroupService;

@Override
public boolean supports(GroupType groupType) {
return groupType == GroupType.WEEKLY;
}

@Override
public long getGroupUserInfo(long groupId) {
return everyGroupService.getEveryGroupRegisterUserId(groupId);
}
}
8 changes: 3 additions & 5 deletions src/main/java/com/ggang/be/api/group/facade/GroupFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class GroupFacade {
private final GroupInfoStrategyRegistry groupInfoStrategyRegistry;
private final ApplyGroupStrategyRegistry applyGroupStrategyRegistry;
private final CancelGroupStrategyRegistry cancelGroupStrategyRegistry;
private final GroupUserInfoStrategyRegistry groupUserInfoStrategyRegistry;

public GroupResponse getGroupInfo(GroupType groupType, Long groupId, long userId) {
UserEntity currentUser = userService.getUserById(userId);
Expand Down Expand Up @@ -82,12 +83,9 @@ public NearestGroupResponse getNearestGroupInfo(long userId) {
}

public UserInfo getGroupUserInfo(GroupType groupType, Long groupId) {
long userId = switch (groupType) {
case WEEKLY -> everyGroupService.getEveryGroupRegisterUserId(groupId);
case ONCE -> onceGroupService.getOnceGroupRegisterUserId(groupId);
};
GroupUserInfoStrategy groupUserInfoStrategy = groupUserInfoStrategyRegistry.getGroupUserInfo(groupType);

return UserInfo.of(userService.getUserById(userId));
return UserInfo.of(userService.getUserById(groupUserInfoStrategy.getGroupUserInfo(groupId)));
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.ggang.be.api.group.onceGroup.strategy;

import com.ggang.be.api.group.onceGroup.service.OnceGroupService;
import com.ggang.be.api.group.registry.GroupUserInfoStrategy;
import com.ggang.be.domain.constant.GroupType;
import com.ggang.be.global.annotation.Strategy;
import lombok.RequiredArgsConstructor;

@Strategy
@RequiredArgsConstructor
public class OnceGroupUserInfoStrategy implements GroupUserInfoStrategy {

private final OnceGroupService onceGroupService;

@Override
public boolean supports(GroupType groupType) {
return groupType == GroupType.ONCE;
}

@Override
public long getGroupUserInfo(long groupId) {
return onceGroupService.getOnceGroupRegisterUserId(groupId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ggang.be.api.group.registry;

import com.ggang.be.domain.constant.GroupType;

public interface GroupUserInfoStrategy {
boolean supports(GroupType groupType);

long getGroupUserInfo(long groupId);
}
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 GroupUserInfoStrategyRegistry {

private final List<GroupUserInfoStrategy> groupUserInfoStrategies;

public GroupUserInfoStrategy getGroupUserInfo(GroupType groupType) {
return groupUserInfoStrategies.stream()
.filter(strategy -> strategy.supports(groupType))
.findFirst()
.orElseThrow(() -> new GongBaekException(ResponseError.BAD_REQUEST));
}
}

0 comments on commit b682e72

Please sign in to comment.