source: src/main/java/com/example/eatys_app/service/MeniServiceImpl.java

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

Adding project to repo

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[b3f2adb]1package com.example.eatys_app.service;
2
3import com.example.eatys_app.model.Meni;
4import com.example.eatys_app.model.Restoran;
5import com.example.eatys_app.model.Tip;
6import com.example.eatys_app.model.exceptions.InvalidMeniIdException;
7import com.example.eatys_app.model.exceptions.InvalidRestoranIdException;
8import com.example.eatys_app.model.exceptions.InvalidTipIdException;
9import com.example.eatys_app.repository.MeniRepository;
10import com.example.eatys_app.repository.RestoranRepository;
11import com.example.eatys_app.repository.TipRepository;
12import org.springframework.stereotype.Service;
13
14import java.util.List;
15
16@Service
17public 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}
Note: See TracBrowser for help on using the repository browser.