1 | package com.example.eatys_app.service;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.eatys_app.model.Cena;
|
---|
5 | import com.example.eatys_app.model.Meni;
|
---|
6 | import com.example.eatys_app.model.Obrok;
|
---|
7 | import com.example.eatys_app.model.exceptions.InvalidMeniIdException;
|
---|
8 | import com.example.eatys_app.model.exceptions.InvalidObrokIdException;
|
---|
9 | import com.example.eatys_app.model.exceptions.InvalidUniqueIdException;
|
---|
10 | import com.example.eatys_app.repository.CenaRepository;
|
---|
11 | import com.example.eatys_app.repository.MeniRepository;
|
---|
12 | import com.example.eatys_app.repository.ObrokRepository;
|
---|
13 | import org.springframework.stereotype.Service;
|
---|
14 |
|
---|
15 | import java.util.Date;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | @Service
|
---|
19 | public class CenaServiceImpl implements CenaService{
|
---|
20 |
|
---|
21 | private final CenaRepository cenaRepository;
|
---|
22 | private final ObrokRepository obrokRepository;
|
---|
23 | private final MeniRepository meniRepository;
|
---|
24 |
|
---|
25 | public CenaServiceImpl(CenaRepository cenaRepository, ObrokRepository obrokRepository, MeniRepository meniRepository) {
|
---|
26 | this.cenaRepository = cenaRepository;
|
---|
27 | this.obrokRepository = obrokRepository;
|
---|
28 | this.meniRepository = meniRepository;
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public Cena findbyUniqueId(Integer uid) {
|
---|
34 | return this.cenaRepository.findById(uid).orElseThrow(InvalidUniqueIdException::new);
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public List<Cena> listAll() {
|
---|
39 | return this.cenaRepository.findAll();
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public Obrok findById(Integer id) {
|
---|
44 | return this.obrokRepository.findById(id).orElseThrow(InvalidObrokIdException::new);
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public Cena create(Integer cenaId,Integer obrokId, Integer cenaIznos, Date cenaVaziOd, Date cenaVaziDo) {
|
---|
49 | Obrok obrok= this.obrokRepository.findById(obrokId).orElseThrow(InvalidObrokIdException::new);
|
---|
50 | Cena cena = new Cena(cenaId,obrok,cenaIznos,cenaVaziOd,cenaVaziDo);
|
---|
51 | return this.cenaRepository.save(cena);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public Cena update(Integer id, Integer obrokId, Integer cenaIznos, Date cenaVaziOd, Date cenaVaziDo) {
|
---|
56 | Obrok obrok= this.obrokRepository.findById(obrokId).orElseThrow(InvalidObrokIdException::new);
|
---|
57 | Cena cena=this.findbyUniqueId(id);
|
---|
58 | cena.setObrok(obrok);
|
---|
59 | cena.setCenaIznos(cenaIznos);
|
---|
60 | cena.setCenaVaziOd(cenaVaziOd);
|
---|
61 | cena.setCenaVaziDo(cenaVaziDo);
|
---|
62 | return this.cenaRepository.save(cena);
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public Cena delete(Integer id) {
|
---|
67 | Cena cena=this.findbyUniqueId(id);
|
---|
68 | this.cenaRepository.deleteById(id);
|
---|
69 | return cena;
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public List<Cena> listCeniByObrok( Integer meniId) {
|
---|
74 | Meni meni=this.meniRepository.findById(meniId).orElseThrow(InvalidMeniIdException::new);
|
---|
75 | List<Obrok>obroci=this.obrokRepository.findAllByMeni(meni);
|
---|
76 | return this.cenaRepository.findAllByObrokIn(obroci);
|
---|
77 | }
|
---|
78 | }
|
---|