[850b344] | 1 | package com.project.beautycenter.service.impl;
|
---|
| 2 |
|
---|
| 3 | import com.project.beautycenter.model.Termini;
|
---|
| 4 | import com.project.beautycenter.model.Vraboteni;
|
---|
| 5 | import com.project.beautycenter.model.exceptions.InvalidTerminIdException;
|
---|
| 6 | import com.project.beautycenter.repository.TerminiRepository;
|
---|
| 7 | import com.project.beautycenter.repository.VraboteniRepository;
|
---|
| 8 | import com.project.beautycenter.service.TerminiService;
|
---|
| 9 | import org.springframework.stereotype.Service;
|
---|
| 10 |
|
---|
| 11 | import java.time.Instant;
|
---|
| 12 | import java.util.List;
|
---|
| 13 |
|
---|
| 14 | @Service
|
---|
| 15 | public class TerminiServiceImpl implements TerminiService {
|
---|
| 16 |
|
---|
| 17 | private final TerminiRepository terminiRepository;
|
---|
| 18 | private final VraboteniRepository vraboteniRepository;
|
---|
| 19 |
|
---|
| 20 | public TerminiServiceImpl(TerminiRepository terminiRepository, VraboteniRepository vraboteniRepository) {
|
---|
| 21 | this.terminiRepository = terminiRepository;
|
---|
| 22 | this.vraboteniRepository = vraboteniRepository;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | @Override
|
---|
| 26 | public List<Termini> findAll() {
|
---|
| 27 | return this.terminiRepository.findAll();
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | @Override
|
---|
| 31 | public Termini findbyId(Integer id) {
|
---|
| 32 | return this.terminiRepository.findById(id).orElseThrow(NullPointerException::new);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public Termini create(Instant pocetok, Integer vremetraenje, Integer vrabotenId) {
|
---|
| 36 | Vraboteni vraboten = this.vraboteniRepository.getById(vrabotenId);
|
---|
| 37 | Termini termin = new Termini(pocetok, vremetraenje, vraboten);
|
---|
| 38 | return this.terminiRepository.save(termin);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | @Override
|
---|
| 42 | public Termini update(Integer id, Instant pocetok, Integer vremetraenje, Integer vrabotenId) {
|
---|
| 43 | Termini termini = this.terminiRepository.findById(id).orElseThrow(InvalidTerminIdException::new);
|
---|
| 44 | Vraboteni vraboten = this.vraboteniRepository.getById(vrabotenId);
|
---|
| 45 | termini.setPocetok(pocetok);
|
---|
| 46 | termini.setVremetraenje(vremetraenje);
|
---|
| 47 | termini.setVraboteni(vraboten);
|
---|
| 48 | return this.terminiRepository.save(termini);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | @Override
|
---|
| 52 | public Termini delete(Integer id) {
|
---|
| 53 | Termini termini = this.terminiRepository.findById(id).orElseThrow(InvalidTerminIdException::new);
|
---|
| 54 | this.terminiRepository.delete(termini);
|
---|
| 55 | return termini;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Override
|
---|
| 59 | public List<Termini> listAllNotReserved() {
|
---|
| 60 | return this.terminiRepository.findAllByRezervacijasNull();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | }
|
---|