Skip to content

Commit

Permalink
[REFACTOR] fortuneId -> fortuneWordId로 네이밍 변경 및 자잘한 리팩토링 (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
kseysh authored Oct 8, 2024
2 parents 9304930 + 3a3da78 commit 0604766
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 57 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Long> fortuneWordIds = fortuneWordRepository.findAllIds();
return fortuneWordIds.get(random.nextInt(fortuneWordIds.size()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Optional<FortuneCard> 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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,7 +20,7 @@
@SecurityRequirement(name = "Authorization")
public class FortuneController {

private final FortuneProvider fortuneProvider;
private final FortuneService fortuneService;

@Operation(summary = "오늘의 솝마디 조회")
@ApiResponses({
Expand All @@ -36,7 +36,7 @@ public ResponseEntity<FortuneResponse> getFortune(
) {
return ResponseEntity.ok(
FortuneResponse.of(
fortuneProvider.getTodayFortuneWordByUserId(user.getId(), todayDate),
fortuneService.getTodayFortuneWordByUserId(user.getId(), todayDate),
user.getUsername()
)
);
Expand All @@ -54,7 +54,7 @@ public ResponseEntity<FortuneCardResponse> getFortuneCard(
) {
return ResponseEntity.ok(
FortuneCardResponse.of(
fortuneProvider.getTodayFortuneCardByUserId(user.getId())
fortuneService.getTodayFortuneCardByUserId(user.getId())
)
);
}
Expand Down

0 comments on commit 0604766

Please sign in to comment.