Advertisement
yxngxr1

Unfinished

Mar 8th, 2025
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. // Получение количества незавершённых задач в проектах
  2.     @GetMapping("/tasks/count")
  3.     public ResponseEntity<Map<Long, Long>> getUnfinishedTasksCount() {
  4.         return ResponseEntity.ok(projectService.getUnfinishedTasksCount());
  5.     }
  6.  
  7. public Map<Long, Long> getUnfinishedTasksCount() {
  8.         // попробовать избавиться от Object[], добавить типизацию
  9.         List<UnfinishedTasksCountDto> results = taskRepository.countUnfinishedTasksByProjectIdOptimized();
  10.  
  11.         return results.stream()
  12.                 .collect(Collectors.toMap(UnfinishedTasksCountDto::getProjectId, UnfinishedTasksCountDto::getUnfinishedTasks));
  13.     }
  14.  
  15. @Query("SELECT new com.ssau.todoJpa.dto.UnfinishedTasksCountDto(p.id, COALESCE(COUNT(t.id), 0)) " +
  16.             "FROM Project p " +
  17.             "LEFT JOIN p.tasks t " +
  18.             "WHERE t.isCompleted = false OR t IS NULL " +
  19.             "GROUP BY p.id")
  20.     // Создать interface/DTO, посмотреть spring query projection
  21.     List<UnfinishedTasksCountDto> countUnfinishedTasksByProjectIdOptimized();
  22.  
  23.  
  24. @Setter
  25. @Getter
  26. @AllArgsConstructor
  27. public class UnfinishedTasksCountDto {
  28.     private Long projectId;
  29.     private Long unfinishedTasks;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement