[b3f2adb] | 1 | package com.example.eatys_app.service;
|
---|
| 2 |
|
---|
| 3 | import com.example.eatys_app.model.Meni;
|
---|
| 4 | import com.example.eatys_app.model.Obrok;
|
---|
| 5 | import com.example.eatys_app.model.exceptions.InvalidMeniIdException;
|
---|
| 6 | import com.example.eatys_app.model.exceptions.InvalidObrokIdException;
|
---|
| 7 | import com.example.eatys_app.repository.MeniRepository;
|
---|
| 8 | import com.example.eatys_app.repository.ObrokRepository;
|
---|
| 9 | import org.springframework.stereotype.Service;
|
---|
| 10 |
|
---|
| 11 | import java.util.List;
|
---|
| 12 |
|
---|
| 13 | @Service
|
---|
| 14 | public class ObrokServiceImpl implements ObrokService{
|
---|
| 15 |
|
---|
| 16 | private final ObrokRepository obrokRepository;
|
---|
| 17 | private final MeniRepository meniRepository;
|
---|
| 18 |
|
---|
| 19 | public ObrokServiceImpl(ObrokRepository obrokRepository, MeniRepository meniRepository) {
|
---|
| 20 | this.obrokRepository = obrokRepository;
|
---|
| 21 | this.meniRepository = meniRepository;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | @Override
|
---|
| 25 | public List<Obrok> listAll() {
|
---|
| 26 | return this.obrokRepository.findAll();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | @Override
|
---|
| 30 | public Obrok findById(Integer id) {
|
---|
| 31 | return this.obrokRepository.findById(id).orElseThrow(InvalidObrokIdException::new);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | @Override
|
---|
| 35 | public Obrok create(String ime, String opis, Integer meniId) {
|
---|
| 36 | Meni meni=this.meniRepository.findById(meniId).orElseThrow(InvalidMeniIdException::new);
|
---|
| 37 | Obrok obrok= new Obrok(ime,opis,meni);
|
---|
| 38 | return this.obrokRepository.save(obrok);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | @Override
|
---|
| 42 | public Obrok update(Integer id, String ime, String opis, Integer meniId) {
|
---|
| 43 | Meni meni=this.meniRepository.findById(meniId).orElseThrow(InvalidMeniIdException::new);
|
---|
| 44 | Obrok obrok=this.findById(id);
|
---|
| 45 | obrok.setIme(ime);
|
---|
| 46 | obrok.setOpis(opis);
|
---|
| 47 | obrok.setMeni(meni);
|
---|
| 48 | return this.obrokRepository.save(obrok);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | @Override
|
---|
| 52 | public Obrok delete(Integer id) {
|
---|
| 53 | Obrok obrok=this.findById(id);
|
---|
| 54 | this.obrokRepository.delete(obrok);
|
---|
| 55 | return obrok;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Override
|
---|
| 59 | public List<Obrok> listObrociByMeni(Integer meniId) {
|
---|
| 60 | Meni meni=this.meniRepository.findById(meniId).orElseThrow(InvalidMeniIdException::new);
|
---|
| 61 | return this.obrokRepository.findAllByMeni(meni);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|