diff --git a/src/main/java/gymmi/workspace/repository/custom/WorkoutHistoryCustomRepository.java b/src/main/java/gymmi/workspace/repository/custom/WorkoutHistoryCustomRepository.java index f7abc0a..80d9af8 100644 --- a/src/main/java/gymmi/workspace/repository/custom/WorkoutHistoryCustomRepository.java +++ b/src/main/java/gymmi/workspace/repository/custom/WorkoutHistoryCustomRepository.java @@ -12,4 +12,5 @@ public interface WorkoutHistoryCustomRepository { List getAllByWorkspaceId(Long workspaceId, Pageable pageable); List getAllByDate(LocalDate localDate); + List getAllByDate(Long workerId, LocalDate localDate); } diff --git a/src/main/java/gymmi/workspace/repository/custom/WorkoutHistoryCustomRepositoryImpl.java b/src/main/java/gymmi/workspace/repository/custom/WorkoutHistoryCustomRepositoryImpl.java index 22ba5df..b504ded 100644 --- a/src/main/java/gymmi/workspace/repository/custom/WorkoutHistoryCustomRepositoryImpl.java +++ b/src/main/java/gymmi/workspace/repository/custom/WorkoutHistoryCustomRepositoryImpl.java @@ -1,8 +1,6 @@ package gymmi.workspace.repository.custom; -import com.querydsl.core.Tuple; import com.querydsl.jpa.impl.JPAQueryFactory; -import gymmi.workspace.domain.entity.Objection; import gymmi.workspace.domain.entity.WorkoutHistory; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; @@ -11,7 +9,6 @@ import java.time.LocalDateTime; import java.util.List; -import static gymmi.workspace.domain.entity.QObjection.objection; import static gymmi.workspace.domain.entity.QWorker.worker; import static gymmi.workspace.domain.entity.QWorkoutConfirmation.workoutConfirmation; import static gymmi.workspace.domain.entity.QWorkoutHistory.workoutHistory; @@ -46,4 +43,16 @@ public List getAllByDate(LocalDate now) { .fetch(); } + @Override + public List getAllByDate(Long workerId, LocalDate now) { + LocalDateTime startDay = now.atStartOfDay(); + LocalDateTime endDay = now.plusDays(1).atStartOfDay(); + return jpaQueryFactory.select(workoutHistory) + .from(workoutHistory) + .where(workoutHistory.createdAt.goe(startDay).and(workoutHistory.createdAt.before(endDay) + .and(workoutHistory.worker.id.eq(workerId)) + )) + .fetch(); + } + } diff --git a/src/main/java/gymmi/workspace/service/WorkspaceCommandService.java b/src/main/java/gymmi/workspace/service/WorkspaceCommandService.java index a0ef4c2..c2cfe67 100644 --- a/src/main/java/gymmi/workspace/service/WorkspaceCommandService.java +++ b/src/main/java/gymmi/workspace/service/WorkspaceCommandService.java @@ -19,7 +19,10 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; -import java.util.*; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; @Service @RequiredArgsConstructor @@ -113,7 +116,7 @@ public Integer workMissionsInWorkspace( Worker worker = workerRepository.getByUserIdAndWorkspaceId(loginedUser.getId(), workspace.getId()); List missions = missionRepository.getAllByWorkspaceId(workspace.getId()); Map workouts = getWorkouts(workoutRequest.getMissions()); - validateDailyWorkoutHistoryCount(); + validateDailyWorkoutHistoryCount(worker.getId()); WorkspaceProgressManager workspaceProgressManager = new WorkspaceProgressManager(workspace, missions); @@ -136,8 +139,8 @@ public Integer workMissionsInWorkspace( return workoutHistory.getSum(); } - private void validateDailyWorkoutHistoryCount() { - List workoutHistories = workoutHistoryRepository.getAllByDate(LocalDate.now()); + private void validateDailyWorkoutHistoryCount(Long workerId) { + List workoutHistories = workoutHistoryRepository.getAllByDate(workerId, LocalDate.now()); if (workoutHistories.size() >= 3) { throw new InvalidStateException(ErrorCode.EXCEED_MAX_DAILY_WORKOUT_HISTORY_COUNT); }