1 | package com.example.eatys_app.service;
|
---|
2 |
|
---|
3 | import com.example.eatys_app.model.Menadzer;
|
---|
4 | import com.example.eatys_app.model.Restoran;
|
---|
5 | import com.example.eatys_app.model.exceptions.InvalidMenadzerIdException;
|
---|
6 | import com.example.eatys_app.model.exceptions.InvalidRestoranIdException;
|
---|
7 | import com.example.eatys_app.repository.MenadzerRepository;
|
---|
8 | import com.example.eatys_app.repository.RestoranRepository;
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 |
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | @Service
|
---|
14 | public class RestoranServiceImpl implements RestoranService{
|
---|
15 |
|
---|
16 | private final RestoranRepository restoranRepository;
|
---|
17 | private final MenadzerRepository menadzerRepository;
|
---|
18 |
|
---|
19 | public RestoranServiceImpl(RestoranRepository restoranRepository, MenadzerRepository menadzerRepository) {
|
---|
20 | this.restoranRepository = restoranRepository;
|
---|
21 | this.menadzerRepository = menadzerRepository;
|
---|
22 | }
|
---|
23 |
|
---|
24 |
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | public List<Restoran> listAll() {
|
---|
28 | return this.restoranRepository.findAll();
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public Restoran findById(Integer id) {
|
---|
33 | return this.restoranRepository.findById(id).orElseThrow(InvalidRestoranIdException::new);
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public Restoran create(String ime, Integer rejting, String adresa, Integer menadzerId) {
|
---|
38 | Menadzer menadzer = this.menadzerRepository.findById(menadzerId).orElseThrow(InvalidMenadzerIdException::new);
|
---|
39 | Restoran restoran= new Restoran(ime,rejting,adresa,menadzer);
|
---|
40 | return this.restoranRepository.save(restoran);
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public Restoran update(Integer id, String ime, Integer rejting, String adresa, Integer menadzerId) {
|
---|
45 | Menadzer menadzer = this.menadzerRepository.findById(menadzerId).orElseThrow(InvalidMenadzerIdException::new);
|
---|
46 | Restoran restoran=this.findById(id);
|
---|
47 | restoran.setIme(ime);
|
---|
48 | restoran.setRejting(rejting);
|
---|
49 | restoran.setAdresa(adresa);
|
---|
50 | restoran.setMenadzer(menadzer);
|
---|
51 | return this.restoranRepository.save(restoran);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public Restoran delete(Integer id) {
|
---|
56 | Restoran restoran=this.findById(id);
|
---|
57 | this.restoranRepository.delete(restoran);
|
---|
58 | return restoran;
|
---|
59 | }
|
---|
60 | }
|
---|