1 | package com.example.baza.service.impl;
|
---|
2 |
|
---|
3 | import com.example.baza.model.Dobavuvac;
|
---|
4 | import com.example.baza.model.InstancaOdKniga;
|
---|
5 | import com.example.baza.model.Knigi;
|
---|
6 | import com.example.baza.model.exception.InvalidKnigaIdException;
|
---|
7 | import com.example.baza.model.exception.InvalidUniqueIdException;
|
---|
8 | import com.example.baza.repository.InstancaOdKnigaRepository;
|
---|
9 | import com.example.baza.repository.KnigiRepository;
|
---|
10 | import com.example.baza.service.InstancaOdKnigaService;
|
---|
11 | import org.springframework.stereotype.Service;
|
---|
12 |
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | @Service
|
---|
16 | public class InstancaOdKnigaServiceImpl implements InstancaOdKnigaService {
|
---|
17 | private final InstancaOdKnigaRepository instancaOdKnigaRepository;
|
---|
18 | private final KnigiRepository knigiRepository;
|
---|
19 |
|
---|
20 | public InstancaOdKnigaServiceImpl(InstancaOdKnigaRepository instancaOdKnigaRepository, KnigiRepository knigiRepository) {
|
---|
21 | this.instancaOdKnigaRepository = instancaOdKnigaRepository;
|
---|
22 | this.knigiRepository = knigiRepository;
|
---|
23 | }
|
---|
24 |
|
---|
25 | @Override
|
---|
26 | public InstancaOdKniga findByUniqueId(Integer uid) {
|
---|
27 | return this.instancaOdKnigaRepository.findById(uid).orElseThrow(InvalidUniqueIdException::new);
|
---|
28 | }
|
---|
29 |
|
---|
30 | @Override
|
---|
31 | public Knigi findById(Integer id) {
|
---|
32 | return this.knigiRepository.findById(id).orElseThrow(InvalidKnigaIdException::new);
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public List<InstancaOdKniga> listAll() {
|
---|
37 | return this.instancaOdKnigaRepository.findAll();
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public InstancaOdKniga create(Knigi idNaKniga, Boolean daliEOstetena, Boolean status, Dobavuvac idNaDobavuvac) {
|
---|
42 | return this.instancaOdKnigaRepository.save(new InstancaOdKniga(idNaKniga,daliEOstetena,status,idNaDobavuvac));
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public InstancaOdKniga update(Integer id, Knigi idNaKniga, Boolean daliEOstetena, Boolean status, Dobavuvac idNaDobavuvac) {
|
---|
47 | InstancaOdKniga i=this.findByUniqueId(id);
|
---|
48 | i.setIdNaKniga(idNaKniga);
|
---|
49 | i.setDaliEOstetena(daliEOstetena);
|
---|
50 | i.setStatus(status);
|
---|
51 | i.setIdNaDobavuvac(idNaDobavuvac);
|
---|
52 | return this.instancaOdKnigaRepository.save(i);
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public InstancaOdKniga delete(Integer id) {
|
---|
57 | InstancaOdKniga i=this.findByUniqueId(id);
|
---|
58 | this.instancaOdKnigaRepository.deleteById(id);
|
---|
59 | return i;
|
---|
60 | }
|
---|
61 | }
|
---|