1 | package mk.ukim.finki.eglas.services.Impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.eglas.model.Election;
|
---|
4 | import mk.ukim.finki.eglas.model.Municipality;
|
---|
5 | import mk.ukim.finki.eglas.repository.ElectionRepository;
|
---|
6 | import mk.ukim.finki.eglas.repository.MunicipalityRepository;
|
---|
7 | import mk.ukim.finki.eglas.services.MunicipalityService;
|
---|
8 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 |
|
---|
11 | import java.util.List;
|
---|
12 | // Jovan
|
---|
13 | @Service
|
---|
14 | public class MunicipalityServiceImpl implements MunicipalityService {
|
---|
15 |
|
---|
16 | private final MunicipalityRepository municipalityRepository;
|
---|
17 |
|
---|
18 | MunicipalityServiceImpl(MunicipalityRepository municipalityRepository){
|
---|
19 | this.municipalityRepository = municipalityRepository;
|
---|
20 | }
|
---|
21 |
|
---|
22 | @Override
|
---|
23 | public List<Municipality> findAll() {
|
---|
24 | return municipalityRepository.findAll();
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public Municipality findById(Long id) {
|
---|
29 | return municipalityRepository.findById(id).orElseThrow(() -> new RuntimeException("Municipality not found"));
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public Municipality update(Long id, String name) {
|
---|
34 | Municipality municipality = new Municipality();
|
---|
35 | if(id != null)
|
---|
36 | {
|
---|
37 | municipality = findById(id);
|
---|
38 | }
|
---|
39 | municipality.setName(name);
|
---|
40 | return municipalityRepository.save(municipality);
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public Municipality delete(Long id) {
|
---|
45 | Municipality municipality = findById(id);
|
---|
46 | municipalityRepository.delete(municipality);
|
---|
47 | return municipality;
|
---|
48 | }
|
---|
49 | }
|
---|