Skip to content

Commit

Permalink
Refactored and added end point for cancelling talent shifts
Browse files Browse the repository at this point in the history
  • Loading branch information
masoudarvishian committed May 10, 2024
1 parent 8d318e6 commit ea04e03
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ResponseDto<GetShiftsResponse> 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")
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public interface IJobService {
Optional<Job> getJob(UUID id);
void cancelShift(UUID companyId, UUID shiftId);
Optional<Shift> getShift(UUID id);
void cancelShiftForTalent(UUID companyId, UUID talentId, UUID shiftId);
void cancelShiftForTalent(UUID companyId, UUID talentId);
}
8 changes: 6 additions & 2 deletions src/main/java/com/zenjob/challenge/service/JobService.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ public List<Shift> getShifts(UUID id) {
}

public void bookTalent(UUID talentId, UUID shiftId) {
// Optional<Shift> shiftById = shiftRepository.findById(shiftId);
// shiftById.map(shift -> shiftRepository.save(shift.setTalentId(talentId)));

Optional<Shift> shiftById = shiftRepository.findById(shiftId);
shiftById.map(shift -> shiftRepository.save(shift.setTalentId(talentId)));
shiftById.get().setTalentId(talentId);
shiftRepository.save(shiftById.get());
}

@Override
Expand Down Expand Up @@ -90,7 +94,7 @@ public Optional<Shift> getShift(UUID id) {
}

@Override
public void cancelShiftForTalent(UUID companyId, UUID talentId, UUID shiftId) {
public void cancelShiftForTalent(UUID companyId, UUID talentId) {
List<Shift> shifts = getShiftsByTalentIdAndCompanyId(talentId, companyId);
shifts.forEach(shift -> {
shift.setTalentId(null);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/zenjob/challenge/JobServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Shift> firstShiftById = jobService.getShift(firstShift.getId());
Expand Down

0 comments on commit ea04e03

Please sign in to comment.