package com.project.beautycenter.service.impl; import com.project.beautycenter.model.*; import com.project.beautycenter.model.exceptions.InvalidServiceException; import com.project.beautycenter.model.exceptions.ServiceIdReservedException; import com.project.beautycenter.repository.OcenaRepository; import com.project.beautycenter.repository.UslugiRepository; import com.project.beautycenter.repository.VraboteniRepository; import com.project.beautycenter.repository.VraboteniUslugiRepository; import com.project.beautycenter.service.UslugiService; import com.project.beautycenter.service.VraboteniUslugiService; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.util.List; @Service public class UslugiServiceImpl implements UslugiService { private final UslugiRepository uslugiRepository; private final OcenaRepository ocenaRepository; private final VraboteniRepository vraboteniRepository; private final VraboteniUslugiRepository vraboteniUslugiRepository; private final VraboteniUslugiService vraboteniUslugiService; public UslugiServiceImpl(UslugiRepository uslugiRepository, OcenaRepository ocenaRepository, VraboteniRepository vraboteniRepository, VraboteniUslugiRepository vraboteniUslugiRepository, VraboteniUslugiService vraboteniUslugiService) { this.uslugiRepository = uslugiRepository; this.ocenaRepository = ocenaRepository; this.vraboteniRepository = vraboteniRepository; this.vraboteniUslugiRepository = vraboteniUslugiRepository; this.vraboteniUslugiService = vraboteniUslugiService; } @Override public List findAll() { return this.uslugiRepository.findAll(); } @Override public Uslugi findbyId(String id) { return this.uslugiRepository.findById(id).orElseThrow(NullPointerException::new); } @Override public Uslugi findbyDejnost(String dejnost) { return null; } @Override public Uslugi create(String id, String dejnost, String cena, List vraboteniId) { try { Uslugi postoeckaUsluga = this.uslugiRepository.findById(id).orElseThrow(InvalidServiceException::new); throw new ServiceIdReservedException("This id is reserved,try with another!"); } catch (InvalidServiceException ex) { Uslugi usluga = new Uslugi(id, dejnost, cena); List vraboteni = this.vraboteniRepository.findAllById(vraboteniId); this.uslugiRepository.save(usluga); for (int i = 0; i < vraboteni.size(); i++) { VraboteniUslugiId vUid = new VraboteniUslugiId(vraboteni.get(i).getId(), usluga.getId()); VraboteniUslugi vrabU = new VraboteniUslugi(vUid, vraboteni.get(i), usluga); this.vraboteniUslugiRepository.save(vrabU); } return usluga; } } @Override @Transactional public Uslugi update(String id, String dejnost, String cena, List vraboteniId) { Uslugi usluga = this.uslugiRepository.findById(id).orElseThrow(InvalidServiceException::new); usluga.setDejnost(dejnost); usluga.setCena(cena); this.vraboteniUslugiRepository.deleteAllByUsluga(usluga); usluga = this.uslugiRepository.save(usluga); this.addVraboteniForUsluga(usluga, vraboteniId); return usluga; } @Transactional protected void addVraboteniForUsluga(Uslugi usluga, List vraboteniId) { if (vraboteniId != null) { vraboteniId.stream() .map(this::findVrabotenById) .forEach(vraboten -> this.vraboteniUslugiRepository .save(new VraboteniUslugi((new VraboteniUslugiId(vraboten.getId(), usluga.getId())), vraboten, usluga))); } } private Vraboteni findVrabotenById(Integer id) { return this.vraboteniRepository.getById(id); } @Override public Uslugi delete(String id) { Uslugi uslugi = this.uslugiRepository.findById(id).orElseThrow(InvalidServiceException::new); this.vraboteniUslugiService.deletebyUsluga(uslugi.getId()); this.uslugiRepository.delete(uslugi); return uslugi; } @Override public void rate(Uslugi usluga, Integer vrednost, String komentar, Klienti klient) { Ocena o = new Ocena(vrednost, komentar, usluga, klient); this.ocenaRepository.save(o); } @Override public List findAllById(List uslugiIds) { return this.uslugiRepository.findAllById(uslugiIds); } }