1 | package com.example.eatys_app.service;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.eatys_app.model.Kupuvac;
|
---|
5 | import com.example.eatys_app.model.Naracka;
|
---|
6 | import com.example.eatys_app.model.Obrok;
|
---|
7 | import com.example.eatys_app.model.SeSostoiOd;
|
---|
8 | import com.example.eatys_app.model.exceptions.*;
|
---|
9 | import com.example.eatys_app.repository.*;
|
---|
10 | import jakarta.transaction.Transactional;
|
---|
11 | import org.springframework.stereotype.Service;
|
---|
12 |
|
---|
13 | @Service
|
---|
14 | public class SeSostoiOdServiceImpl implements SeSostoiOdService {
|
---|
15 |
|
---|
16 | private final SeSostoiOdRepository seSostoiOdRepository;
|
---|
17 | private final NarackaRepository narackaRepository;
|
---|
18 | private final ObrokRepository obrokRepository;
|
---|
19 | private final KupuvacRepository kupuvacRepository;
|
---|
20 | private final CenaRepository cenaRepository;
|
---|
21 |
|
---|
22 | public SeSostoiOdServiceImpl(SeSostoiOdRepository seSostoiOdRepository, NarackaRepository narackaRepository, ObrokRepository obrokRepository, KupuvacRepository kupuvacRepository, CenaRepository cenaRepository) {
|
---|
23 | this.seSostoiOdRepository = seSostoiOdRepository;
|
---|
24 | this.narackaRepository = narackaRepository;
|
---|
25 | this.obrokRepository = obrokRepository;
|
---|
26 | this.kupuvacRepository = kupuvacRepository;
|
---|
27 | this.cenaRepository = cenaRepository;
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public SeSostoiOd create(String kupuvacName, Integer obrokId, Integer kolicina) {
|
---|
33 | Kupuvac kupuvac=this.kupuvacRepository.findByIme(kupuvacName).orElseThrow(KupuvacNotFoundException2::new);
|
---|
34 | Naracka naracka=this.narackaRepository.findByKupuvac(kupuvac).orElseThrow(NarackaNotFoundException2::new);
|
---|
35 | Obrok obrok=this.obrokRepository.findById(obrokId).orElseThrow(InvalidObrokIdException::new);
|
---|
36 | Integer cena=this.cenaRepository.findByObrok(obrok).orElseThrow(InvalidObrokIdException::new).getCenaIznos()*kolicina;
|
---|
37 | SeSostoiOd seSostoiOd = new SeSostoiOd(naracka,obrok,kolicina,cena);
|
---|
38 | return this.seSostoiOdRepository.save(seSostoiOd);
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public SeSostoiOd delete(String kupuvacName, Integer obrokId) {
|
---|
43 | Kupuvac kupuvac=this.kupuvacRepository.findByIme(kupuvacName).orElseThrow(KupuvacNotFoundException2::new);
|
---|
44 | Naracka naracka=this.narackaRepository.findByKupuvac(kupuvac).orElseThrow(NarackaNotFoundException2::new);
|
---|
45 | Obrok obrok=this.obrokRepository.findById(obrokId).orElseThrow(InvalidObrokIdException::new);
|
---|
46 | SeSostoiOd seSostoiOd=this.seSostoiOdRepository.findByNarackaAndObrok(naracka,obrok).orElseThrow(InvalidNarackaAndObrokException::new);
|
---|
47 | this.seSostoiOdRepository.delete(seSostoiOd);
|
---|
48 | return seSostoiOd;
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public void payment(String kupuvacName) {
|
---|
54 | Kupuvac kupuvac=this.kupuvacRepository.findByIme(kupuvacName).orElseThrow(KupuvacNotFoundException2::new);
|
---|
55 | Naracka naracka=this.narackaRepository.findByKupuvac(kupuvac).orElseThrow(NarackaNotFoundException2::new);
|
---|
56 | this.narackaRepository.delete(naracka);
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public Naracka getActiveShoppingCart(String username) {
|
---|
62 |
|
---|
63 | Kupuvac kupuvac = this.kupuvacRepository.findByIme(username)
|
---|
64 | .orElseThrow(() -> new KupuvacNotFoundException(username));
|
---|
65 |
|
---|
66 | return this.narackaRepository
|
---|
67 | .findByKupuvac(kupuvac)
|
---|
68 | .orElseGet(() -> {
|
---|
69 | Naracka cart = new Naracka(kupuvac);
|
---|
70 | return this.narackaRepository.save(cart);
|
---|
71 | });
|
---|
72 | }
|
---|
73 | }
|
---|