Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: #365 일정 목록 조회 기능 수정 #366

Merged
merged 7 commits into from
Dec 31, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.growup.pms.common.exception.code.ErrorCode;
import com.growup.pms.common.exception.exceptions.BusinessException;
import com.growup.pms.status.domain.Status;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -12,4 +13,6 @@ public interface StatusRepository extends JpaRepository<Status, Long>, StatusQue
default Status findByIdOrThrow(Long id) {
return findById(id).orElseThrow(() -> new BusinessException(ErrorCode.STATUS_NOT_FOUND));
}

List<Status> findAllByProjectId(Long projectId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import com.growup.pms.task.controller.dto.response.TaskResponse;
import java.util.List;
import java.util.Map;

public interface TaskQueryRepository {

Map<Long, List<TaskResponse>> getTasksByProjectId(Long projectId);
void decreaseSortOrderByStatus(Long statusId, Short sortOrder);

void updateSortOrderInStatus(Long projectId, Short sortOrder);
List<TaskResponse> getAllTasksByStatus(Long statusId);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.growup.pms.task.repository;

import static com.growup.pms.project.domain.QProject.project;
import static com.growup.pms.status.domain.QStatus.status;
import static com.growup.pms.task.domain.QTask.task;
import static java.util.stream.Collectors.groupingBy;

import com.growup.pms.task.controller.dto.response.TaskResponse;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
Expand All @@ -25,57 +20,43 @@ public class TaskQueryRepositoryImpl implements TaskQueryRepository {

private final JPAQueryFactory queryFactory;

@Override
public Map<Long, List<TaskResponse>> getTasksByProjectId(Long projectId) {
List<Long> ids = queryFactory
.select(task.id)
public void decreaseSortOrderByStatus(Long statusId, Short sortOrder) {
queryFactory.update(task)
.set(task.sortOrder, task.sortOrder.subtract(1))
.where(isStatusId(statusId), task.sortOrder.gt(sortOrder))
.execute();
}
longBright marked this conversation as resolved.
Show resolved Hide resolved

public List<TaskResponse> getAllTasksByStatus(Long statusId) {
List<Long> ids = queryFactory.select(task.id)
.from(task)
.join(task.status, status)
.join(task.status.project, project)
.where(
isProjectId(projectId)
isStatusId(statusId)
)
.orderBy(task.sortOrder.asc())
.fetch();

if (ids.isEmpty()) {
return Collections.emptyMap();
return Collections.emptyList();
}

List<Tuple> results = queryFactory
.select(task.status.id,
Projections.constructor(TaskResponse.class,
task.id,
task.status.id,
task.name,
task.content,
task.sortOrder,
task.startDate,
task.endDate
))
return queryFactory.select(Projections.constructor(TaskResponse.class,
task.id,
task.status.id,
task.name,
task.content,
task.sortOrder,
task.startDate,
task.endDate
))
.from(task)
.join(task.status, status)
.join(task.status.project, project)
.where(
task.id.in(ids)
)
.orderBy(task.sortOrder.asc())
.fetch();

return results.stream()
.map(tuple -> tuple.get(1, TaskResponse.class))
.collect(groupingBy(task -> Objects.requireNonNull(task).getStatusId()));
}

public void updateSortOrderInStatus(Long statusId, Short sortOrder) {
queryFactory.update(task)
.set(task.sortOrder, task.sortOrder.subtract(1))
.where(isStatusId(statusId), task.sortOrder.gt(sortOrder))
.execute();
}

private BooleanExpression isProjectId(Long projectId) {
return projectId != null ? task.status.project.id.eq(projectId) : null;
}

private BooleanExpression isStatusId(Long statusId) {
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/growup/pms/task/service/TaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.growup.pms.user.repository.UserRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -70,11 +69,11 @@ private void addAssignees(Task savedTask, List<Long> assigneeIds) {

public List<TaskKanbanResponse> getTasks(Long projectId) {
List<TaskKanbanResponse> responses = new ArrayList<>();
Map<Long, List<TaskResponse>> statusTaskMap = taskRepository.getTasksByProjectId(projectId);
statusTaskMap.forEach((statusId, tasks) -> {
Status status = statusRepository.findByIdOrThrow(statusId);
responses.add(TaskKanbanResponse.of(status, tasks));
});
statusRepository.findAllByProjectId(projectId)
.forEach(status -> {
List<TaskResponse> tasks = taskRepository.getAllTasksByStatus(status.getId());
responses.add(TaskKanbanResponse.of(status, tasks));
});
return responses;
}

Expand Down Expand Up @@ -109,7 +108,7 @@ public void editTaskOrder(List<TaskOrderEditCommand> commands) {
public void deleteTask(Long taskId) {
Task task = taskRepository.findByIdOrThrow(taskId);
Status status = task.getStatus();
taskRepository.updateSortOrderInStatus(status.getId(), task.getSortOrder());
taskRepository.decreaseSortOrderByStatus(status.getId(), task.getSortOrder());
longBright marked this conversation as resolved.
Show resolved Hide resolved
taskRepository.delete(task);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.growup.pms.user.repository.UserRepository;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -138,6 +137,7 @@ void setUp() {
.종료일자는(LocalDate.parse("2023-01-15"))
.이다()
);

PMS_등록기능 = taskRepository.save(
일정은()
.상태는(PMS_완료)
Expand All @@ -149,7 +149,7 @@ void setUp() {
.이다()
);
PMS_조회기능 = taskRepository.save(
일정은().식별자는(3L)
일정은()
.상태는(PMS_진행중)
.이름은("PMS 프로젝트의 조회 기능 구현을 진행함")
.내용은("- 조회 레포지토리 구현 <br> - 조회 쿼리 구현 및 테스트 작성")
Expand All @@ -159,7 +159,7 @@ void setUp() {
.이다()
);
PMS_수정기능 = taskRepository.save(
일정은().식별자는(4L)
일정은()
.상태는(PMS_할일)
.이름은("PMS 프로젝트의 수정 기능 구현을 진행함")
.내용은(null)
Expand All @@ -169,7 +169,7 @@ void setUp() {
.이다()
);
PMS_삭제기능 = taskRepository.save(
일정은().식별자는(5L)
일정은()
.상태는(PMS_할일)
.이름은("PMS 프로젝트의 삭제 기능 구현을 진행함")
.내용은("- 누가누가 이 기능에 먼저 도착할까")
Expand All @@ -186,38 +186,42 @@ class 상태별_일정_전체_검색시 {
@Test
void 성공한다() {
// given
Long 프로젝트_ID = PMS_프로젝트.getId();
Long 완료_상태_ID = PMS_완료.getId();
Long 진행중_상태_ID = PMS_진행중.getId();
Long 할일_상태_ID = PMS_할일.getId();
Long 보류_상태_ID = PMS_보류.getId();

// when
Map<Long, List<TaskResponse>> 실제_결과 = taskQueryRepository.getTasksByProjectId(프로젝트_ID);
List<TaskResponse> 완료_결과 = taskQueryRepository.getAllTasksByStatus(완료_상태_ID);
List<TaskResponse> 진행중_결과 = taskQueryRepository.getAllTasksByStatus(진행중_상태_ID);
List<TaskResponse> 할일_결과 = taskQueryRepository.getAllTasksByStatus(할일_상태_ID);
List<TaskResponse> 보류_결과 = taskQueryRepository.getAllTasksByStatus(보류_상태_ID);

// then
assertThat(실제_결과.get(완료_상태_ID)).hasSize(2);
assertThat(실제_결과.get(완료_상태_ID).stream().map(TaskResponse::getTaskName))
.containsExactly("PMS 프로젝트의 환경설정을 진행함", "PMS 프로젝트의 등록 기능 구현을 진행함");
assertSoftly(softly -> {
assertThat(완료_결과).hasSize(2);
assertThat(완료_결과.stream().map(TaskResponse::getTaskName))
.containsExactly("PMS 프로젝트의 환경설정을 진행함", "PMS 프로젝트의 등록 기능 구현을 진행함");

assertThat(실제_결과.get(진행중_상태_ID)).hasSize(1);
assertThat(실제_결과.get(진행중_상태_ID).stream().map(TaskResponse::getTaskName))
.containsExactly("PMS 프로젝트의 조회 기능 구현을 진행함");
assertThat(진행중_결과).hasSize(1);
assertThat(진행중_결과.stream().map(TaskResponse::getTaskName))
.containsExactly("PMS 프로젝트의 조회 기능 구현을 진행함");

assertThat(실제_결과.get(할일_상태_ID)).hasSize(2);
assertThat(실제_결과.get(할일_상태_ID).stream().map(TaskResponse::getTaskName))
.containsExactly("PMS 프로젝트의 수정 기능 구현을 진행함", "PMS 프로젝트의 삭제 기능 구현을 진행함");
assertThat(할일_결과).hasSize(2);
assertThat(할일_결과.stream().map(TaskResponse::getTaskName))
.containsExactly("PMS 프로젝트의 수정 기능 구현을 진행함", "PMS 프로젝트의 삭제 기능 구현을 진행함");

assertThat(실제_결과.get(보류_상태_ID)).isNull();
assertThat(보류_결과).isEmpty();
});
}

@Test
void 해당_프로젝트에_일정이_없으면_빈맵을_반환한다() {
void 해당_상태에_일정이_없으면_빈리스트를_반환한다() {
// given
Long 잘못된_프로젝트_ID = Long.MAX_VALUE;
Long 잘못된_상태_ID = Long.MIN_VALUE;

// when
Map<Long, List<TaskResponse>> 실제_결과 = taskQueryRepository.getTasksByProjectId(잘못된_프로젝트_ID);
List<TaskResponse> 실제_결과 = taskRepository.getAllTasksByStatus(잘못된_상태_ID);

// then
assertThat(실제_결과).isEmpty();
Expand All @@ -234,7 +238,7 @@ class 일정_정렬순서_변경시 {
Short 삭제될_정렬순서 = PMS_환경설정.getSortOrder();

// when
taskQueryRepository.updateSortOrderInStatus(상태_ID, 삭제될_정렬순서);
taskQueryRepository.decreaseSortOrderByStatus(상태_ID, 삭제될_정렬순서);
entityManager.clear();

// then
Expand Down
39 changes: 17 additions & 22 deletions src/test/java/com/growup/pms/task/service/TaskServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -173,18 +172,19 @@ class 사용자가_상태별로_프로젝트_일정_전체_조회시에 {
.정렬순서는((short) 3)
.이다();

List<TaskResponse> 예상_일정_목록_2 = List.of(예상_일정_3);
List<Status> 예상_상태_목록 = List.of(예상_상태_1, 예상_상태_2);

List<TaskResponse> 예상_일정_목록_1 = List.of(예상_일정_1, 예상_일정_2);
Map<Long, List<TaskResponse>> 예상_상태별_일정 = Map.of(예상_상태_ID_1, 예상_일정_목록_1, 예상_상태_ID_2, 예상_일정_목록_2);
List<TaskResponse> 예상_일정_목록_2 = List.of(예상_일정_3);

TaskKanbanResponse 예상_응답_1 = 일정_칸반_응답은().일정목록은(예상_일정_목록_1).이다();
TaskKanbanResponse 예상_응답_2 = 일정_칸반_응답은().상태_식별자는(예상_상태_ID_2).일정목록은(예상_일정_목록_2).이다();

List<TaskKanbanResponse> 예상_결과 = List.of(예상_응답_1, 예상_응답_2);

when(taskRepository.getTasksByProjectId(예상_프로젝트_ID)).thenReturn(예상_상태별_일정);
when(statusRepository.findByIdOrThrow(예상_상태_ID_1)).thenReturn(예상_상태_1);
when(statusRepository.findByIdOrThrow(예상_상태_ID_2)).thenReturn(예상_상태_2);
when(statusRepository.findAllByProjectId(예상_프로젝트_ID)).thenReturn(예상_상태_목록);
when(taskRepository.getAllTasksByStatus(예상_상태_ID_1)).thenReturn(예상_일정_목록_1);
when(taskRepository.getAllTasksByStatus(예상_상태_ID_2)).thenReturn(예상_일정_목록_2);

// when
List<TaskKanbanResponse> 실제_결과 = taskService.getTasks(예상_프로젝트_ID);
Expand All @@ -195,15 +195,12 @@ class 사용자가_상태별로_프로젝트_일정_전체_조회시에 {
}

@Test
void 해당_프로젝트에_일정이_없으면_빈맵을_반환한다() {
void 해당_상태에_일정이_없으면_빈리스트를_반환한다() {
// given
Long 잘못된_프로젝트_ID = Long.MAX_VALUE;

Map<Long, List<TaskResponse>> 예상_결과 = Collections.emptyMap();
when(taskRepository.getTasksByProjectId(잘못된_프로젝트_ID)).thenReturn(예상_결과);
Long 잘못된_상태_ID = Long.MIN_VALUE;

// when
List<TaskKanbanResponse> 실제_결과 = taskService.getTasks(잘못된_프로젝트_ID);
List<TaskKanbanResponse> 실제_결과 = taskService.getTasks(잘못된_상태_ID);

// then
assertThat(실제_결과).isEmpty();
Expand Down Expand Up @@ -436,7 +433,7 @@ class 사용자가_프로젝트_일정_삭제시에 {
taskService.deleteTask(기존_상태_ID);

// then
verify(taskRepository).updateSortOrderInStatus(기존_상태_ID, 기존_일정.getSortOrder());
verify(taskRepository).decreaseSortOrderByStatus(기존_상태_ID, 기존_일정.getSortOrder());
verify(taskRepository).delete(기존_일정);
}

Expand Down Expand Up @@ -476,15 +473,13 @@ class 사용자가_프로젝트_일정_첨부파일_조회시에 {
List<TaskAttachmentResponse> 실제_결과 = taskService.getTaskAttachments(일정_ID);

// then
assertSoftly(softly -> {
softly.assertThat(실제_결과).hasSize(예상_응답_목록.size())
.extracting("fileId", "fileName", "uploadName")
.containsExactlyInAnyOrder(
tuple(예상_응답_1.fileId(), 예상_응답_1.fileName(), 예상_응답_1.uploadName()),
tuple(예상_응답_2.fileId(), 예상_응답_2.fileName(), 예상_응답_2.uploadName()),
tuple(예상_응답_3.fileId(), 예상_응답_3.fileName(), 예상_응답_3.uploadName())
);
});
assertSoftly(softly -> softly.assertThat(실제_결과).hasSize(예상_응답_목록.size())
.extracting("fileId", "fileName", "uploadName")
.containsExactlyInAnyOrder(
tuple(예상_응답_1.fileId(), 예상_응답_1.fileName(), 예상_응답_1.uploadName()),
tuple(예상_응답_2.fileId(), 예상_응답_2.fileName(), 예상_응답_2.uploadName()),
tuple(예상_응답_3.fileId(), 예상_응답_3.fileName(), 예상_응답_3.uploadName())
));
}

@Test
Expand Down
Loading