1 | package mk.ukim.finki.eglas.services.Impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.eglas.model.Address;
|
---|
4 | import mk.ukim.finki.eglas.model.Party;
|
---|
5 | import mk.ukim.finki.eglas.repository.PartyRepository;
|
---|
6 | import mk.ukim.finki.eglas.services.AddressService;
|
---|
7 | import mk.ukim.finki.eglas.services.PartyService;
|
---|
8 | import org.springframework.stereotype.Service;
|
---|
9 |
|
---|
10 | import java.util.List;
|
---|
11 | // Jovan
|
---|
12 | @Service
|
---|
13 | public class PartyServiceImpl implements PartyService {
|
---|
14 | private final PartyRepository partyRepository;
|
---|
15 | private final AddressService addressService;
|
---|
16 |
|
---|
17 | PartyServiceImpl(PartyRepository partyRepository, AddressService addressService){
|
---|
18 | this.partyRepository = partyRepository;
|
---|
19 | this.addressService = addressService;
|
---|
20 | }
|
---|
21 |
|
---|
22 | @Override
|
---|
23 | public List<Party> findAll() {
|
---|
24 | return partyRepository.findAll();
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public Party findById(Long id) {
|
---|
29 | return partyRepository.findById(id).orElseThrow(() -> new RuntimeException("Party not found"));
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public Party update(Long id, String name, String street, Integer houseNumber, Integer entranceNumber, Integer apartmentNumber, Long municipality) {
|
---|
34 | Address address = addressService.findByStreetLikeAndHouseNumberAndEntranceNumberAndApartmentNumberAAndMunicipality(street, houseNumber, entranceNumber, apartmentNumber, municipality);
|
---|
35 | Party party = new Party();
|
---|
36 | if(id != null)
|
---|
37 | {
|
---|
38 | party = findById(id);
|
---|
39 | }
|
---|
40 | party.setName(name);
|
---|
41 | party.setAddress(address);
|
---|
42 | return partyRepository.save(party);
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public Party delete(Long id) {
|
---|
47 | Party party = findById(id);
|
---|
48 | partyRepository.delete(party);
|
---|
49 | return party;
|
---|
50 | }
|
---|
51 | }
|
---|