Skip to content

Commit

Permalink
feat-be: 특정 지원자 카드를 대시보드의 다른 프로세스로 이동 (#30)
Browse files Browse the repository at this point in the history
* Create draft PR for #29

* feat(Applicant): 지원서 프로세스 변경 API 기능 구현

Co-Authored-By: Leetaehoon <xogns1514@gmail.com>

* style: 코드 컨벤션 개행 추가 및 제거

---------

Co-authored-by: Leetaehoon <xogns1514@gmail.com>
Co-authored-by: Do Yeop Kim <113661364+Dobby-Kim@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 22, 2024
1 parent 21363fc commit 00407ed
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.cruru.applicant.controller;

import com.cruru.applicant.controller.dto.ApplicantMoveRequest;
import com.cruru.applicant.service.ApplicantService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/applicants")
@RequiredArgsConstructor
public class ApplicantController {

private final ApplicantService applicantService;

@PutMapping("/move-process/{processId}")
public ResponseEntity<Void> updateApplicantProcess(
@PathVariable long processId,
@RequestBody ApplicantMoveRequest moveRequest) {

applicantService.updateApplicantProcess(processId, moveRequest);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.cruru.applicant.controller.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

public record ApplicantMoveRequest(
@JsonProperty(value = "applicant_ids")
List<Long> applicantIds
) {
}
4 changes: 4 additions & 0 deletions src/main/java/com/cruru/applicant/domain/Applicant.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public Applicant(String name, String email, String phone, Process process) {
this.process = process;
}

public void updateProcess(Process process) {
this.process = process;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/cruru/applicant/service/ApplicantService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.cruru.applicant.service;

import com.cruru.applicant.controller.dto.ApplicantMoveRequest;
import com.cruru.applicant.domain.Applicant;
import com.cruru.applicant.domain.repository.ApplicantRepository;
import com.cruru.process.domain.Process;
import com.cruru.process.domain.repository.ProcessRepository;
import com.cruru.process.exception.ProcessNotFoundException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class ApplicantService {

private final ApplicantRepository applicantRepository;
private final ProcessRepository processRepository;

public void updateApplicantProcess(long processId, ApplicantMoveRequest moveRequest) {
Process process = processRepository.findById(processId)
.orElseThrow(ProcessNotFoundException::new);

List<Applicant> applicants = applicantRepository.findAllById(moveRequest.applicantIds());
applicants.forEach(applicant -> applicant.updateProcess(process));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.cruru.process.exception;

import com.cruru.advice.NotFoundException;

public class ProcessNotFoundException extends NotFoundException {

private static final String MESSAGE = "존재하지 않는 프로세스입니다.";

public ProcessNotFoundException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.cruru.applicant.service;

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

import com.cruru.applicant.controller.dto.ApplicantMoveRequest;
import com.cruru.applicant.domain.Applicant;
import com.cruru.applicant.domain.repository.ApplicantRepository;
import com.cruru.dashboard.domain.Dashboard;
import com.cruru.dashboard.domain.repository.DashboardRepository;
import com.cruru.process.domain.Process;
import com.cruru.process.domain.repository.ProcessRepository;
import java.util.List;
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;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
class ApplicantServiceTest {

@Autowired
private ApplicantService applicantService;

@Autowired
private ApplicantRepository applicantRepository;

@Autowired
private ProcessRepository processRepository;

@Autowired
private DashboardRepository dashboardRepository;

@DisplayName("다건의 지원서를 요청된 프로세스로 일괄 변경한다.")
@Test
void updateApplicantProcess() {
// given
Dashboard dashboard = dashboardRepository.save(new Dashboard(1L, "모집 공고1", null));
Process beforeProcess = new Process(1L, "이전 프로세스", "프로세스 설명1", dashboard);
Process afterProcess = new Process(2L, "이후 프로세스", "프로세스 설명2", dashboard);
List<Process> processes = List.of(beforeProcess, afterProcess);
processRepository.saveAll(processes);
List<Applicant> applicants = List.of(new Applicant(1L, null, null, null, beforeProcess),
new Applicant(2L, null, null, null, beforeProcess), new Applicant(3L, null, null, null, beforeProcess),
new Applicant(4L, null, null, null, beforeProcess), new Applicant(5L, null, null, null, beforeProcess));

// when
List<Long> applicantIds = List.of(1L, 2L, 3L, 4L, 5L);
ApplicantMoveRequest moveRequest = new ApplicantMoveRequest(applicantIds);
applicantService.updateApplicantProcess(2L, moveRequest);

// then
List<Applicant> actualApplicants = applicantRepository.findAllById(applicantIds);
List<Process> actualProcesses = actualApplicants.stream()
.map(Applicant::getProcess)
.toList();
assertThat(actualProcesses.stream()
.allMatch(process -> process.equals(afterProcess))).isTrue();
}
}

0 comments on commit 00407ed

Please sign in to comment.