Skip to content

Commit

Permalink
refactor: 특정 날짜에 동일 코드가 있는지 확인하는 조건 QueryDsl로 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
zinzoddari committed Jan 7, 2024
1 parent 3b2b331 commit 74da4db
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public class WorkTimerService {
public Long registerTimer(LocalDateTime checkIn, AuthenticateCodeGenerator authenticateCodeStrategy) {

VerificationCode code = VerificationCode.create(authenticateCodeStrategy);
boolean isExistence = workTimerRepository.findByCheckInGreaterThanEqualAndCode(checkIn.toLocalDate().atStartOfDay(), code).isPresent();
boolean isExistence = workTimerRepository.findByCheckInAndCode(checkIn.toLocalDate(), code).isPresent();

while (isExistence) {
code = VerificationCode.create(authenticateCodeStrategy);
isExistence = workTimerRepository.findByCheckInGreaterThanEqualAndCode(checkIn.toLocalDate().atStartOfDay(), code).isPresent();
isExistence = workTimerRepository.findByCheckInAndCode(checkIn.toLocalDate(), code).isPresent();
}

return workTimerRepository.save(WorkTimer.registerOf(checkIn, code))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.idiot.yesslave.worktimer.repository;

import org.idiot.yesslave.worktimer.domain.VerificationCode;
import org.idiot.yesslave.worktimer.domain.WorkTimer;

import java.time.LocalDate;
import java.util.Optional;

public interface WorkTimerCustomRepository {
Optional<WorkTimer> findByCheckInAndCode(LocalDate checkIn, VerificationCode code);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package org.idiot.yesslave.worktimer.repository;

import org.idiot.yesslave.worktimer.domain.VerificationCode;
import org.idiot.yesslave.worktimer.domain.WorkTimer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Optional;
public interface WorkTimerRepository extends JpaRepository<WorkTimer, Long>, WorkTimerCustomRepository {

public interface WorkTimerRepository extends JpaRepository<WorkTimer, Long> {

Optional<WorkTimer> findByCheckInGreaterThanEqualAndCode(@Param("checkIn") LocalDateTime checkIn, @Param("code") VerificationCode code);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.idiot.yesslave.worktimer.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.idiot.yesslave.worktimer.domain.QWorkTimer;
import org.idiot.yesslave.worktimer.domain.VerificationCode;
import org.idiot.yesslave.worktimer.domain.WorkTimer;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.Optional;


@Repository
@RequiredArgsConstructor
public class WorkTimerRepositoryImpl implements WorkTimerCustomRepository {
private final JPAQueryFactory jpaQueryFactory;

@Override
public Optional<WorkTimer> findByCheckInAndCode(LocalDate checkIn, VerificationCode code) {
QWorkTimer workTimer = QWorkTimer.workTimer;

return Optional.ofNullable(jpaQueryFactory.selectFrom(workTimer)
.where(workTimer.checkIn.after(checkIn.atStartOfDay())
.and(workTimer.checkIn.before(checkIn.plusDays(1).atStartOfDay()))
.and(workTimer.code.eq(code)))
.limit(1)
.fetchOne());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void success() {

final WorkTimer expectedWorkTime = new WorkTimer(1L, checkIn, null, code);

given(workTimerRepository.findByCheckInGreaterThanEqualAndCode(eq(checkIn.toLocalDate().atStartOfDay()), any(VerificationCode.class)))
given(workTimerRepository.findByCheckInAndCode(eq(checkIn.toLocalDate()), any(VerificationCode.class)))
.willReturn(Optional.empty());

given(workTimerRepository.save(any(WorkTimer.class)))
Expand Down

0 comments on commit 74da4db

Please sign in to comment.