From ea04e032aac39bf07ef1bcb68b263231b7796853 Mon Sep 17 00:00:00 2001 From: masoudarvishian Date: Fri, 10 May 2024 11:32:17 +0200 Subject: [PATCH] Refactored and added end point for cancelling talent shifts --- .../challenge/controller/ShiftController.java | 15 ++++++++++++++- .../com/zenjob/challenge/service/IJobService.java | 2 +- .../com/zenjob/challenge/service/JobService.java | 8 ++++++-- .../com/zenjob/challenge/JobServiceTests.java | 2 +- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/zenjob/challenge/controller/ShiftController.java b/src/main/java/com/zenjob/challenge/controller/ShiftController.java index 0d2901c..504c106 100644 --- a/src/main/java/com/zenjob/challenge/controller/ShiftController.java +++ b/src/main/java/com/zenjob/challenge/controller/ShiftController.java @@ -44,7 +44,7 @@ public ResponseDto getShifts(@PathVariable("jobId") UUID uuid @PatchMapping(path = "/{id}/book") @ResponseStatus(code = HttpStatus.NO_CONTENT) public void bookTalent(@PathVariable("id") UUID shiftId, @RequestBody @Valid ShiftController.BookTalentRequestDto dto) { - jobService.bookTalent(shiftId, dto.talent); + jobService.bookTalent(dto.talent, shiftId); } @DeleteMapping(path = "/{id}/cancel") @@ -53,12 +53,25 @@ public void cancelShift(@PathVariable("id") UUID shiftId, @RequestBody @Valid Sh jobService.cancelShift(dto.companyId, shiftId); } + @DeleteMapping(path = "/cancel-talent") + @ResponseStatus(code = HttpStatus.NO_CONTENT) + public void cancelShiftForTalent(@RequestBody @Valid ShiftController.CancelShiftForTalentRequestDto dto) { + jobService.cancelShiftForTalent(dto.companyId, dto.talentId); + } + @NoArgsConstructor @Data private static class CancelShiftRequestDto { UUID companyId; } + @NoArgsConstructor + @Data + private static class CancelShiftForTalentRequestDto { + UUID talentId; + UUID companyId; + } + @NoArgsConstructor @Data private static class BookTalentRequestDto { diff --git a/src/main/java/com/zenjob/challenge/service/IJobService.java b/src/main/java/com/zenjob/challenge/service/IJobService.java index 47d8ec7..3db880d 100644 --- a/src/main/java/com/zenjob/challenge/service/IJobService.java +++ b/src/main/java/com/zenjob/challenge/service/IJobService.java @@ -16,5 +16,5 @@ public interface IJobService { Optional getJob(UUID id); void cancelShift(UUID companyId, UUID shiftId); Optional getShift(UUID id); - void cancelShiftForTalent(UUID companyId, UUID talentId, UUID shiftId); + void cancelShiftForTalent(UUID companyId, UUID talentId); } diff --git a/src/main/java/com/zenjob/challenge/service/JobService.java b/src/main/java/com/zenjob/challenge/service/JobService.java index 0637756..5a3d227 100644 --- a/src/main/java/com/zenjob/challenge/service/JobService.java +++ b/src/main/java/com/zenjob/challenge/service/JobService.java @@ -57,8 +57,12 @@ public List getShifts(UUID id) { } public void bookTalent(UUID talentId, UUID shiftId) { +// Optional shiftById = shiftRepository.findById(shiftId); +// shiftById.map(shift -> shiftRepository.save(shift.setTalentId(talentId))); + Optional shiftById = shiftRepository.findById(shiftId); - shiftById.map(shift -> shiftRepository.save(shift.setTalentId(talentId))); + shiftById.get().setTalentId(talentId); + shiftRepository.save(shiftById.get()); } @Override @@ -90,7 +94,7 @@ public Optional getShift(UUID id) { } @Override - public void cancelShiftForTalent(UUID companyId, UUID talentId, UUID shiftId) { + public void cancelShiftForTalent(UUID companyId, UUID talentId) { List shifts = getShiftsByTalentIdAndCompanyId(talentId, companyId); shifts.forEach(shift -> { shift.setTalentId(null); diff --git a/src/test/java/com/zenjob/challenge/JobServiceTests.java b/src/test/java/com/zenjob/challenge/JobServiceTests.java index ddcedc7..6907215 100644 --- a/src/test/java/com/zenjob/challenge/JobServiceTests.java +++ b/src/test/java/com/zenjob/challenge/JobServiceTests.java @@ -161,7 +161,7 @@ public void cancel_shift_for_a_talent() { jobService.bookTalent(talentId, secondShift.getId()); // when - jobService.cancelShiftForTalent(job.getCompanyId(), talentId, firstShift.getId()); + jobService.cancelShiftForTalent(job.getCompanyId(), talentId); // then Optional firstShiftById = jobService.getShift(firstShift.getId());