Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ public interface TaskRepository extends JpaRepository<Task, Long> {
List<Task> findByUserIdAndCategoryIdAndDueDate(Long userId, Long categoryId, LocalDate date);

/**
* 카테고리 내에서 현재 등록된 Task의 최대 position을 조회합니다.
* 특정 날짜와 카테고리 내에서 현재 등록된 Task의 최대 position을 조회합니다.
* <p>
* - 새로운 Task 생성 시 position 지정에 사용됩니다.
* - 값이 없으면 null을 반환합니다.
*
* @param categoryId
* @param date
* @return max position 값 (null 가능)
*/
@Query("SELECT MAX(t.position) FROM Task t WHERE t.category.id = :categoryId")
Integer findMaxPositionByCategoryId(@Param("categoryId") Long categoryId);
Integer findTopByCategoryIdAndDueDateOrderByPositionDesc(Long categoryId, LocalDate date);

List<Task> findAllByUserIdAndCategoryId(Long userId, Long categoryId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -46,7 +47,7 @@ public TaskResponseDTO createTask(Long userId, Long categoryId, TaskCreateReques
.orElseThrow(() -> new GeneralException(ErrorStatus.TASK_CATEGORY_NOT_FOUND));

// position 지정
Integer position = generateNextPosition(categoryId);
Integer position = generateNextPosition(categoryId, request.getDate());

Task task = taskConverter.toEntity(request, category, user, position);
taskRepository.save(task);
Expand All @@ -57,19 +58,24 @@ public TaskResponseDTO createTask(Long userId, Long categoryId, TaskCreateReques

/**
* 새로운 할 일의 position 을 계산합니다.
* 카테고리 내 task의 최대 position을 조회한 후,
* 없으면 0번, 있으면 max+1로 지정합니다.
* 특정 날짜와 카테고리 내 task의 최대 position을 조회한 후,
* 없으면 0, 있으면 max+1로 지정합니다.
*
* @param categoryId
* @param date
* @return 새로운 할 일의 position
*/
private Integer generateNextPosition(Long categoryId) {
Integer max = taskRepository.findMaxPositionByCategoryId(categoryId);
private Integer generateNextPosition(Long categoryId, LocalDate date) {
Integer max = taskRepository.findTopByCategoryIdAndDueDateOrderByPositionDesc(categoryId, date);
if (max == null) {
log.debug("[TASK][generateNextPosition] max가 null이므로 기본값 0으로 시작합니다.");
return 0;
}
return max + 1;
int next = max + 1;
log.debug("[TASK][generateNextPosition] date={}, categoryId={} 의 max={} -> next={}",
date, categoryId, max, next);

return next;
}

/**
Expand Down