From 6b25b5c05a38c7f2ceae16bbb2f2fb149c5fb42a Mon Sep 17 00:00:00 2001 From: kseysh Date: Mon, 7 Oct 2024 01:33:54 +0900 Subject: [PATCH 1/4] =?UTF-8?q?[refactor]=20fortuneId=20->=20fortuneWord?= =?UTF-8?q?=EB=A1=9C=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EB=B3=80=EA=B2=BD=20?= =?UTF-8?q?(#396)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/sopt/app/domain/entity/fortune/UserFortune.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/sopt/app/domain/entity/fortune/UserFortune.java b/src/main/java/org/sopt/app/domain/entity/fortune/UserFortune.java index 669e5b92..f40738a4 100755 --- a/src/main/java/org/sopt/app/domain/entity/fortune/UserFortune.java +++ b/src/main/java/org/sopt/app/domain/entity/fortune/UserFortune.java @@ -20,13 +20,13 @@ public class UserFortune { private Long userId; @NotNull - private Long fortuneId; + private Long fortuneWordId; @NotNull private LocalDate checkedAt; - public void updateTodayFortune(final Long fortuneId, final LocalDate checkedAt) { - this.fortuneId = fortuneId; + public void updateTodayFortune(final Long fortuneWordId, final LocalDate checkedAt) { + this.fortuneWordId = fortuneWordId; this.checkedAt = checkedAt; } } From 3a6d371f659358c67d5ae6b04e539e827ecf7470 Mon Sep 17 00:00:00 2001 From: kseysh Date: Mon, 7 Oct 2024 01:36:05 +0900 Subject: [PATCH 2/4] =?UTF-8?q?[refactor]=20FortuneGenerator=20=EC=B1=85?= =?UTF-8?q?=EC=9E=84=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EB=B6=84=EB=A6=AC=20(#396)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/fortune/FortuneGenerator.java | 37 ------------------ ...rtuneProvider.java => FortuneService.java} | 39 +++++++++++++------ .../fortune/FortuneWordIdGenerator.java | 20 ++++++++++ 3 files changed, 47 insertions(+), 49 deletions(-) delete mode 100644 src/main/java/org/sopt/app/application/fortune/FortuneGenerator.java rename src/main/java/org/sopt/app/application/fortune/{FortuneProvider.java => FortuneService.java} (55%) create mode 100644 src/main/java/org/sopt/app/application/fortune/FortuneWordIdGenerator.java diff --git a/src/main/java/org/sopt/app/application/fortune/FortuneGenerator.java b/src/main/java/org/sopt/app/application/fortune/FortuneGenerator.java deleted file mode 100644 index 5026fbe2..00000000 --- a/src/main/java/org/sopt/app/application/fortune/FortuneGenerator.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.sopt.app.application.fortune; - -import java.time.LocalDate; -import java.util.List; -import java.util.Random; -import lombok.RequiredArgsConstructor; -import org.sopt.app.domain.entity.fortune.UserFortune; -import org.sopt.app.interfaces.postgres.fortune.FortuneWordRepository; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -@Service -@RequiredArgsConstructor -public class FortuneGenerator { - - private final FortuneWordRepository fortuneWordRepository; - private final Random random = new Random(); - - public UserFortune generateNewUserFortune(Long userId, LocalDate todayDate) { - return UserFortune.builder() - .userId(userId) - .checkedAt(todayDate) - .fortuneId(this.generateRandomFortuneWordId()) - .build(); - } - - @Transactional - public void updateTodayUserFortune(UserFortune userFortune, final LocalDate todayDate) { - userFortune.updateTodayFortune(this.generateRandomFortuneWordId(), todayDate); - } - - private Long generateRandomFortuneWordId() { - List fortuneWordIds = fortuneWordRepository.findAllIds(); - return fortuneWordIds.get(random.nextInt(fortuneWordIds.size())); - } - -} diff --git a/src/main/java/org/sopt/app/application/fortune/FortuneProvider.java b/src/main/java/org/sopt/app/application/fortune/FortuneService.java similarity index 55% rename from src/main/java/org/sopt/app/application/fortune/FortuneProvider.java rename to src/main/java/org/sopt/app/application/fortune/FortuneService.java index b7f548ce..a2e6847b 100644 --- a/src/main/java/org/sopt/app/application/fortune/FortuneProvider.java +++ b/src/main/java/org/sopt/app/application/fortune/FortuneService.java @@ -13,30 +13,45 @@ @Service @RequiredArgsConstructor -public class FortuneProvider { +public class FortuneService { private final FortuneCardRepository fortuneCardRepository; private final FortuneWordRepository fortuneWordRepository; private final UserFortuneRepository userFortuneRepository; - private final FortuneGenerator fortuneGenerator; + private final FortuneWordIdGenerator fortuneWordIdGenerator; @Transactional public FortuneWordInfo getTodayFortuneWordByUserId(final Long userId, final LocalDate todayDate) { - UserFortune userFortune = userFortuneRepository.findByUserId(userId) - .orElseGet(() -> { - UserFortune generatedUserFortune = fortuneGenerator.generateNewUserFortune(userId, todayDate); - return userFortuneRepository.save(generatedUserFortune); - }); - - if(!userFortune.getCheckedAt().equals(todayDate)) { - fortuneGenerator.updateTodayUserFortune(userFortune, todayDate); - } + UserFortune userFortune = this.getTodayUserFortune(userId, todayDate); - return fortuneWordRepository.findById(userFortune.getFortuneId()) + return fortuneWordRepository.findById(userFortune.getFortuneWordId()) .map(FortuneWordInfo::of) .orElseThrow(() -> new BadRequestException(ErrorCode.FORTUNE_NOT_FOUND)); } + private UserFortune getTodayUserFortune(final Long userId, final LocalDate todayDate) { + return userFortuneRepository.findByUserId(userId) + .map(userFortune -> validate(userFortune, todayDate)) + .orElseGet(() -> createNewUserFortune(userId, todayDate)); + } + + private UserFortune validate(UserFortune userFortune, LocalDate todayDate) { + if (!userFortune.getCheckedAt().equals(todayDate)) { + userFortune.updateTodayFortune(fortuneWordIdGenerator.generateRandomFortuneWordId(), todayDate); + } + return userFortune; + } + + private UserFortune createNewUserFortune(Long userId, LocalDate todayDate) { + return userFortuneRepository.save( + UserFortune.builder() + .userId(userId) + .checkedAt(todayDate) + .fortuneWordId(fortuneWordIdGenerator.generateRandomFortuneWordId()) + .build() + ); + } + public FortuneCardInfo getTodayFortuneCardByUserId(final Long userId) { return fortuneCardRepository.findByRelatedUserId(userId) .map(FortuneCardInfo::of) diff --git a/src/main/java/org/sopt/app/application/fortune/FortuneWordIdGenerator.java b/src/main/java/org/sopt/app/application/fortune/FortuneWordIdGenerator.java new file mode 100644 index 00000000..b0e0ac56 --- /dev/null +++ b/src/main/java/org/sopt/app/application/fortune/FortuneWordIdGenerator.java @@ -0,0 +1,20 @@ +package org.sopt.app.application.fortune; + +import java.util.List; +import java.util.Random; +import lombok.RequiredArgsConstructor; +import org.sopt.app.interfaces.postgres.fortune.FortuneWordRepository; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class FortuneWordIdGenerator { + + private final FortuneWordRepository fortuneWordRepository; + private final Random random = new Random(); + + public Long generateRandomFortuneWordId() { + List fortuneWordIds = fortuneWordRepository.findAllIds(); + return fortuneWordIds.get(random.nextInt(fortuneWordIds.size())); + } +} From cb16db8cd9a961c80ab356c599a1a20018e5f0d1 Mon Sep 17 00:00:00 2001 From: kseysh Date: Mon, 7 Oct 2024 01:36:27 +0900 Subject: [PATCH 3/4] =?UTF-8?q?[refactor]=20FortuneProvider=20->=20Fortune?= =?UTF-8?q?Service=EB=A1=9C=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20(#396)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sopt/app/presentation/fortune/FortuneController.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/sopt/app/presentation/fortune/FortuneController.java b/src/main/java/org/sopt/app/presentation/fortune/FortuneController.java index a93ea917..be02e757 100755 --- a/src/main/java/org/sopt/app/presentation/fortune/FortuneController.java +++ b/src/main/java/org/sopt/app/presentation/fortune/FortuneController.java @@ -1,7 +1,7 @@ package org.sopt.app.presentation.fortune; import io.swagger.v3.oas.annotations.security.SecurityRequirement; -import org.sopt.app.application.fortune.FortuneProvider; +import org.sopt.app.application.fortune.FortuneService; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.web.bind.annotation.*; import io.swagger.v3.oas.annotations.Operation; @@ -20,7 +20,7 @@ @SecurityRequirement(name = "Authorization") public class FortuneController { - private final FortuneProvider fortuneProvider; + private final FortuneService fortuneService; @Operation(summary = "오늘의 솝마디 조회") @ApiResponses({ @@ -36,7 +36,7 @@ public ResponseEntity getFortune( ) { return ResponseEntity.ok( FortuneResponse.of( - fortuneProvider.getTodayFortuneWordByUserId(user.getId(), todayDate), + fortuneService.getTodayFortuneWordByUserId(user.getId(), todayDate), user.getUsername() ) ); @@ -54,7 +54,7 @@ public ResponseEntity getFortuneCard( ) { return ResponseEntity.ok( FortuneCardResponse.of( - fortuneProvider.getTodayFortuneCardByUserId(user.getId()) + fortuneService.getTodayFortuneCardByUserId(user.getId()) ) ); } From 3a3da78d8ff27d2a80d990bc85613afc51ec211c Mon Sep 17 00:00:00 2001 From: kseysh Date: Mon, 7 Oct 2024 01:38:36 +0900 Subject: [PATCH 4/4] =?UTF-8?q?[refactor]=20=EB=B3=80=EA=B2=BD=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20=EC=88=98=EC=A0=95=20=EB=B0=98?= =?UTF-8?q?=EC=98=81=20(#396)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interfaces/postgres/fortune/FortuneCardRepositoryImpl.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/sopt/app/interfaces/postgres/fortune/FortuneCardRepositoryImpl.java b/src/main/java/org/sopt/app/interfaces/postgres/fortune/FortuneCardRepositoryImpl.java index 4679e22e..dd2ae976 100644 --- a/src/main/java/org/sopt/app/interfaces/postgres/fortune/FortuneCardRepositoryImpl.java +++ b/src/main/java/org/sopt/app/interfaces/postgres/fortune/FortuneCardRepositoryImpl.java @@ -8,7 +8,6 @@ import org.sopt.app.domain.entity.fortune.QFortuneWord; import org.sopt.app.domain.entity.fortune.QUserFortune; import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; @Repository @RequiredArgsConstructor @@ -26,7 +25,7 @@ public Optional findByRelatedUserId(final Long userId) { queryFactory.select(fortuneCard) .from(userFortune) .join(fortuneWord) - .on(userFortune.fortuneId.eq(fortuneWord.id)) + .on(userFortune.fortuneWordId.eq(fortuneWord.id)) .join(fortuneCard) .on(fortuneWord.fortuneCardId.eq(fortuneCard.id)) .where(userFortune.userId.eq(userId))