package com.project.beautycenter.service.impl; import com.project.beautycenter.model.Termini; import com.project.beautycenter.model.Vraboteni; import com.project.beautycenter.model.exceptions.InvalidTerminIdException; import com.project.beautycenter.repository.TerminiRepository; import com.project.beautycenter.repository.VraboteniRepository; import com.project.beautycenter.service.TerminiService; import org.springframework.stereotype.Service; import java.time.Instant; import java.util.List; @Service public class TerminiServiceImpl implements TerminiService { private final TerminiRepository terminiRepository; private final VraboteniRepository vraboteniRepository; public TerminiServiceImpl(TerminiRepository terminiRepository, VraboteniRepository vraboteniRepository) { this.terminiRepository = terminiRepository; this.vraboteniRepository = vraboteniRepository; } @Override public List findAll() { return this.terminiRepository.findAll(); } @Override public Termini findbyId(Integer id) { return this.terminiRepository.findById(id).orElseThrow(NullPointerException::new); } public Termini create(Instant pocetok, Integer vremetraenje, Integer vrabotenId) { Vraboteni vraboten = this.vraboteniRepository.getById(vrabotenId); Termini termin = new Termini(pocetok, vremetraenje, vraboten); return this.terminiRepository.save(termin); } @Override public Termini update(Integer id, Instant pocetok, Integer vremetraenje, Integer vrabotenId) { Termini termini = this.terminiRepository.findById(id).orElseThrow(InvalidTerminIdException::new); Vraboteni vraboten = this.vraboteniRepository.getById(vrabotenId); termini.setPocetok(pocetok); termini.setVremetraenje(vremetraenje); termini.setVraboteni(vraboten); return this.terminiRepository.save(termini); } @Override public Termini delete(Integer id) { Termini termini = this.terminiRepository.findById(id).orElseThrow(InvalidTerminIdException::new); this.terminiRepository.delete(termini); return termini; } @Override public List listAllNotReserved() { return this.terminiRepository.findAllByRezervacijasNull(); } }