source: src/main/java/com/project/beautycenter/service/impl/UslugiServiceImpl.java

Last change on this file was 850b344, checked in by Tamara Simikj <tamara.simic12@…>, 2 years ago

Initial commit

  • Property mode set to 100644
File size: 4.5 KB
Line 
1package com.project.beautycenter.service.impl;
2
3import com.project.beautycenter.model.*;
4import com.project.beautycenter.model.exceptions.InvalidServiceException;
5import com.project.beautycenter.model.exceptions.ServiceIdReservedException;
6import com.project.beautycenter.repository.OcenaRepository;
7import com.project.beautycenter.repository.UslugiRepository;
8import com.project.beautycenter.repository.VraboteniRepository;
9import com.project.beautycenter.repository.VraboteniUslugiRepository;
10import com.project.beautycenter.service.UslugiService;
11import com.project.beautycenter.service.VraboteniUslugiService;
12import org.springframework.stereotype.Service;
13
14import javax.transaction.Transactional;
15import java.util.List;
16
17@Service
18public class UslugiServiceImpl implements UslugiService {
19 private final UslugiRepository uslugiRepository;
20 private final OcenaRepository ocenaRepository;
21 private final VraboteniRepository vraboteniRepository;
22 private final VraboteniUslugiRepository vraboteniUslugiRepository;
23 private final VraboteniUslugiService vraboteniUslugiService;
24
25 public UslugiServiceImpl(UslugiRepository uslugiRepository, OcenaRepository ocenaRepository, VraboteniRepository vraboteniRepository, VraboteniUslugiRepository vraboteniUslugiRepository, VraboteniUslugiService vraboteniUslugiService) {
26 this.uslugiRepository = uslugiRepository;
27 this.ocenaRepository = ocenaRepository;
28 this.vraboteniRepository = vraboteniRepository;
29 this.vraboteniUslugiRepository = vraboteniUslugiRepository;
30 this.vraboteniUslugiService = vraboteniUslugiService;
31 }
32
33 @Override
34 public List<Uslugi> findAll() {
35 return this.uslugiRepository.findAll();
36 }
37
38 @Override
39 public Uslugi findbyId(String id) {
40 return this.uslugiRepository.findById(id).orElseThrow(NullPointerException::new);
41 }
42
43 @Override
44 public Uslugi findbyDejnost(String dejnost) {
45 return null;
46 }
47
48 @Override
49 public Uslugi create(String id, String dejnost, String cena, List<Integer> vraboteniId) {
50 try {
51 Uslugi postoeckaUsluga = this.uslugiRepository.findById(id).orElseThrow(InvalidServiceException::new);
52 throw new ServiceIdReservedException("This id is reserved,try with another!");
53 } catch (InvalidServiceException ex) {
54 Uslugi usluga = new Uslugi(id, dejnost, cena);
55 List<Vraboteni> vraboteni = this.vraboteniRepository.findAllById(vraboteniId);
56 this.uslugiRepository.save(usluga);
57 for (int i = 0; i < vraboteni.size(); i++) {
58 VraboteniUslugiId vUid = new VraboteniUslugiId(vraboteni.get(i).getId(), usluga.getId());
59 VraboteniUslugi vrabU = new VraboteniUslugi(vUid, vraboteni.get(i), usluga);
60 this.vraboteniUslugiRepository.save(vrabU);
61 }
62
63 return usluga;
64 }
65
66 }
67
68 @Override
69 @Transactional
70 public Uslugi update(String id, String dejnost, String cena, List<Integer> vraboteniId) {
71 Uslugi usluga = this.uslugiRepository.findById(id).orElseThrow(InvalidServiceException::new);
72 usluga.setDejnost(dejnost);
73 usluga.setCena(cena);
74 this.vraboteniUslugiRepository.deleteAllByUsluga(usluga);
75 usluga = this.uslugiRepository.save(usluga);
76 this.addVraboteniForUsluga(usluga, vraboteniId);
77 return usluga;
78
79 }
80
81 @Transactional
82 protected void addVraboteniForUsluga(Uslugi usluga, List<Integer> vraboteniId) {
83 if (vraboteniId != null) {
84 vraboteniId.stream()
85 .map(this::findVrabotenById)
86 .forEach(vraboten -> this.vraboteniUslugiRepository
87 .save(new VraboteniUslugi((new VraboteniUslugiId(vraboten.getId(), usluga.getId())), vraboten, usluga)));
88 }
89 }
90
91 private Vraboteni findVrabotenById(Integer id) {
92 return this.vraboteniRepository.getById(id);
93 }
94
95 @Override
96 public Uslugi delete(String id) {
97 Uslugi uslugi = this.uslugiRepository.findById(id).orElseThrow(InvalidServiceException::new);
98 this.vraboteniUslugiService.deletebyUsluga(uslugi.getId());
99 this.uslugiRepository.delete(uslugi);
100 return uslugi;
101 }
102
103 @Override
104 public void rate(Uslugi usluga, Integer vrednost, String komentar, Klienti klient) {
105 Ocena o = new Ocena(vrednost, komentar, usluga, klient);
106 this.ocenaRepository.save(o);
107 }
108
109 @Override
110 public List<Uslugi> findAllById(List<String> uslugiIds) {
111 return this.uslugiRepository.findAllById(uslugiIds);
112 }
113
114}
Note: See TracBrowser for help on using the repository browser.