Skip to content

Commit

Permalink
Refactored and added ShiftService
Browse files Browse the repository at this point in the history
  • Loading branch information
masoudarvishian committed May 10, 2024
1 parent cf05758 commit 84f0118
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -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<Shift> getShifts(UUID id);
void bookTalent(UUID talent, UUID shiftId) throws NotFoundException;
void cancelJob(UUID companyId, UUID jobId) throws NotFoundException;
Optional<Job> getJob(UUID id);
void cancelShift(UUID companyId, UUID shiftId) throws NotFoundException;
Optional<Shift> getShift(UUID id);
void cancelShiftForTalent(UUID companyId, UUID talentId);
}
Original file line number Diff line number Diff line change
@@ -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<Shift> getShifts(UUID id);
void bookTalent(UUID talentId, UUID shiftId) throws NotFoundException;
void cancelShift(UUID companyId, UUID shiftId) throws NotFoundException;
Optional<Shift> getShift(UUID id);
void cancelShiftForTalent(UUID companyId, UUID talentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -39,18 +36,6 @@ public Job createJob(UUID companyId, LocalDate startDate, LocalDate endDate) {
return jobRepository.save(job);
}

public List<Shift> getShifts(UUID id) {
return shiftRepository.findAllByJobId(id);
}

public void bookTalent(UUID talentId, UUID shiftId) throws NotFoundException {
Optional<Shift> 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> job = getJob(jobId);
Expand All @@ -67,31 +52,6 @@ public Optional<Job> getJob(UUID id) {
return jobRepository.findById(id);
}

@Override
public void cancelShift(UUID companyId, UUID shiftId) throws NotFoundException {
Optional<Shift> 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<Shift> getShift(UUID id) {
return shiftRepository.findById(id);
}

@Override
public void cancelShiftForTalent(UUID companyId, UUID talentId) {
List<Shift> 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())
Expand Down Expand Up @@ -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<Shift> getShiftsByTalentIdAndCompanyId(UUID talentId, UUID companyId) {
List<Shift> shifts = shiftRepository.findAllByTalentId(talentId);
return shifts.stream()
.filter(shift -> companyId.equals(shift.getJob().getCompanyId()))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -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<Shift> getShifts(UUID id) {
return shiftRepository.findAllByJobId(id);
}

@Override
public void bookTalent(UUID talentId, UUID shiftId) throws NotFoundException {
Optional<Shift> 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> 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<Shift> getShift(UUID id) {
return shiftRepository.findById(id);
}

@Override
public void cancelShiftForTalent(UUID companyId, UUID talentId) {
List<Shift> shifts = getShiftsByTalentIdAndCompanyId(talentId, companyId);
shifts.forEach(shift -> {
shift.setTalentId(null);
shiftRepository.save(shift);
});
}

private List<Shift> getShiftsByTalentIdAndCompanyId(UUID talentId, UUID companyId) {
List<Shift> shifts = shiftRepository.findAllByTalentId(talentId);
return shifts.stream()
.filter(shift -> companyId.equals(shift.getJob().getCompanyId()))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +17,7 @@
@RequestMapping(path = "/shift")
@RequiredArgsConstructor
public class ShiftController {
private final JobService jobService;
private final ShiftService shiftService;

@GetMapping(path = "/{jobId}")
@ResponseBody
Expand All @@ -33,23 +33,23 @@ public ResponseDto<GetShiftsResponseDto> 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<ShiftResponseDto> getShiftResponses(UUID uuid) {
return jobService.getShifts(uuid).stream()
return shiftService.getShifts(uuid).stream()
.map(shift -> ShiftResponseDto.builder()
.id(shift.getId())
.talentId(shift.getTalentId())
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<Shift> firstShiftById = jobService.getShift(firstShift.getId());
Optional<Shift> secondShiftById = jobService.getShift(secondShift.getId());
Optional<Shift> firstShiftById = shiftService.getShift(firstShift.getId());
Optional<Shift> secondShiftById = shiftService.getShift(secondShift.getId());
Assertions.assertNull(firstShiftById.get().getTalentId());
Assertions.assertNull(secondShiftById.get().getTalentId());
}
Expand Down

0 comments on commit 84f0118

Please sign in to comment.