source: src/main/java/com/example/eatys_app/service/ObrokServiceImpl.java@ b3f2adb

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

Adding project to repo

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