Skip to content
Merged
26 changes: 24 additions & 2 deletions src/main/java/com/codeit/todo/repository/GoalRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

Expand All @@ -13,7 +16,26 @@ public interface GoalRepository extends JpaRepository<Goal, Integer> {

Optional<Goal> findByGoalIdAndUser_UserId(int goalId, int userId);

Slice<Goal> findByUser_UserId(int userId, Pageable pageable);
Slice<Goal> findByUser_UserId(@Param("userId") int userId, Pageable pageable);

Slice<Goal> findByGoalIdAndUser_UserId(Integer goalId, int userId, Pageable pageable);
Slice<Goal> findByGoalIdAndUser_UserId(@Param("lastGoalId") Integer lastGoalId, @Param("userId") int userId, Pageable pageable);

@Query("""
select g
from Goal g
join fetch g.todos t
where g.user.userId = :userId
and :today between t.startDate and t.endDate
""")
Slice<Goal> findByUserAndHasTodos(@Param("userId") int userId, Pageable pageable, @Param("today") LocalDate today);

@Query("""
select g
from Goal g
join fetch g.todos t
where g.user.userId = :userId
and g.goalId = :lastGoalId
and :today between t.startDate and t.endDate
""")
Slice<Goal> findByUserAndHasTodosAfterLastGoalId(@Param("lastGoalId") Integer lastGoalId, @Param("userId") int userId, Pageable pageable, @Param("today") LocalDate today);
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Slice<ReadTodosWithGoalsResponse> findAllGoals(int userId, ReadTodoComple
int pageSize = request.size();
Pageable pageable = PageRequest.of(0, pageSize);

Slice<Goal> goals = todoServiceImpl.getGoalsPagination(userId, request, pageable);
Slice<Goal> goals = getGoalsPagination(userId, request, pageable);

List<ReadTodosWithGoalsResponse> goalsResponses = goals.stream()
.map(goal -> {
Expand All @@ -127,6 +127,16 @@ public Slice<ReadTodosWithGoalsResponse> findAllGoals(int userId, ReadTodoComple
return new SliceImpl<>(sortedGoalsResponses, pageable, goals.hasNext());
}

private Slice<Goal> getGoalsPagination(int userId, ReadTodoCompleteWithGoalRequest request, Pageable pageable){
Slice<Goal> goals;
if (Objects.isNull(request.lastGoalId()) || request.lastGoalId() <= 0) {
goals = goalRepository.findByUser_UserId(userId, pageable);
} else {
goals = goalRepository.findByGoalIdAndUser_UserId(request.lastGoalId(), userId, pageable);
}
return goals;
}

private List<ReadTodosWithGoalsResponse> sortAllGoals(List<ReadTodosWithGoalsResponse> goalsResponses){
List<ReadTodosWithGoalsResponse> sortedGoalsResponses = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ public Slice<ReadTodosWithGoalsResponse> findTodoListWithGoals(int userId, @Vali
int pageSize = request.size();
Pageable pageable = PageRequest.of(0, pageSize);

Slice<Goal> goals = getGoalsPagination(userId, request, pageable);
Slice<Goal> goals;
LocalDate today = LocalDate.now();

if (Objects.isNull(request.lastGoalId()) || request.lastGoalId() <= 0) {
goals = goalRepository.findByUserAndHasTodos(userId, pageable, today);
} else {
goals = goalRepository.findByUserAndHasTodosAfterLastGoalId(request.lastGoalId(), userId, pageable, today);
}

List<ReadTodosWithGoalsResponse> responses = goals.getContent().stream()
.map(goal -> {
List<Todo> todos = todoRepository.findTodosByGoalIdBetweenDates(goal.getGoalId(), LocalDate.now());

List<Todo> todos = goal.getTodos();
List<ReadTodosResponse> todosResponses = makeTodosResponses(todos);

double goalProgress = calculateGoalProgress(todos);
Expand Down Expand Up @@ -270,7 +276,7 @@ public double calculateGoalProgress(List<Todo> todos) {
}

public List<ReadTodosResponse> makeTodosResponses(List<Todo> todos){
List<ReadTodosResponse> todosResponses = todos.stream()
return todos.stream()
.map(todo -> {
List<Complete> completes = completeRepository.findByTodo_TodoId(todo.getTodoId());

Expand All @@ -280,8 +286,6 @@ public List<ReadTodosResponse> makeTodosResponses(List<Todo> todos){

return ReadTodosResponse.from(todo, completeResponses);
}).toList();

return todosResponses;
}

public Slice<Goal> getGoalsPagination(int userId, ReadTodoCompleteWithGoalRequest request, Pageable pageable){
Expand Down
Loading