Skip to content

Commit

Permalink
Refactored to throw NotFoundException when an entity is not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
masoudarvishian committed May 10, 2024
1 parent 307653a commit cf05758
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,10 +12,10 @@
public interface JobService {
Job createJob(UUID companyId, LocalDate startDate, LocalDate endDate);
List<Shift> 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<Job> getJob(UUID id);
void cancelShift(UUID companyId, UUID shiftId);
void cancelShift(UUID companyId, UUID shiftId) throws NotFoundException;
Optional<Shift> getShift(UUID id);
void cancelShiftForTalent(UUID companyId, UUID talentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,15 +43,19 @@ public List<Shift> getShifts(UUID id) {
return shiftRepository.findAllByJobId(id);
}

public void bookTalent(UUID talentId, UUID shiftId) {
public void bookTalent(UUID talentId, UUID shiftId) throws NotFoundException {
Optional<Shift> 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> 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");

Expand All @@ -63,8 +68,10 @@ public Optional<Job> getJob(UUID id) {
}

@Override
public void cancelShift(UUID companyId, UUID shiftId) {
public void cancelShift(UUID companyId, UUID shiftId) throws NotFoundException {
Optional<Shift> 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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -32,7 +33,7 @@ public ResponseDto<RequestJobResponseDto> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -31,7 +32,7 @@ public ResponseDto<GetShiftsResponseDto> 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);
}

Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -30,6 +31,12 @@ ResponseEntity<Object> handleCustomException(InvalidActionException ex) {
return new ResponseEntity<>(errorResponse, HttpStatus.METHOD_NOT_ALLOWED);
}

@ExceptionHandler(NotFoundException.class)
ResponseEntity<Object> handleCustomException(NotFoundException ex) {
ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.NOT_FOUND.value(), ex.getMessage());
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}

@ExceptionHandler(Exception.class)
ResponseEntity<Object> handleAllExceptions(Exception ex) {
ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit cf05758

Please sign in to comment.