Skip to content

Commit

Permalink
[refactor] #144 나의 모임 전체 조회 API 클래스 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
2hyunjinn committed Jan 19, 2025
1 parent 443dcec commit caf2fc9
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 36 deletions.
41 changes: 6 additions & 35 deletions src/main/java/com/ggang/be/api/group/facade/GroupFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -51,6 +50,7 @@ public class GroupFacade {
private final PrepareRegisterGongbaekFacade prepareRegisterGongbaekFacade;
private final CancelGroupStrategyRegistry cancelGroupStrategyRegistry;
private final GroupUserInfoStrategyRegistry groupUserInfoStrategyRegistry;
private final MyGroupStrategyRegistry myGroupStrategyRegistry;

public GroupResponse getGroupInfo(GroupType groupType, Long groupId, long userId) {
UserEntity currentUser = userService.getUserById(userId);
Expand Down Expand Up @@ -122,14 +122,13 @@ public void cancelMyApplication(Long userId, GroupRequest requestDto) {
public List<MyGroupResponse> getMyGroups(long userId, FillGroupFilterRequest filterRequestDto) {
UserEntity currentUser = userService.getUserById(userId);

List<GroupVo> groupResponses = switch (filterRequestDto.getFillGroupCategory()) {
case REGISTER -> getGroupsRegister(currentUser, filterRequestDto.status());
case APPLY -> getGroupsApply(currentUser, filterRequestDto.status());
};
MyGroupStrategy groupStrategy = myGroupStrategyRegistry.getGroupStrategy(filterRequestDto.getFillGroupCategory());

List<GroupVo> groupResponses = groupStrategy.getGroups(currentUser, filterRequestDto.status());

return groupResponses.stream()
.map(MyGroupResponse::fromGroupVo)
.collect(Collectors.toList());
.map(MyGroupResponse::fromGroupVo)
.collect(Collectors.toList());
}

public List<ActiveGroupsResponse> getFillGroups(long userId, Category category) {
Expand Down Expand Up @@ -217,34 +216,6 @@ private NearestGroupResponse getNearestGroupFromDates(NearestEveryGroup nearestE
}
}

private List<GroupVo> getGroupsRegister(UserEntity currentUser, boolean status) {
List<EveryGroupVo> everyGroupResponses = everyGroupService.getMyRegisteredGroups(
currentUser, status).groups();
List<OnceGroupVo> onceGroupResponses = onceGroupService.getMyRegisteredGroups(currentUser,
status).groups();

return Stream.concat(
everyGroupResponses.stream().map(GroupVo::fromEveryGroup),
onceGroupResponses.stream().map(GroupVo::fromOnceGroup))
.sorted(Comparator.comparing(GroupVo::createdAt).reversed())
.collect(Collectors.toList()
);
}

private List<GroupVo> getGroupsApply(UserEntity currentUser, boolean status) {
List<EveryGroupVo> everyGroupResponses = userEveryGroupService.getMyAppliedGroups(
currentUser, status).groups();
List<OnceGroupVo> onceGroupResponses = userOnceGroupService.getMyAppliedGroups(currentUser,
status).groups();

return Stream.concat(
everyGroupResponses.stream().map(GroupVo::fromEveryGroup),
onceGroupResponses.stream().map(GroupVo::fromOnceGroup))
.sorted((group1, group2) -> group2.createdAt().compareTo(group1.createdAt()))
.collect(Collectors.toList()
);
}

public ReadFillMembersResponse getGroupUsersInfo(Long userId, ReadFillMembersRequest dto) {
UserEntity findUserEntity = userService.getUserById(userId);

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/ggang/be/api/group/registry/MyGroupStrategy.java
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.domain.constant.FillGroupType;
import com.ggang.be.domain.group.dto.GroupVo;
import com.ggang.be.domain.user.UserEntity;

import java.util.List;

public interface MyGroupStrategy {
boolean supports(FillGroupType category);

List<GroupVo> getGroups(UserEntity userEntity, boolean status);
}
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.FillGroupType;
import com.ggang.be.global.annotation.Registry;
import lombok.RequiredArgsConstructor;

import java.util.List;

@Registry
@RequiredArgsConstructor
public class MyGroupStrategyRegistry {

private final List<MyGroupStrategy> groupStrategies;

public MyGroupStrategy getGroupStrategy(FillGroupType category) {
return groupStrategies.stream()
.filter(strategy -> strategy.supports(category))
.findFirst()
.orElseThrow(() -> new GongBaekException(ResponseError.BAD_REQUEST));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ public interface RegisterGroupStrategy {

boolean support(GroupType groupType);


RegisterGongbaekResponse registerGroup(PrepareRegisterInfo prepareRegisterInfo);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.ggang.be.api.group.strategy;

import com.ggang.be.api.group.registry.MyGroupStrategy;
import com.ggang.be.api.userEveryGroup.service.UserEveryGroupService;
import com.ggang.be.api.userOnceGroup.service.UserOnceGroupService;
import com.ggang.be.domain.constant.FillGroupType;
import com.ggang.be.domain.group.dto.GroupVo;
import com.ggang.be.domain.group.everyGroup.dto.EveryGroupVo;
import com.ggang.be.domain.group.onceGroup.dto.OnceGroupVo;
import com.ggang.be.domain.user.UserEntity;
import com.ggang.be.global.util.GroupVoAggregator;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class MyApplyGroupStrategy implements MyGroupStrategy {

private final UserEveryGroupService userEveryGroupService;
private final UserOnceGroupService userOnceGroupService;

@Override
public boolean supports(FillGroupType category) {
return category == FillGroupType.APPLY;
}

@Override
public List<GroupVo> getGroups(UserEntity userEntity, boolean status) {
List<EveryGroupVo> everyGroupResponses = userEveryGroupService.getMyAppliedGroups(userEntity, status).groups();
List<OnceGroupVo> onceGroupResponses = userOnceGroupService.getMyAppliedGroups(userEntity, status).groups();

return GroupVoAggregator.aggregateAndSort(everyGroupResponses, onceGroupResponses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.ggang.be.api.group.strategy;

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.MyGroupStrategy;
import com.ggang.be.domain.constant.FillGroupType;
import com.ggang.be.domain.group.dto.GroupVo;
import com.ggang.be.domain.group.everyGroup.dto.EveryGroupVo;
import com.ggang.be.domain.group.onceGroup.dto.OnceGroupVo;
import com.ggang.be.domain.user.UserEntity;
import com.ggang.be.global.util.GroupVoAggregator;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class MyRegisterGroupStrategy implements MyGroupStrategy {

private final EveryGroupService everyGroupService;
private final OnceGroupService onceGroupService;

@Override
public boolean supports(FillGroupType category) {
return category == FillGroupType.REGISTER;
}

@Override
public List<GroupVo> getGroups(UserEntity userEntity, boolean status) {
List<EveryGroupVo> everyGroupResponses = everyGroupService.getMyRegisteredGroups(userEntity, status).groups();
List<OnceGroupVo> onceGroupResponses = onceGroupService.getMyRegisteredGroups(userEntity, status).groups();

return GroupVoAggregator.aggregateAndSort(everyGroupResponses, onceGroupResponses);
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/ggang/be/global/util/GroupVoAggregator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ggang.be.global.util;

import com.ggang.be.domain.group.dto.GroupVo;
import com.ggang.be.domain.group.everyGroup.dto.EveryGroupVo;
import com.ggang.be.domain.group.onceGroup.dto.OnceGroupVo;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GroupVoAggregator {

public static List<GroupVo> aggregateAndSort(List<EveryGroupVo> everyGroupResponses, List<OnceGroupVo> onceGroupResponses) {
return Stream.concat(
everyGroupResponses.stream().map(GroupVo::fromEveryGroup),
onceGroupResponses.stream().map(GroupVo::fromOnceGroup)
)
.sorted((group1, group2) -> group2.createdAt().compareTo(group1.createdAt()))
.collect(Collectors.toList());
}
}

0 comments on commit caf2fc9

Please sign in to comment.