source: src/main/java/com/example/eatys_app/service/CenaServiceImpl.java

Last change on this file was b3f2adb, checked in by Aleksandar Siljanoski <acewow3@…>, 14 months ago

Adding project to repo

  • Property mode set to 100644
File size: 2.8 KB
Line 
1package com.example.eatys_app.service;
2
3
4import com.example.eatys_app.model.Cena;
5import com.example.eatys_app.model.Meni;
6import com.example.eatys_app.model.Obrok;
7import com.example.eatys_app.model.exceptions.InvalidMeniIdException;
8import com.example.eatys_app.model.exceptions.InvalidObrokIdException;
9import com.example.eatys_app.model.exceptions.InvalidUniqueIdException;
10import com.example.eatys_app.repository.CenaRepository;
11import com.example.eatys_app.repository.MeniRepository;
12import com.example.eatys_app.repository.ObrokRepository;
13import org.springframework.stereotype.Service;
14
15import java.util.Date;
16import java.util.List;
17
18@Service
19public 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}
Note: See TracBrowser for help on using the repository browser.