-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf05758
commit 84f0118
Showing
6 changed files
with
104 additions
and
69 deletions.
There are no files selected for viewing
7 changes: 0 additions & 7 deletions
7
src/main/java/com/zenjob/challenge/application/interfaces/JobService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/zenjob/challenge/application/interfaces/ShiftService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/com/zenjob/challenge/application/services/ShiftServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters