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 ad31c40..86b0682 100644 --- a/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java +++ b/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java @@ -28,28 +28,13 @@ class JobServiceImpl implements JobService { private final JobRepository jobRepository; private final ShiftRepository shiftRepository; - public Job createJob(UUID companyId, LocalDate startDate, LocalDate endDate) { - if (startDate.isBefore(LocalDate.now())) - throw new InvalidStartDateException("Start date cannot be before now"); - - if (endDate.isBefore(startDate)) - throw new InvalidEndDateException("End date cannot be before start date"); + private static final int START_HOUR = 9; + private static final int END_HOUR = 17; - Job job = Job.builder() - .id(UUID.randomUUID()) - .companyId(companyId) - .startTime(startDate.atTime(9, 0, 0).toInstant(ZoneOffset.UTC)) - .endTime(endDate.atTime(17, 0, 0).toInstant(ZoneOffset.UTC)) - .build(); - job.setShifts(LongStream.range(0, ChronoUnit.DAYS.between(startDate, endDate)) - .mapToObj(idx -> startDate.plus(idx, ChronoUnit.DAYS)) - .map(date -> Shift.builder() - .id(UUID.randomUUID()) - .job(job) - .startTime(date.atTime(9, 0, 0).toInstant(ZoneOffset.UTC)) - .endTime(date.atTime(17, 0, 0).toInstant(ZoneOffset.UTC)) - .build()) - .collect(Collectors.toList())); + public Job createJob(UUID companyId, LocalDate startDate, LocalDate endDate) { + validateDates(startDate, endDate); + Job job = buildJob(companyId, startDate, endDate); + setJobShifts(job, startDate, endDate); return jobRepository.save(job); } @@ -100,6 +85,35 @@ public void cancelShiftForTalent(UUID companyId, UUID talentId) { }); } + private static Job buildJob(UUID companyId, LocalDate startDate, LocalDate endDate) { + return Job.builder() + .id(UUID.randomUUID()) + .companyId(companyId) + .startTime(startDate.atTime(START_HOUR, 0, 0).toInstant(ZoneOffset.UTC)) + .endTime(endDate.atTime(END_HOUR, 0, 0).toInstant(ZoneOffset.UTC)) + .build(); + } + + private static void setJobShifts(Job job, LocalDate startDate, LocalDate endDate) { + job.setShifts(LongStream.range(0, ChronoUnit.DAYS.between(startDate, endDate)) + .mapToObj(idx -> startDate.plus(idx, ChronoUnit.DAYS)) + .map(date -> Shift.builder() + .id(UUID.randomUUID()) + .job(job) + .startTime(date.atTime(START_HOUR, 0, 0).toInstant(ZoneOffset.UTC)) + .endTime(date.atTime(END_HOUR, 0, 0).toInstant(ZoneOffset.UTC)) + .build()) + .collect(Collectors.toList())); + } + + private static void validateDates(LocalDate startDate, LocalDate endDate) { + if (startDate.isBefore(LocalDate.now())) + throw new InvalidStartDateException("Start date cannot be before now"); + + if (endDate.isBefore(startDate)) + throw new InvalidEndDateException("End date cannot be before start date"); + } + private List getShiftsByTalentIdAndCompanyId(UUID talentId, UUID companyId) { List shifts = shiftRepository.findAllByTalentId(talentId); return shifts.stream() 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 649b12b..bd2295d 100644 --- a/src/main/java/com/zenjob/challenge/rest/controller/ShiftController.java +++ b/src/main/java/com/zenjob/challenge/rest/controller/ShiftController.java @@ -21,15 +21,7 @@ public class ShiftController { @GetMapping(path = "/{jobId}") @ResponseBody public ResponseDto getShifts(@PathVariable("jobId") UUID uuid) { - List shiftResponses = jobService.getShifts(uuid).stream() - .map(shift -> ShiftResponseDto.builder() - .id(shift.getId()) - .talentId(shift.getTalentId()) - .jobId(shift.getJob().getId()) - .start(shift.getCreatedAt()) - .end(shift.getEndTime()) - .build()) - .collect(Collectors.toList()); + List shiftResponses = getShiftResponses(uuid); return ResponseDto.builder() .data(GetShiftsResponseDto.builder() .shifts(shiftResponses) @@ -54,4 +46,16 @@ public void cancelShiftForTalent(@RequestBody @Valid CancelShiftForTalentRequest public void cancelShift(@PathVariable("id") UUID shiftId, @RequestBody @Valid CancelShiftRequestDto dto) { jobService.cancelShift(dto.getCompanyId(), shiftId); } + + private List getShiftResponses(UUID uuid) { + return jobService.getShifts(uuid).stream() + .map(shift -> ShiftResponseDto.builder() + .id(shift.getId()) + .talentId(shift.getTalentId()) + .jobId(shift.getJob().getId()) + .start(shift.getCreatedAt()) + .end(shift.getEndTime()) + .build()) + .collect(Collectors.toList()); + } }