Skip to content

Commit

Permalink
Refactored JobServiceImpl.java & ShiftController.java
Browse files Browse the repository at this point in the history
  • Loading branch information
masoudarvishian committed May 10, 2024
1 parent 7d769c0 commit 307653a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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<Shift> getShiftsByTalentIdAndCompanyId(UUID talentId, UUID companyId) {
List<Shift> shifts = shiftRepository.findAllByTalentId(talentId);
return shifts.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,7 @@ public class ShiftController {
@GetMapping(path = "/{jobId}")
@ResponseBody
public ResponseDto<GetShiftsResponseDto> getShifts(@PathVariable("jobId") UUID uuid) {
List<ShiftResponseDto> 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<ShiftResponseDto> shiftResponses = getShiftResponses(uuid);
return ResponseDto.<GetShiftsResponseDto>builder()
.data(GetShiftsResponseDto.builder()
.shifts(shiftResponses)
Expand All @@ -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<ShiftResponseDto> 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());
}
}

0 comments on commit 307653a

Please sign in to comment.