diff --git a/src/main/java/com/zenjob/challenge/application/interfaces/JobService.java b/src/main/java/com/zenjob/challenge/application/interfaces/JobService.java index 915b95f..6176600 100644 --- a/src/main/java/com/zenjob/challenge/application/interfaces/JobService.java +++ b/src/main/java/com/zenjob/challenge/application/interfaces/JobService.java @@ -1,21 +1,14 @@ package com.zenjob.challenge.application.interfaces; 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; import java.util.Optional; import java.util.UUID; public interface JobService { Job createJob(UUID companyId, LocalDate startDate, LocalDate endDate); - List getShifts(UUID id); - void bookTalent(UUID talent, UUID shiftId) throws NotFoundException; void cancelJob(UUID companyId, UUID jobId) throws NotFoundException; Optional getJob(UUID id); - void cancelShift(UUID companyId, UUID shiftId) throws NotFoundException; - Optional getShift(UUID id); - void cancelShiftForTalent(UUID companyId, UUID talentId); } diff --git a/src/main/java/com/zenjob/challenge/application/interfaces/ShiftService.java b/src/main/java/com/zenjob/challenge/application/interfaces/ShiftService.java new file mode 100644 index 0000000..609fa49 --- /dev/null +++ b/src/main/java/com/zenjob/challenge/application/interfaces/ShiftService.java @@ -0,0 +1,16 @@ +package com.zenjob.challenge.application.interfaces; + +import com.zenjob.challenge.domain.entity.Shift; +import javassist.NotFoundException; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public interface ShiftService { + List getShifts(UUID id); + void bookTalent(UUID talentId, UUID shiftId) throws NotFoundException; + void cancelShift(UUID companyId, UUID shiftId) throws NotFoundException; + Optional getShift(UUID id); + void cancelShiftForTalent(UUID companyId, UUID talentId); +} 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 b5e0b82..a7cd014 100644 --- a/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java +++ b/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java @@ -7,7 +7,6 @@ import com.zenjob.challenge.domain.entity.Job; 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; @@ -16,7 +15,6 @@ import java.time.LocalDate; import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; -import java.util.List; import java.util.Optional; import java.util.UUID; import java.util.stream.Collectors; @@ -27,7 +25,6 @@ @Transactional class JobServiceImpl implements JobService { private final JobRepository jobRepository; - private final ShiftRepository shiftRepository; private static final int START_HOUR = 9; private static final int END_HOUR = 17; @@ -39,18 +36,6 @@ public Job createJob(UUID companyId, LocalDate startDate, LocalDate endDate) { return jobRepository.save(job); } - public List getShifts(UUID id) { - return shiftRepository.findAllByJobId(id); - } - - public void bookTalent(UUID talentId, UUID shiftId) throws NotFoundException { - Optional 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) throws NotFoundException { Optional job = getJob(jobId); @@ -67,31 +52,6 @@ public Optional getJob(UUID id) { return jobRepository.findById(id); } - @Override - public void cancelShift(UUID companyId, UUID shiftId) throws NotFoundException { - Optional 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"); - - shiftRepository.deleteById(shiftId); - } - - @Override - public Optional getShift(UUID id) { - return shiftRepository.findById(id); - } - - @Override - public void cancelShiftForTalent(UUID companyId, UUID talentId) { - List shifts = getShiftsByTalentIdAndCompanyId(talentId, companyId); - shifts.forEach(shift -> { - shift.setTalentId(null); - shiftRepository.save(shift); - }); - } - private static Job buildJob(UUID companyId, LocalDate startDate, LocalDate endDate) { return Job.builder() .id(UUID.randomUUID()) @@ -120,11 +80,4 @@ private static void validateDates(LocalDate startDate, LocalDate endDate) { 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() - .filter(shift -> companyId.equals(shift.getJob().getCompanyId())) - .collect(Collectors.toList()); - } } diff --git a/src/main/java/com/zenjob/challenge/application/services/ShiftServiceImpl.java b/src/main/java/com/zenjob/challenge/application/services/ShiftServiceImpl.java new file mode 100644 index 0000000..957d217 --- /dev/null +++ b/src/main/java/com/zenjob/challenge/application/services/ShiftServiceImpl.java @@ -0,0 +1,69 @@ +package com.zenjob.challenge.application.services; + +import com.zenjob.challenge.application.interfaces.ShiftService; +import com.zenjob.challenge.domain.entity.Shift; +import com.zenjob.challenge.domain.exceptions.InvalidActionException; +import com.zenjob.challenge.repository.ShiftRepository; +import javassist.NotFoundException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; + +@RequiredArgsConstructor +@Service +@Transactional +public class ShiftServiceImpl implements ShiftService { + + private final ShiftRepository shiftRepository; + + @Override + public List getShifts(UUID id) { + return shiftRepository.findAllByJobId(id); + } + + @Override + public void bookTalent(UUID talentId, UUID shiftId) throws NotFoundException { + Optional shiftById = shiftRepository.findById(shiftId); + if (!shiftById.isPresent()) + throw new NotFoundException("Shift not found!"); + shiftById.get().setTalentId(talentId); + shiftRepository.save(shiftById.get()); + } + + @Override + public void cancelShift(UUID companyId, UUID shiftId) throws NotFoundException { + Optional 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"); + + shiftRepository.deleteById(shiftId); + } + + @Override + public Optional getShift(UUID id) { + return shiftRepository.findById(id); + } + + @Override + public void cancelShiftForTalent(UUID companyId, UUID talentId) { + List shifts = getShiftsByTalentIdAndCompanyId(talentId, companyId); + shifts.forEach(shift -> { + shift.setTalentId(null); + shiftRepository.save(shift); + }); + } + + private List getShiftsByTalentIdAndCompanyId(UUID talentId, UUID companyId) { + List shifts = shiftRepository.findAllByTalentId(talentId); + return shifts.stream() + .filter(shift -> companyId.equals(shift.getJob().getCompanyId())) + .collect(Collectors.toList()); + } +} 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 1e65849..d184e5c 100644 --- a/src/main/java/com/zenjob/challenge/rest/controller/ShiftController.java +++ b/src/main/java/com/zenjob/challenge/rest/controller/ShiftController.java @@ -1,7 +1,7 @@ package com.zenjob.challenge.rest.controller; +import com.zenjob.challenge.application.interfaces.ShiftService; 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; @@ -17,7 +17,7 @@ @RequestMapping(path = "/shift") @RequiredArgsConstructor public class ShiftController { - private final JobService jobService; + private final ShiftService shiftService; @GetMapping(path = "/{jobId}") @ResponseBody @@ -33,23 +33,23 @@ public ResponseDto 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) throws NotFoundException { - jobService.bookTalent(dto.getTalentId(), shiftId); + shiftService.bookTalent(dto.getTalentId(), shiftId); } @PatchMapping(path = "/cancel-talent") @ResponseStatus(code = HttpStatus.NO_CONTENT) public void cancelShiftForTalent(@RequestBody @Valid CancelShiftForTalentRequestDto dto) { - jobService.cancelShiftForTalent(dto.getCompanyId(), dto.getTalentId()); + shiftService.cancelShiftForTalent(dto.getCompanyId(), dto.getTalentId()); } @DeleteMapping(path = "/{id}/cancel") @ResponseStatus(code = HttpStatus.NO_CONTENT) public void cancelShift(@PathVariable("id") UUID shiftId, @RequestBody @Valid CancelShiftRequestDto dto) throws NotFoundException { - jobService.cancelShift(dto.getCompanyId(), shiftId); + shiftService.cancelShift(dto.getCompanyId(), shiftId); } private List getShiftResponses(UUID uuid) { - return jobService.getShifts(uuid).stream() + return shiftService.getShifts(uuid).stream() .map(shift -> ShiftResponseDto.builder() .id(shift.getId()) .talentId(shift.getTalentId()) diff --git a/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java b/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java index 9927719..fd6f35f 100644 --- a/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java +++ b/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java @@ -1,5 +1,6 @@ package com.zenjob.challenge.application.services; +import com.zenjob.challenge.application.interfaces.ShiftService; import com.zenjob.challenge.domain.exceptions.InvalidActionException; import com.zenjob.challenge.domain.exceptions.InvalidEndDateException; import com.zenjob.challenge.domain.exceptions.InvalidStartDateException; @@ -25,6 +26,9 @@ public class JobServiceTests { @Autowired private JobService jobService; + @Autowired + private ShiftService shiftService; + @Test public void job_should_have_at_least_one_shift() { // given @@ -104,7 +108,7 @@ public void cancel_a_job_and_its_shifts_by_company() throws NotFoundException { // then Assertions.assertFalse(jobService.getJob(job.getId()).isPresent()); - Assertions.assertTrue(jobService.getShifts(job.getId()).isEmpty()); + Assertions.assertTrue(shiftService.getShifts(job.getId()).isEmpty()); } @Test @@ -129,10 +133,10 @@ public void cancel_a_single_shift_by_company() throws NotFoundException { Shift firstShift = job.getShifts().get(0); // when - jobService.cancelShift(job.getCompanyId(), firstShift.getId()); + shiftService.cancelShift(job.getCompanyId(), firstShift.getId()); // then - Assertions.assertFalse(jobService.getShift(firstShift.getId()).isPresent()); + Assertions.assertFalse(shiftService.getShift(firstShift.getId()).isPresent()); } @Test @@ -146,7 +150,7 @@ public void a_company_can_only_cancel_its_own_shifts() { // when - then Assertions.assertThrows(InvalidActionException.class, () -> - jobService.cancelShift(companyId, firstShift.getId())); + shiftService.cancelShift(companyId, firstShift.getId())); } @Test @@ -158,15 +162,15 @@ public void cancel_shift_for_a_talent() throws NotFoundException { Shift firstShift = job.getShifts().get(0); Shift secondShift = job.getShifts().get(1); UUID talentId = UUID.randomUUID(); - jobService.bookTalent(talentId, firstShift.getId()); - jobService.bookTalent(talentId, secondShift.getId()); + shiftService.bookTalent(talentId, firstShift.getId()); + shiftService.bookTalent(talentId, secondShift.getId()); // when - jobService.cancelShiftForTalent(job.getCompanyId(), talentId); + shiftService.cancelShiftForTalent(job.getCompanyId(), talentId); // then - Optional firstShiftById = jobService.getShift(firstShift.getId()); - Optional secondShiftById = jobService.getShift(secondShift.getId()); + Optional firstShiftById = shiftService.getShift(firstShift.getId()); + Optional secondShiftById = shiftService.getShift(secondShift.getId()); Assertions.assertNull(firstShiftById.get().getTalentId()); Assertions.assertNull(secondShiftById.get().getTalentId()); }