1 | package mk.finki.ukim.mk.vehiclerent.service.impl;
|
---|
2 |
|
---|
3 | import mk.finki.ukim.mk.vehiclerent.generated_model.VozilaEntity;
|
---|
4 | import mk.finki.ukim.mk.vehiclerent.model.exceptions.InvalidVoziloIdException;
|
---|
5 | import mk.finki.ukim.mk.vehiclerent.repository.VozilaRepository;
|
---|
6 | import mk.finki.ukim.mk.vehiclerent.service.VoziloService;
|
---|
7 | import org.springframework.stereotype.Service;
|
---|
8 |
|
---|
9 | import java.math.BigInteger;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | @Service
|
---|
13 | public class VoziloServiceImpl implements VoziloService {
|
---|
14 | private final VozilaRepository vozilaRepository;
|
---|
15 |
|
---|
16 | public VoziloServiceImpl(VozilaRepository vozilaRepository) {
|
---|
17 | this.vozilaRepository = vozilaRepository;
|
---|
18 | }
|
---|
19 |
|
---|
20 | @Override
|
---|
21 | public VozilaEntity findById(int id) {
|
---|
22 | return this.vozilaRepository.findById(id).orElseThrow(InvalidVoziloIdException::new);
|
---|
23 | }
|
---|
24 |
|
---|
25 | @Override
|
---|
26 | public List<VozilaEntity> listAll() {
|
---|
27 | return this.vozilaRepository.findAll();
|
---|
28 | }
|
---|
29 |
|
---|
30 | @Override
|
---|
31 | public VozilaEntity create(Integer idkat, String model, String marka, String regtab, BigInteger cenavozi) {
|
---|
32 | VozilaEntity vozilo = new VozilaEntity(idkat,model,marka,regtab,cenavozi);
|
---|
33 | return this.vozilaRepository.save(vozilo);
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public VozilaEntity update(int id,Integer idkat, String model, String marka, String regtab, BigInteger cenavozi) {
|
---|
38 | VozilaEntity vozilo = this.vozilaRepository.findById(id).orElseThrow(InvalidVoziloIdException::new);
|
---|
39 |
|
---|
40 |
|
---|
41 | vozilo.setIdkat(idkat);
|
---|
42 | vozilo.setModel(model);
|
---|
43 | vozilo.setMarka(marka);
|
---|
44 | vozilo.setRegtab(regtab);
|
---|
45 | vozilo.setCenavozi(cenavozi);
|
---|
46 | return this.vozilaRepository.save(vozilo);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public VozilaEntity delete(int id) {
|
---|
51 | VozilaEntity vozilo = this.findById(id);
|
---|
52 | this.vozilaRepository.delete(vozilo);
|
---|
53 | return vozilo;
|
---|
54 | }
|
---|
55 | }
|
---|