Skip to content

Commit

Permalink
Merge pull request #185 from Team-HMH/fix/#162-duplicate-daily-challenge
Browse files Browse the repository at this point in the history
fix - ์ฑŒ๋ฆฐ์ง€ ์ƒ์„ฑ ์‹œ ์ค‘๋ณต๋œ ์ผ๋ณ„ ์ฑŒ๋ฆฐ์ง€๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๋„๋ก ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๋กœ์ง ์ถ”๊ฐ€
  • Loading branch information
kseysh authored Jul 21, 2024
2 parents 52bee42 + b4341dd commit 8a34f26
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public enum DailyChallengeError implements ErrorBase {

DAILY_CHALLENGE_NOT_FOUND(HttpStatus.NOT_FOUND, "์ผ๋ณ„ ์ฑŒ๋ฆฐ์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
DAILY_CHALLENGE_PERIOD_INDEX_NOT_FOUND(HttpStatus.NOT_FOUND, "ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ์ผ๋ณ„ ์ฑŒ๋ฆฐ์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
DAILY_CHALLENGE_ALREADY_PROCESSED(HttpStatus.BAD_REQUEST, "์ด๋ฏธ ์ฒ˜๋ฆฌ๋œ ์ผ๋ณ„ ์ฑŒ๋ฆฐ์ง€์ž…๋‹ˆ๋‹ค.")
;
DAILY_CHALLENGE_ALREADY_PROCESSED(HttpStatus.BAD_REQUEST, "์ด๋ฏธ ์ฒ˜๋ฆฌ๋œ ์ผ๋ณ„ ์ฑŒ๋ฆฐ์ง€์ž…๋‹ˆ๋‹ค."),
DAILY_CHALLENGE_ALREADY_EXISTS(HttpStatus.BAD_REQUEST, "์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์ผ๋ณ„ ์ฑŒ๋ฆฐ์ง€์ž…๋‹ˆ๋‹ค.");

private final HttpStatus status;
private final String errorMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface DailyChallengeRepository {

List<DailyChallenge> findAllByChallengeIdOrderByChallengeDate(Long challengeId);

boolean existsByUserIdAndChallengeDateIn(Long userId, List<LocalDate> localDates);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package sopt.org.hmh.domain.dailychallenge.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import sopt.org.hmh.domain.dailychallenge.domain.DailyChallenge;
import sopt.org.hmh.domain.dailychallenge.domain.QDailyChallenge;

@Repository
@RequiredArgsConstructor
public class DailyChallengeRepositoryImpl implements DailyChallengeRepository {

private final DailyChallengeJpaRepository dailyChallengeJpaRepository;
private final JPAQueryFactory queryFactory;

@Override
public void saveAll(List<DailyChallenge> dailyChallengeByChallengePeriod) {
Expand All @@ -27,4 +30,15 @@ public Optional<DailyChallenge> findByChallengeDateAndUserId(LocalDate challenge
public List<DailyChallenge> findAllByChallengeIdOrderByChallengeDate(Long challengeId) {
return dailyChallengeJpaRepository.findAllByChallengeIdOrderByChallengeDate(challengeId);
}

@Override
public boolean existsByUserIdAndChallengeDateIn(Long userId, List<LocalDate> localDates) {
QDailyChallenge dailyChallenge = QDailyChallenge.dailyChallenge;
Integer fetchOne = queryFactory.selectOne()
.from(dailyChallenge)
.where(dailyChallenge.userId.eq(userId)
.and(dailyChallenge.challengeDate.in(localDates)))
.fetchFirst();
return fetchOne != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,20 @@ private void handleAlreadyProcessedDailyChallenge(DailyChallenge dailyChallenge)
}

public void addDailyChallenge(Challenge challenge) {
validateDuplicateDailyChallenge(challenge);
dailyChallengeRepository.saveAll(createDailyChallengeByChallengePeriod(challenge));
}

private void validateDuplicateDailyChallenge(Challenge challenge) {
List<LocalDate> localDatesToCheck = IntStream.range(0, challenge.getPeriod())
.mapToObj(i -> challenge.getStartDate().plusDays(i))
.toList();

if (dailyChallengeRepository.existsByUserIdAndChallengeDateIn(challenge.getUserId(), localDatesToCheck)) {
throw new DailyChallengeException(DailyChallengeError.DAILY_CHALLENGE_ALREADY_EXISTS);
}
}

private List<DailyChallenge> createDailyChallengeByChallengePeriod(Challenge challenge) {
LocalDate startDate = challenge.getStartDate();
Long userId = challenge.getUserId();
Expand Down

0 comments on commit 8a34f26

Please sign in to comment.