Skip to content

Commit

Permalink
feat-be: 프로세스 생성 (#35)
Browse files Browse the repository at this point in the history
* Create draft PR for #31

* feat: 프로세스 생성

Co-Authored-By: 김형호 <140397285+HyungHoKim00@users.noreply.github.com>
Co-Authored-By: Kwoun Ki Ho <73146678+Chocochip101@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: cutehumanS2 <oddpinkjadeite@gmail.com>
Co-authored-by: 김형호 <140397285+HyungHoKim00@users.noreply.github.com>
Co-authored-by: Kwoun Ki Ho <73146678+Chocochip101@users.noreply.github.com>
  • Loading branch information
4 people authored and Dobby-Kim committed Jul 22, 2024
1 parent 92655b6 commit 82648f1
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/main/java/com/cruru/process/controller/ProcessController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.cruru.process.controller;

import com.cruru.process.controller.dto.ProcessCreateRequest;
import com.cruru.process.controller.dto.ProcessesResponse;
import com.cruru.process.service.ProcessService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/processes")
Expand All @@ -21,4 +20,11 @@ public ResponseEntity<ProcessesResponse> read(@RequestParam(name = "dashboard_id
ProcessesResponse processes = processService.findByDashboardId(dashboardId);
return ResponseEntity.ok().body(processes);
}

@PostMapping
public ResponseEntity<Void> create(@RequestParam(name = "dashboard_id") Long dashboardId,
@RequestBody ProcessCreateRequest request) {
processService.create(dashboardId, request);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.cruru.process.controller.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

public record ProcessCreateRequest(
@JsonProperty("process_name")
String name,

String description,

@JsonProperty("prior_process_id")
Long priorProcessId
) {
}
4 changes: 4 additions & 0 deletions src/main/java/com/cruru/process/domain/Process.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public Process(int sequence, String name, String description, Dashboard dashboar
this.dashboard = dashboard;
}

public void increaseSequenceNumber() {
this.sequence++;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/cruru/process/service/ProcessService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.cruru.applicant.domain.repository.ApplicantRepository;
import com.cruru.dashboard.domain.repository.DashboardRepository;
import com.cruru.dashboard.exception.DashboardNotFoundException;
import com.cruru.process.controller.dto.ProcessCreateRequest;
import com.cruru.process.controller.dto.ProcessResponse;
import com.cruru.process.controller.dto.ProcessesResponse;
import com.cruru.process.domain.Process;
Expand Down Expand Up @@ -52,4 +53,21 @@ private ProcessResponse toProcessResponse(Process process) {
private DashboardApplicantDto toApplicantDto(Applicant applicant) {
return new DashboardApplicantDto(applicant.getId(), applicant.getName(), applicant.getCreatedDate());
}

@Transactional
public void create(long dashboardId, ProcessCreateRequest request) {
List<Process> allByDashboardId = processRepository.findAllByDashboardId(dashboardId);
Process priorProcess = processRepository.findById(request.priorProcessId()).orElseThrow();

allByDashboardId.stream()
.filter(process -> process.getSequence() > priorProcess.getSequence())
.forEach(Process::increaseSequenceNumber);

processRepository.save(
new Process(priorProcess.getSequence() + 1,
request.name(),
request.description(),
dashboardRepository.findById(dashboardId).orElseThrow())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import com.cruru.dashboard.domain.Dashboard;
import com.cruru.dashboard.domain.repository.DashboardRepository;
import com.cruru.process.controller.dto.ProcessCreateRequest;
import com.cruru.process.domain.Process;
import com.cruru.process.domain.repository.ProcessRepository;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -20,19 +24,37 @@ class ProcessControllerTest {
@Autowired
private DashboardRepository dashboardRepository;

@Autowired
private ProcessRepository processRepository;

private Dashboard dashboard;

private Process process;

@BeforeEach
void setUp() {
RestAssured.port = port;
dashboard = dashboardRepository.save(new Dashboard("name", null));
process = processRepository.save(new Process(0, "면접", "1차 면접", dashboard));
}

@DisplayName("ID에 해당하는 대시보드의 프로세스 목록과 지원자 정보를 조회한다.")
@DisplayName("프로세스 조회 성공 시, 200을 응답한다.")
@Test
void read() {
RestAssured.given().log().all()
.when().get("/api/v1/processes?dashboard_id=" + dashboard.getId())
.then().log().all().statusCode(200);
}

@DisplayName("프로세스 생성 성공 시, 201을 응답한다.")
@Test
void create() {
ProcessCreateRequest processCreateRequest = new ProcessCreateRequest("면접", "1차 면접", process.getId());

RestAssured.given().log().all()
.contentType(ContentType.JSON)
.body(processCreateRequest)
.when().post("/api/v1/processes?dashboard_id=" + dashboard.getId())
.then().log().all().statusCode(201);
}
}
37 changes: 34 additions & 3 deletions src/test/java/com/cruru/process/service/ProcessServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@
import com.cruru.dashboard.domain.Dashboard;
import com.cruru.dashboard.domain.repository.DashboardRepository;
import com.cruru.dashboard.exception.DashboardNotFoundException;
import com.cruru.process.controller.dto.ProcessCreateRequest;
import com.cruru.process.controller.dto.ProcessesResponse;
import com.cruru.process.domain.Process;
import com.cruru.process.domain.repository.ProcessRepository;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Comparator;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@DisplayName("프로세스 서비스 테스트")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
class ProcessServiceTest {

@PersistenceContext
EntityManager em;
@Autowired
private DashboardRepository dashboardRepository;

@Autowired
private ProcessRepository processRepository;

@Autowired
private ApplicantRepository applicantRepository;

@Autowired
private ProcessService processService;

Expand Down Expand Up @@ -62,4 +67,30 @@ void invalidDashboardId() {
assertThatThrownBy(() -> processService.findByDashboardId(invalidId))
.isInstanceOf(DashboardNotFoundException.class);
}

@DisplayName("새로운 프로세스를 생성한다.")
@Test
void create() {
// given
Dashboard dashboard = new Dashboard("name", null);
dashboard = dashboardRepository.save(dashboard);
Process process1 = new Process(0, "name", "description", dashboard);
process1 = processRepository.save(process1);
Process process2 = new Process(1, "name", "description", dashboard);
process2 = processRepository.save(process2);

// when
ProcessCreateRequest processCreateRequest = new ProcessCreateRequest("면접", "1차 면접", process1.getId());
processService.create(dashboard.getId(), processCreateRequest);

// then
List<Process> allByDashboardId = processRepository.findAllByDashboardId(dashboard.getId())
.stream()
.sorted(Comparator.comparingInt(Process::getSequence))
.toList();


assertThat(allByDashboardId).hasSize(3);
assertThat(allByDashboardId.get(1).getName()).isEqualTo("면접");
}
}

0 comments on commit 82648f1

Please sign in to comment.