1 | package com.example.eatys_app.service;
|
---|
2 |
|
---|
3 | import com.example.eatys_app.model.Meni;
|
---|
4 | import com.example.eatys_app.model.Restoran;
|
---|
5 | import com.example.eatys_app.model.Tip;
|
---|
6 | import com.example.eatys_app.model.exceptions.InvalidMeniIdException;
|
---|
7 | import com.example.eatys_app.model.exceptions.InvalidRestoranIdException;
|
---|
8 | import com.example.eatys_app.model.exceptions.InvalidTipIdException;
|
---|
9 | import com.example.eatys_app.repository.MeniRepository;
|
---|
10 | import com.example.eatys_app.repository.RestoranRepository;
|
---|
11 | import com.example.eatys_app.repository.TipRepository;
|
---|
12 | import org.springframework.stereotype.Service;
|
---|
13 |
|
---|
14 | import java.util.List;
|
---|
15 |
|
---|
16 | @Service
|
---|
17 | public class MeniServiceImpl implements MeniService{
|
---|
18 |
|
---|
19 | private final MeniRepository meniRepository;
|
---|
20 | private final RestoranRepository restoranRepository;
|
---|
21 | private final TipRepository tipRepository;
|
---|
22 |
|
---|
23 | public MeniServiceImpl(MeniRepository meniRepository, RestoranRepository restoranRepository, TipRepository tipRepository) {
|
---|
24 | this.meniRepository = meniRepository;
|
---|
25 | this.restoranRepository = restoranRepository;
|
---|
26 | this.tipRepository = tipRepository;
|
---|
27 | }
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public List<Meni> listAll() {
|
---|
31 | return this.meniRepository.findAll();
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public Meni findById(Integer id) {
|
---|
36 | return this.meniRepository.findById(id).orElseThrow(InvalidMeniIdException::new);
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public Meni create(Integer tipId, Integer restoranId) {
|
---|
41 | Restoran restoran =this.restoranRepository.findById(restoranId).orElseThrow(InvalidRestoranIdException::new);
|
---|
42 | Tip tip = this.tipRepository.findById(tipId).orElseThrow(InvalidTipIdException::new);
|
---|
43 | Meni meni = new Meni(restoran,tip);
|
---|
44 | return this.meniRepository.save(meni);
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public Meni update(Integer id, Integer tipId, Integer restoranId) {
|
---|
49 | Restoran restoran =this.restoranRepository.findById(restoranId).orElseThrow(InvalidRestoranIdException::new);
|
---|
50 | Tip tip = this.tipRepository.findById(tipId).orElseThrow(InvalidTipIdException::new);
|
---|
51 | Meni meni=this.findById(id);
|
---|
52 | meni.setTip(tip);
|
---|
53 | meni.setRestoran(restoran);
|
---|
54 | return this.meniRepository.save(meni);
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public Meni delete(Integer id) {
|
---|
59 | Meni meni=this.findById(id);
|
---|
60 | this.meniRepository.delete(meni);
|
---|
61 | return meni;
|
---|
62 | }
|
---|
63 | }
|
---|