From cf057583ed14f3d120aeebfbf6379a03c8b8009f Mon Sep 17 00:00:00 2001 From: masoudarvishian Date: Fri, 10 May 2024 16:19:02 +0200 Subject: [PATCH] Refactored to throw NotFoundException when an entity is not exist --- .../application/interfaces/JobService.java | 7 ++++--- .../application/services/JobServiceImpl.java | 13 ++++++++++--- .../challenge/rest/controller/JobController.java | 3 ++- .../challenge/rest/controller/ShiftController.java | 5 +++-- .../exceptionhandler/GlobalExceptionHandler.java | 7 +++++++ .../application/services/JobServiceTests.java | 7 ++++--- 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/zenjob/challenge/application/interfaces/JobService.java b/src/main/java/com/zenjob/challenge/application/interfaces/JobService.java index 283b73f..915b95f 100644 --- a/src/main/java/com/zenjob/challenge/application/interfaces/JobService.java +++ b/src/main/java/com/zenjob/challenge/application/interfaces/JobService.java @@ -2,6 +2,7 @@ import com.zenjob.challenge.domain.entity.Job; import com.zenjob.challenge.domain.entity.Shift; +import javassist.NotFoundException; import java.time.LocalDate; import java.util.List; @@ -11,10 +12,10 @@ public interface JobService { Job createJob(UUID companyId, LocalDate startDate, LocalDate endDate); List getShifts(UUID id); - void bookTalent(UUID talent, UUID shiftId); - void cancelJob(UUID companyId, UUID jobId); + void bookTalent(UUID talent, UUID shiftId) throws NotFoundException; + void cancelJob(UUID companyId, UUID jobId) throws NotFoundException; Optional getJob(UUID id); - void cancelShift(UUID companyId, UUID shiftId); + void cancelShift(UUID companyId, UUID shiftId) throws NotFoundException; Optional getShift(UUID id); void cancelShiftForTalent(UUID companyId, UUID talentId); } diff --git a/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java b/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java index 86b0682..b5e0b82 100644 --- a/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java +++ b/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java @@ -8,6 +8,7 @@ import com.zenjob.challenge.domain.entity.Shift; import com.zenjob.challenge.repository.JobRepository; import com.zenjob.challenge.repository.ShiftRepository; +import javassist.NotFoundException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -42,15 +43,19 @@ public List getShifts(UUID id) { return shiftRepository.findAllByJobId(id); } - public void bookTalent(UUID talentId, UUID shiftId) { + public void bookTalent(UUID talentId, UUID shiftId) throws NotFoundException { Optional shiftById = shiftRepository.findById(shiftId); + if (!shiftById.isPresent()) + throw new NotFoundException("Shift not found!"); shiftById.get().setTalentId(talentId); shiftRepository.save(shiftById.get()); } @Override - public void cancelJob(UUID companyId, UUID jobId) { + public void cancelJob(UUID companyId, UUID jobId) throws NotFoundException { Optional job = getJob(jobId); + if (!job.isPresent()) + throw new NotFoundException("Job not found!"); if (!job.get().getCompanyId().equals(companyId)) throw new InvalidActionException("You cannot cancel job of other companies"); @@ -63,8 +68,10 @@ public Optional getJob(UUID id) { } @Override - public void cancelShift(UUID companyId, UUID shiftId) { + public void cancelShift(UUID companyId, UUID shiftId) throws NotFoundException { Optional shift = getShift(shiftId); + if (!shift.isPresent()) + throw new NotFoundException("Shift not found!"); if (!shift.get().getJob().getCompanyId().equals(companyId)) throw new InvalidActionException("You cannot cancel shift of other companies"); diff --git a/src/main/java/com/zenjob/challenge/rest/controller/JobController.java b/src/main/java/com/zenjob/challenge/rest/controller/JobController.java index 1b07f43..f5dd5f6 100644 --- a/src/main/java/com/zenjob/challenge/rest/controller/JobController.java +++ b/src/main/java/com/zenjob/challenge/rest/controller/JobController.java @@ -6,6 +6,7 @@ import com.zenjob.challenge.rest.dto.ResponseDto; import com.zenjob.challenge.domain.entity.Job; import com.zenjob.challenge.application.interfaces.JobService; +import javassist.NotFoundException; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; @@ -32,7 +33,7 @@ public ResponseDto requestJob(@RequestBody @Valid Request @DeleteMapping(path = "/{id}/cancel") @ResponseStatus(code = HttpStatus.NO_CONTENT) - public void cancelJob(@PathVariable("id") UUID jobId, @RequestBody @Valid CancelJobRequestDto dto) { + public void cancelJob(@PathVariable("id") UUID jobId, @RequestBody @Valid CancelJobRequestDto dto) throws NotFoundException { jobService.cancelJob(dto.getCompanyId(), jobId); } } diff --git a/src/main/java/com/zenjob/challenge/rest/controller/ShiftController.java b/src/main/java/com/zenjob/challenge/rest/controller/ShiftController.java index bd2295d..1e65849 100644 --- a/src/main/java/com/zenjob/challenge/rest/controller/ShiftController.java +++ b/src/main/java/com/zenjob/challenge/rest/controller/ShiftController.java @@ -3,6 +3,7 @@ import com.zenjob.challenge.rest.dto.ResponseDto; import com.zenjob.challenge.application.interfaces.JobService; import com.zenjob.challenge.rest.dto.shift.*; +import javassist.NotFoundException; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; @@ -31,7 +32,7 @@ public ResponseDto getShifts(@PathVariable("jobId") UUID u @PatchMapping(path = "/{id}/book") @ResponseStatus(code = HttpStatus.NO_CONTENT) - public void bookTalent(@PathVariable("id") UUID shiftId, @RequestBody @Valid BookTalentRequestDto dto) { + public void bookTalent(@PathVariable("id") UUID shiftId, @RequestBody @Valid BookTalentRequestDto dto) throws NotFoundException { jobService.bookTalent(dto.getTalentId(), shiftId); } @@ -43,7 +44,7 @@ public void cancelShiftForTalent(@RequestBody @Valid CancelShiftForTalentRequest @DeleteMapping(path = "/{id}/cancel") @ResponseStatus(code = HttpStatus.NO_CONTENT) - public void cancelShift(@PathVariable("id") UUID shiftId, @RequestBody @Valid CancelShiftRequestDto dto) { + public void cancelShift(@PathVariable("id") UUID shiftId, @RequestBody @Valid CancelShiftRequestDto dto) throws NotFoundException { jobService.cancelShift(dto.getCompanyId(), shiftId); } diff --git a/src/main/java/com/zenjob/challenge/rest/exceptionhandler/GlobalExceptionHandler.java b/src/main/java/com/zenjob/challenge/rest/exceptionhandler/GlobalExceptionHandler.java index 7b70486..ec78ddf 100644 --- a/src/main/java/com/zenjob/challenge/rest/exceptionhandler/GlobalExceptionHandler.java +++ b/src/main/java/com/zenjob/challenge/rest/exceptionhandler/GlobalExceptionHandler.java @@ -3,6 +3,7 @@ import com.zenjob.challenge.domain.exceptions.InvalidActionException; import com.zenjob.challenge.domain.exceptions.InvalidEndDateException; import com.zenjob.challenge.domain.exceptions.InvalidStartDateException; +import javassist.NotFoundException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -30,6 +31,12 @@ ResponseEntity handleCustomException(InvalidActionException ex) { return new ResponseEntity<>(errorResponse, HttpStatus.METHOD_NOT_ALLOWED); } + @ExceptionHandler(NotFoundException.class) + ResponseEntity handleCustomException(NotFoundException ex) { + ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.NOT_FOUND.value(), ex.getMessage()); + return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND); + } + @ExceptionHandler(Exception.class) ResponseEntity handleAllExceptions(Exception ex) { ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage()); diff --git a/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java b/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java index a0ef964..9927719 100644 --- a/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java +++ b/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java @@ -6,6 +6,7 @@ import com.zenjob.challenge.domain.entity.Job; import com.zenjob.challenge.domain.entity.Shift; import com.zenjob.challenge.application.interfaces.JobService; +import javassist.NotFoundException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -92,7 +93,7 @@ public void job_hours_should_not_be_out_of_scope() { } @Test - public void cancel_a_job_and_its_shifts_by_company() { + public void cancel_a_job_and_its_shifts_by_company() throws NotFoundException { // given LocalDate startDate = LocalDate.now(); LocalDate endDate = LocalDate.now().plusDays(5); @@ -120,7 +121,7 @@ public void a_company_can_only_cancel_its_own_jobs() { } @Test - public void cancel_a_single_shift_by_company() { + public void cancel_a_single_shift_by_company() throws NotFoundException { // given LocalDate startDate = LocalDate.now(); LocalDate endDate = LocalDate.now().plusDays(5); @@ -149,7 +150,7 @@ public void a_company_can_only_cancel_its_own_shifts() { } @Test - public void cancel_shift_for_a_talent() { + public void cancel_shift_for_a_talent() throws NotFoundException { // given LocalDate startDate = LocalDate.now(); LocalDate endDate = LocalDate.now().plusDays(5);