From 6caa163dff53ee034a05ba8ace9fa26cf1424464 Mon Sep 17 00:00:00 2001 From: simhani1 Date: Fri, 29 Aug 2025 17:00:01 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refcator:=20=EC=A0=95=EC=B1=85=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=B6=94=EA=B0=80=EB=A1=9C=20srevice=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=EC=9D=98=20=EC=B1=85=EC=9E=84=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../planet/service/PlanetPolicy.java | 44 +++++++++++++++++++ .../planet/service/PlanetServiceImpl.java | 22 +++------- 2 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/planetrush/planetrush/planet/service/PlanetPolicy.java diff --git a/src/main/java/com/planetrush/planetrush/planet/service/PlanetPolicy.java b/src/main/java/com/planetrush/planetrush/planet/service/PlanetPolicy.java new file mode 100644 index 0000000..e8e6be2 --- /dev/null +++ b/src/main/java/com/planetrush/planetrush/planet/service/PlanetPolicy.java @@ -0,0 +1,44 @@ +package com.planetrush.planetrush.planet.service; + +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; + +import org.springframework.stereotype.Component; + +import com.planetrush.planetrush.member.domain.Member; +import com.planetrush.planetrush.planet.domain.Planet; +import com.planetrush.planetrush.planet.exception.InvalidStartDateException; +import com.planetrush.planetrush.planet.exception.ResidentAlreadyExistsException; +import com.planetrush.planetrush.planet.exception.ResidentOverflowException; +import com.planetrush.planetrush.planet.repository.ResidentRepository; +import com.planetrush.planetrush.planet.repository.custom.ResidentRepositoryCustom; + +@Component +public final class PlanetPolicy { + + /** + * 최대 입주 가능 인원을 확인합니다. + */ + public static void validateResidentLimit(Member member, ResidentRepositoryCustom residentRepositoryCustom) { + if (residentRepositoryCustom.getReadyAndInProgressResidents(member) >= 9) { + throw new ResidentOverflowException("resident count overflow"); + } + } + + /** + * 동일한 행성에 중복 가입을 방지합니다. + */ + public static void validateDuplicateResident(Member member, Planet planet, ResidentRepository residentRepository) { + residentRepository.findByMemberIdAndPlanetId(member.getId(), planet.getId()) + .ifPresent(resident -> { + throw new ResidentAlreadyExistsException("resident already exists: " + resident.getId()); + }); + } + + public static void validateStartDateWithinTwoWeeks(LocalDate startDate) { + if(ChronoUnit.DAYS.between(LocalDate.now(), startDate) > 14) { + throw new InvalidStartDateException("Start date must be within 14 days from today."); + } + } + +} diff --git a/src/main/java/com/planetrush/planetrush/planet/service/PlanetServiceImpl.java b/src/main/java/com/planetrush/planetrush/planet/service/PlanetServiceImpl.java index 4891d6d..b4dac6d 100644 --- a/src/main/java/com/planetrush/planetrush/planet/service/PlanetServiceImpl.java +++ b/src/main/java/com/planetrush/planetrush/planet/service/PlanetServiceImpl.java @@ -1,5 +1,7 @@ package com.planetrush.planetrush.planet.service; +import static com.planetrush.planetrush.planet.service.PlanetPolicy.*; + import java.time.LocalDate; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; @@ -21,11 +23,8 @@ import com.planetrush.planetrush.planet.domain.image.DefaultPlanetImg; import com.planetrush.planetrush.planet.exception.DuplicatedDeleteResidentRequestException; import com.planetrush.planetrush.planet.exception.DuplicatedRegisterResidentRequestException; -import com.planetrush.planetrush.planet.exception.InvalidStartDateException; import com.planetrush.planetrush.planet.exception.PlanetNotFoundException; -import com.planetrush.planetrush.planet.exception.ResidentAlreadyExistsException; import com.planetrush.planetrush.planet.exception.ResidentNotFoundException; -import com.planetrush.planetrush.planet.exception.ResidentOverflowException; import com.planetrush.planetrush.planet.repository.DefaultPlanetImgRepository; import com.planetrush.planetrush.planet.repository.PlanetRepository; import com.planetrush.planetrush.planet.repository.ResidentRepository; @@ -320,13 +319,8 @@ public void registerResident(PlanetSubscriptionDto dto) { log.error("[IDEMPOTENT] 행성 가입 중복 요청 발생, 회원={}, 행성={}", member.getId(), planet.getId()); throw new DuplicatedRegisterResidentRequestException(); } - if(residentRepositoryCustom.getReadyAndInProgressResidents(member) >= 9) { - throw new ResidentOverflowException("resident count overflow"); - } - residentRepository.findByMemberIdAndPlanetId(member.getId(), planet.getId()) - .ifPresent(resident -> { - throw new ResidentAlreadyExistsException("resident already exists: " + resident.getId()); - }); + validateResidentLimit(member, residentRepositoryCustom); + validateDuplicateResident(member, planet, residentRepository); planet.addParticipant(); residentRepository.save(Resident.isNotCreator(member, planet)); } @@ -368,12 +362,8 @@ public void deleteResident(PlanetSubscriptionDto dto) { public void registerPlanet(RegisterPlanetDto dto) { Member member = memberRepository.findById(dto.getMemberId()) .orElseThrow(() -> new MemberNotFoundException("Member not found with ID: " + dto.getMemberId())); - if(ChronoUnit.DAYS.between(LocalDate.now(), dto.getStartDate()) > 14) { - throw new InvalidStartDateException("Start date must be within 14 days from today."); - } - if(residentRepositoryCustom.getReadyAndInProgressResidents(member) >= 9) { - throw new ResidentOverflowException("resident count overflow"); - } + validateStartDateWithinTwoWeeks(dto.getStartDate()); + validateResidentLimit(member, residentRepositoryCustom); Planet planet = planetRepository.save(Planet.builder() .name(dto.getName()) .category(Category.valueOf(dto.getCategory())) From fffa86536a92ab396697fd314ce20cdacab7d139 Mon Sep 17 00:00:00 2001 From: simhani1 Date: Fri, 29 Aug 2025 17:05:00 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=EB=A7=A4=EC=A7=81=EB=84=98=EB=B2=84?= =?UTF-8?q?=20=EC=83=81=EC=88=98=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../planet/service/PlanetPolicy.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/planetrush/planetrush/planet/service/PlanetPolicy.java b/src/main/java/com/planetrush/planetrush/planet/service/PlanetPolicy.java index e8e6be2..1a2e135 100644 --- a/src/main/java/com/planetrush/planetrush/planet/service/PlanetPolicy.java +++ b/src/main/java/com/planetrush/planetrush/planet/service/PlanetPolicy.java @@ -16,17 +16,25 @@ @Component public final class PlanetPolicy { + private static final int MAX_RESIDENT_LIMIT = 9; + private static final int MAX_CHALLENGE_START_OFFSET = 14; + /** - * 최대 입주 가능 인원을 확인합니다. + * 가입 가능한 최대 챌린지 수를 초과하는지 검사합니다. + * @param member + * @param residentRepositoryCustom */ public static void validateResidentLimit(Member member, ResidentRepositoryCustom residentRepositoryCustom) { - if (residentRepositoryCustom.getReadyAndInProgressResidents(member) >= 9) { + if (residentRepositoryCustom.getReadyAndInProgressResidents(member) >= MAX_RESIDENT_LIMIT) { throw new ResidentOverflowException("resident count overflow"); } } /** * 동일한 행성에 중복 가입을 방지합니다. + * @param member + * @param planet + * @param residentRepository */ public static void validateDuplicateResident(Member member, Planet planet, ResidentRepository residentRepository) { residentRepository.findByMemberIdAndPlanetId(member.getId(), planet.getId()) @@ -35,8 +43,12 @@ public static void validateDuplicateResident(Member member, Planet planet, Resid }); } + /** + * 2주 이내로 시작하는지 검사합니다. + * @param startDate + */ public static void validateStartDateWithinTwoWeeks(LocalDate startDate) { - if(ChronoUnit.DAYS.between(LocalDate.now(), startDate) > 14) { + if(ChronoUnit.DAYS.between(LocalDate.now(), startDate) > MAX_CHALLENGE_START_OFFSET) { throw new InvalidStartDateException("Start date must be within 14 days from today."); } }