1 | package com.example.eatys_app.service;
|
---|
2 |
|
---|
3 | import com.example.eatys_app.model.*;
|
---|
4 | import com.example.eatys_app.model.exceptions.*;
|
---|
5 | import com.example.eatys_app.repository.*;
|
---|
6 | import org.springframework.stereotype.Service;
|
---|
7 |
|
---|
8 | import java.util.Set;
|
---|
9 |
|
---|
10 | @Service
|
---|
11 | public class NarackaServiceImpl implements NarackaService{
|
---|
12 |
|
---|
13 | private final KupuvacRepository kupuvacRepository;
|
---|
14 | private final ObrokRepository obrokRepository;
|
---|
15 | private final NarackaRepository narackaRepository;
|
---|
16 | private final CenaRepository cenaRepository;
|
---|
17 | private final SeSostoiOdRepository seSostoiOdRepository;
|
---|
18 | private final KorisnikRepository korisnikRepository;
|
---|
19 |
|
---|
20 | public NarackaServiceImpl(KupuvacRepository kupuvacRepository, ObrokRepository obrokRepository, NarackaRepository narackaRepository, CenaRepository cenaRepository, SeSostoiOdRepository seSostoiOdRepository, KorisnikRepository korisnikRepository) {
|
---|
21 | this.kupuvacRepository = kupuvacRepository;
|
---|
22 | this.obrokRepository = obrokRepository;
|
---|
23 | this.narackaRepository = narackaRepository;
|
---|
24 | this.cenaRepository = cenaRepository;
|
---|
25 | this.seSostoiOdRepository = seSostoiOdRepository;
|
---|
26 | this.korisnikRepository = korisnikRepository;
|
---|
27 | }
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public Set<SeSostoiOd> listAllObrociInShoppingCart(Integer cartId) {
|
---|
31 | if(!this.narackaRepository.findById(cartId).isPresent())
|
---|
32 | throw new NarackaNotFoundException(cartId);
|
---|
33 | Set<SeSostoiOd> se_sostoi_od=this.narackaRepository.findById(cartId).get().getNarackaSeSostoiOd();
|
---|
34 | // se_sostoi_od.forEach(SeSostoiOd::getCena);
|
---|
35 | return this.narackaRepository.findById(cartId).get().getNarackaSeSostoiOd();
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public int Total(Integer cartId) {
|
---|
40 | if(!this.narackaRepository.findById(cartId).isPresent())
|
---|
41 | throw new NarackaNotFoundException(cartId);
|
---|
42 | Set<SeSostoiOd> se_sostoi_od=this.narackaRepository.findById(cartId).get().getNarackaSeSostoiOd();
|
---|
43 | // se_sostoi_od.forEach(SeSostoiOd::getCena);
|
---|
44 | int total_cena=0;
|
---|
45 | for (SeSostoiOd seSostoiOd : se_sostoi_od)
|
---|
46 | {
|
---|
47 | total_cena=total_cena+seSostoiOd.getCena();
|
---|
48 | }
|
---|
49 | return total_cena;
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public Naracka getActiveShoppingCart(String username) {
|
---|
54 |
|
---|
55 | Kupuvac kupuvac = this.kupuvacRepository.findByIme(username)
|
---|
56 | .orElseThrow(() -> new KupuvacNotFoundException(username));
|
---|
57 |
|
---|
58 | return this.narackaRepository
|
---|
59 | .findByKupuvac(kupuvac)
|
---|
60 | .orElseGet(() -> {
|
---|
61 | Naracka cart = new Naracka(kupuvac);
|
---|
62 | return this.narackaRepository.save(cart);
|
---|
63 | });
|
---|
64 |
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public SeSostoiOd addObrokToShoppingCart(String username, Integer obrokId) {
|
---|
69 |
|
---|
70 | Naracka shoppingCart = this.getActiveShoppingCart(username);
|
---|
71 | Obrok obrok = this.obrokRepository.findById(obrokId).orElseThrow(ObrokNotFound::new);
|
---|
72 | Integer kolicina=1;
|
---|
73 | Integer cena=this.cenaRepository.findByObrok(obrok).orElseThrow(InvalidObrokIdException::new).getCenaIznos();
|
---|
74 | SeSostoiOd seSostoiOd = new SeSostoiOd(shoppingCart,obrok,kolicina,cena);
|
---|
75 |
|
---|
76 | // shoppingCart.getNarackaSeSostoiOd().add(seSostoiOd);
|
---|
77 | return this.seSostoiOdRepository.save(seSostoiOd);
|
---|
78 |
|
---|
79 | }
|
---|
80 |
|
---|
81 | }
|
---|