From da9734a62885780bf5d4d4f6a7ae3ca83fa1ca24 Mon Sep 17 00:00:00 2001 From: 2hyunjinn Date: Thu, 23 Jan 2025 22:02:57 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20#177=20=EB=82=98=EC=9D=98=20=EB=AA=A8?= =?UTF-8?q?=EC=9E=84=20=EC=82=AD=EC=A0=9C=ED=95=98=EA=B8=B0=20API=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/group/controller/GroupController.java | 11 ++++++++ .../everyGroup/service/EveryGroupService.java | 4 ++- .../strategy/DeleteEveryGroupStrategy.java | 27 +++++++++++++++++++ .../be/api/group/facade/GroupFacade.java | 12 +++++++++ .../onceGroup/service/OnceGroupService.java | 2 ++ .../strategy/DeleteOnceGroupStrategy.java | 27 +++++++++++++++++++ .../group/registry/DeleteGroupStrategy.java | 11 ++++++++ .../registry/DeleteGroupStrategyRegistry.java | 22 +++++++++++++++ .../application/EveryGroupServiceImpl.java | 19 ++++++++++--- .../application/OnceGroupServiceImpl.java | 12 +++++++++ 10 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/ggang/be/api/group/everyGroup/strategy/DeleteEveryGroupStrategy.java create mode 100644 src/main/java/com/ggang/be/api/group/onceGroup/strategy/DeleteOnceGroupStrategy.java create mode 100644 src/main/java/com/ggang/be/api/group/registry/DeleteGroupStrategy.java create mode 100644 src/main/java/com/ggang/be/api/group/registry/DeleteGroupStrategyRegistry.java diff --git a/src/main/java/com/ggang/be/api/group/controller/GroupController.java b/src/main/java/com/ggang/be/api/group/controller/GroupController.java index 55fc6e6..fafdbce 100644 --- a/src/main/java/com/ggang/be/api/group/controller/GroupController.java +++ b/src/main/java/com/ggang/be/api/group/controller/GroupController.java @@ -149,4 +149,15 @@ public ResponseEntity> cancelMyApplication( return ResponseBuilder.ok(null); } + + @DeleteMapping("/my/groups") + public ResponseEntity> deleteMyGroup( + @RequestHeader("Authorization") String accessToken, + @RequestBody final GroupRequest requestDto + ) { + Long userId = jwtService.parseTokenAndGetUserId(accessToken); + groupFacade.deleteMyGroup(userId, requestDto); + + return ResponseBuilder.ok(null); + } } diff --git a/src/main/java/com/ggang/be/api/group/everyGroup/service/EveryGroupService.java b/src/main/java/com/ggang/be/api/group/everyGroup/service/EveryGroupService.java index 1197bd0..1985391 100644 --- a/src/main/java/com/ggang/be/api/group/everyGroup/service/EveryGroupService.java +++ b/src/main/java/com/ggang/be/api/group/everyGroup/service/EveryGroupService.java @@ -24,7 +24,9 @@ public interface EveryGroupService { ReadCommentGroup readCommentInGroup(UserEntity userEntity, boolean commentEntity, final long groupId); EveryGroupEntity registerEveryGroup(RegisterGroupServiceRequest serviceRequest, - GongbaekTimeSlotEntity gongbaekTimeSlotEntity); + GongbaekTimeSlotEntity gongbaekTimeSlotEntity); + + void deleteEveryGroup(UserEntity currentUser, EveryGroupEntity everyGroupEntity); ReadEveryGroup getActiveEveryGroups(UserEntity currentUser, Category category); diff --git a/src/main/java/com/ggang/be/api/group/everyGroup/strategy/DeleteEveryGroupStrategy.java b/src/main/java/com/ggang/be/api/group/everyGroup/strategy/DeleteEveryGroupStrategy.java new file mode 100644 index 0000000..3e02abc --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/everyGroup/strategy/DeleteEveryGroupStrategy.java @@ -0,0 +1,27 @@ +package com.ggang.be.api.group.everyGroup.strategy; + +import com.ggang.be.api.group.dto.GroupRequest; +import com.ggang.be.api.group.everyGroup.service.EveryGroupService; +import com.ggang.be.api.group.registry.DeleteGroupStrategy; +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 DeleteEveryGroupStrategy implements DeleteGroupStrategy { + private final EveryGroupService everyGroupService; + + @Override + public boolean support(GroupType groupType) { + return groupType.equals(GroupType.WEEKLY); + } + + @Override + public void deleteGroup(UserEntity userEntity, GroupRequest request) { + EveryGroupEntity everyGroupEntity = everyGroupService.findEveryGroupEntityByGroupId(request.groupId()); + everyGroupService.deleteEveryGroup(userEntity, everyGroupEntity); + } +} diff --git a/src/main/java/com/ggang/be/api/group/facade/GroupFacade.java b/src/main/java/com/ggang/be/api/group/facade/GroupFacade.java index b29e92c..0ef0ba9 100644 --- a/src/main/java/com/ggang/be/api/group/facade/GroupFacade.java +++ b/src/main/java/com/ggang/be/api/group/facade/GroupFacade.java @@ -38,6 +38,7 @@ public class GroupFacade { private final RegisterGroupStrategyRegistry registerGroupStrategyRegistry; private final PrepareRegisterGongbaekFacade prepareRegisterGongbaekFacade; private final CancelGroupStrategyRegistry cancelGroupStrategyRegistry; + private final DeleteGroupStrategyRegistry deleteGroupStrategyRegistry; private final GroupUserInfoStrategyRegistry groupUserInfoStrategyRegistry; private final ActiveCombinedGroupVoPreparer activeCombinedGroupVoPreparer; private final MyGroupStrategyRegistry myGroupStrategyRegistry; @@ -107,6 +108,17 @@ public void cancelMyApplication(Long userId, GroupRequest requestDto) { cancelGroupStrategy.cancelGroup(findUserEntity, requestDto); } + @Transactional + public void deleteMyGroup(Long userId, GroupRequest requestDto) { + UserEntity findUserEntity = userService.getUserById(userId); + + DeleteGroupStrategy deleteGroupStrategy = deleteGroupStrategyRegistry.getDeleteGroupStrategy( + requestDto.groupType() + ); + + deleteGroupStrategy.deleteGroup(findUserEntity, requestDto); + } + public List getMyGroups(long userId, FillGroupType category, boolean status) { UserEntity currentUser = userService.getUserById(userId); diff --git a/src/main/java/com/ggang/be/api/group/onceGroup/service/OnceGroupService.java b/src/main/java/com/ggang/be/api/group/onceGroup/service/OnceGroupService.java index c66fccd..ceb8ffb 100644 --- a/src/main/java/com/ggang/be/api/group/onceGroup/service/OnceGroupService.java +++ b/src/main/java/com/ggang/be/api/group/onceGroup/service/OnceGroupService.java @@ -26,6 +26,8 @@ public interface OnceGroupService { OnceGroupEntity registerOnceGroup(RegisterGroupServiceRequest serviceRequest, GongbaekTimeSlotEntity gongbaekTimeSlotEntity); + void deleteOnceGroup(UserEntity currentUser, OnceGroupEntity onceGroupEntity); + ReadOnceGroup getActiveOnceGroups(UserEntity currentUser, Category category); void validateApplyOnceGroup(UserEntity currentUser, OnceGroupEntity onceGroupEntity); diff --git a/src/main/java/com/ggang/be/api/group/onceGroup/strategy/DeleteOnceGroupStrategy.java b/src/main/java/com/ggang/be/api/group/onceGroup/strategy/DeleteOnceGroupStrategy.java new file mode 100644 index 0000000..6b13473 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/onceGroup/strategy/DeleteOnceGroupStrategy.java @@ -0,0 +1,27 @@ +package com.ggang.be.api.group.onceGroup.strategy; + +import com.ggang.be.api.group.dto.GroupRequest; +import com.ggang.be.api.group.onceGroup.service.OnceGroupService; +import com.ggang.be.api.group.registry.DeleteGroupStrategy; +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 DeleteOnceGroupStrategy implements DeleteGroupStrategy { + private final OnceGroupService onceGroupService; + + @Override + public boolean support(GroupType groupType) { + return groupType.equals(GroupType.ONCE); + } + + @Override + public void deleteGroup(UserEntity userEntity, GroupRequest request) { + OnceGroupEntity onceGroupEntity = onceGroupService.findOnceGroupEntityByGroupId(request.groupId()); + onceGroupService.deleteOnceGroup(userEntity, onceGroupEntity); + } +} diff --git a/src/main/java/com/ggang/be/api/group/registry/DeleteGroupStrategy.java b/src/main/java/com/ggang/be/api/group/registry/DeleteGroupStrategy.java new file mode 100644 index 0000000..192c167 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/registry/DeleteGroupStrategy.java @@ -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 DeleteGroupStrategy { + boolean support(GroupType groupType); + + void deleteGroup(UserEntity userEntity, GroupRequest request); +} diff --git a/src/main/java/com/ggang/be/api/group/registry/DeleteGroupStrategyRegistry.java b/src/main/java/com/ggang/be/api/group/registry/DeleteGroupStrategyRegistry.java new file mode 100644 index 0000000..737b910 --- /dev/null +++ b/src/main/java/com/ggang/be/api/group/registry/DeleteGroupStrategyRegistry.java @@ -0,0 +1,22 @@ +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 DeleteGroupStrategyRegistry { + private final List deleteGroupStrategies; + + public DeleteGroupStrategy getDeleteGroupStrategy(GroupType groupType) { + return deleteGroupStrategies.stream() + .filter(applyGroupStrategy -> applyGroupStrategy.support(groupType)) + .findFirst() + .orElseThrow(() -> new GongBaekException(ResponseError.BAD_REQUEST)); + } +} diff --git a/src/main/java/com/ggang/be/domain/group/everyGroup/application/EveryGroupServiceImpl.java b/src/main/java/com/ggang/be/domain/group/everyGroup/application/EveryGroupServiceImpl.java index 9aece1c..a4a8b53 100644 --- a/src/main/java/com/ggang/be/domain/group/everyGroup/application/EveryGroupServiceImpl.java +++ b/src/main/java/com/ggang/be/domain/group/everyGroup/application/EveryGroupServiceImpl.java @@ -19,14 +19,14 @@ import com.ggang.be.domain.group.vo.ReadCommentGroup; import com.ggang.be.domain.timslot.gongbaekTimeSlot.GongbaekTimeSlotEntity; import com.ggang.be.domain.user.UserEntity; -import java.time.LocalDate; -import java.time.Month; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDate; +import java.time.Month; import java.util.List; import java.util.stream.Collectors; @@ -143,6 +143,13 @@ public EveryGroupEntity registerEveryGroup(RegisterGroupServiceRequest serviceRe return everyGroupRepository.save(buildEntity); } + @Override + @Transactional + public void deleteEveryGroup(UserEntity currentUser, EveryGroupEntity everyGroupEntity) { + validateDeleteEveryGroup(currentUser, everyGroupEntity); + everyGroupRepository.delete(everyGroupEntity); + } + @Override @Transactional public void validateApplyEveryGroup(UserEntity currentUser, EveryGroupEntity everyGroupEntity) { @@ -166,6 +173,10 @@ public void updateStatus() { everyGroupEntities.forEach(groupStatusUpdater::updateEveryGroup); } + private void validateDeleteEveryGroup(UserEntity currentUser, EveryGroupEntity everyGroupEntity) { + if (!everyGroupEntity.isHost(currentUser)) + throw new GongBaekException(ResponseError.UNAUTHORIZED_ACCESS); + } private EveryGroupEntity buildEveryGroupEntity(RegisterGroupServiceRequest serviceRequest, GongbaekTimeSlotEntity gongbaekTimeSlotEntity) { @@ -175,7 +186,7 @@ private EveryGroupEntity buildEveryGroupEntity(RegisterGroupServiceRequest servi return EveryGroupEntity.builder() .category(serviceRequest.category()) - .dueDate(dueDateExtracter(month, nowDate)) + .dueDate(dueDateExtractor(month, nowDate)) .coverImg(serviceRequest.coverImg()) .location(serviceRequest.location()) .status(Status.RECRUITING) @@ -187,7 +198,7 @@ private EveryGroupEntity buildEveryGroupEntity(RegisterGroupServiceRequest servi .build(); } - private LocalDate dueDateExtracter(int month, LocalDate nowDate) { + private LocalDate dueDateExtractor(int month, LocalDate nowDate) { if(month < 7) return LocalDate.of(nowDate.getYear(), Month.JUNE, 30); return LocalDate.of(nowDate.getYear(), Month.DECEMBER, 31); diff --git a/src/main/java/com/ggang/be/domain/group/onceGroup/application/OnceGroupServiceImpl.java b/src/main/java/com/ggang/be/domain/group/onceGroup/application/OnceGroupServiceImpl.java index 16fb7a3..f3fbc10 100644 --- a/src/main/java/com/ggang/be/domain/group/onceGroup/application/OnceGroupServiceImpl.java +++ b/src/main/java/com/ggang/be/domain/group/onceGroup/application/OnceGroupServiceImpl.java @@ -130,6 +130,13 @@ public OnceGroupEntity registerOnceGroup(RegisterGroupServiceRequest serviceRequ return onceGroupRepository.save(buildEntity); } + @Override + @Transactional + public void deleteOnceGroup(UserEntity currentUser, OnceGroupEntity onceGroupEntity) { + validateDeleteOnceGroup(currentUser, onceGroupEntity); + onceGroupRepository.delete(onceGroupEntity); + } + @Override @Transactional public void validateApplyOnceGroup(UserEntity currentUser, OnceGroupEntity onceGroupEntity){ @@ -155,6 +162,11 @@ public void updateStatus() { .forEach(groupStatusUpdater::updateOnceGroup); } + private void validateDeleteOnceGroup(UserEntity currentUser, OnceGroupEntity onceGroupEntity) { + if (!onceGroupEntity.isHost(currentUser)) + throw new GongBaekException(ResponseError.UNAUTHORIZED_ACCESS); + } + private OnceGroupEntity buildOnceGroupEntity(RegisterGroupServiceRequest serviceRequest, GongbaekTimeSlotEntity gongbaekTimeSlotEntity) { return OnceGroupEntity.builder()