[ce6ad22] | 1 | package parkup.services;
|
---|
| 2 |
|
---|
| 3 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 4 | import org.springframework.stereotype.Service;
|
---|
[97fbc67] | 5 | import parkup.data.ParkingZoneLocation;
|
---|
[9ff45d6] | 6 | import parkup.entities.ParkingSession;
|
---|
[97fbc67] | 7 | import parkup.entities.ParkingSpace;
|
---|
[ce6ad22] | 8 | import parkup.entities.ParkingZone;
|
---|
[97fbc67] | 9 | import parkup.entities.Vraboten;
|
---|
[9ff45d6] | 10 | import parkup.repositories.ParkingSessionRepository;
|
---|
| 11 | import parkup.repositories.ParkingSpaceRepository;
|
---|
[ce6ad22] | 12 | import parkup.repositories.ParkingZoneRepository;
|
---|
[9ff45d6] | 13 | import parkup.repositories.VrabotenRepository;
|
---|
[ce6ad22] | 14 |
|
---|
| 15 | import javax.transaction.Transactional;
|
---|
[97fbc67] | 16 | import java.util.ArrayList;
|
---|
[ce6ad22] | 17 | import java.util.List;
|
---|
| 18 | import java.util.Objects;
|
---|
| 19 | import java.util.Optional;
|
---|
| 20 |
|
---|
| 21 | @Service
|
---|
| 22 | public class ParkingZoneService {
|
---|
| 23 | private final ParkingZoneRepository parkingZoneRepository;
|
---|
[9ff45d6] | 24 | private final ParkingSpaceRepository parkingSpaceRepository;
|
---|
| 25 | private final VrabotenRepository vrabotenRepository;
|
---|
| 26 | private final ParkingSessionRepository parkingSessionRepository;
|
---|
[ce6ad22] | 27 |
|
---|
| 28 | @Autowired
|
---|
[9ff45d6] | 29 | public ParkingZoneService(ParkingZoneRepository parkingZoneRepository, ParkingSpaceRepository parkingSpaceRepository, VrabotenRepository vrabotenRepository, ParkingSessionRepository parkingSessionRepository) {
|
---|
[ce6ad22] | 30 | this.parkingZoneRepository = parkingZoneRepository;
|
---|
[9ff45d6] | 31 | this.parkingSpaceRepository = parkingSpaceRepository;
|
---|
| 32 | this.parkingSessionRepository = parkingSessionRepository;
|
---|
| 33 | this.vrabotenRepository = vrabotenRepository;
|
---|
[ce6ad22] | 34 | }
|
---|
| 35 |
|
---|
| 36 | public List<ParkingZone> getAllParkingZones() {
|
---|
| 37 | return parkingZoneRepository.findAll();
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | public ParkingZone findById(int parkingZoneId) {
|
---|
| 41 | Optional<ParkingZone> parkingZone = Optional.ofNullable(this.parkingZoneRepository.findByPzId(parkingZoneId));
|
---|
| 42 | return parkingZone.orElse(null);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[97fbc67] | 45 | public List<String> getAllParkingZoneNames() {
|
---|
| 46 | List<ParkingZone> parkingZones = this.parkingZoneRepository.findAll();
|
---|
| 47 | List<String> parkingZonesNames = new ArrayList<>();
|
---|
[9ff45d6] | 48 | for (ParkingZone pz : parkingZones) {
|
---|
[97fbc67] | 49 | parkingZonesNames.add(pz.getPzName());
|
---|
| 50 | }
|
---|
| 51 | return parkingZonesNames;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public Optional<ParkingZone> addParkingZone(ParkingZone parkingZone) { //zavisno vo koj grad ili opstina i napravi proverki pred dodavanje, implementiraj u naredna faza
|
---|
[ce6ad22] | 55 | Optional<ParkingZone> parkingZoneOpt = Optional.ofNullable(parkingZoneRepository.findByPzName(parkingZone.getPzName()));
|
---|
| 56 | if (parkingZoneOpt.isPresent()) {
|
---|
| 57 | throw new IllegalStateException("Name already taken, try adding a ParkingZone with a different name");
|
---|
| 58 | } else {
|
---|
| 59 | System.out.println(parkingZone);
|
---|
| 60 | parkingZoneRepository.save(parkingZone);
|
---|
| 61 | }
|
---|
[97fbc67] | 62 | return parkingZoneOpt;
|
---|
[ce6ad22] | 63 | }
|
---|
| 64 |
|
---|
[97fbc67] | 65 | public ParkingZone addParkingZoneNameOnly(String name) {
|
---|
| 66 | Optional<ParkingZone> parkingZoneOpt = Optional.ofNullable(parkingZoneRepository.findByPzName(name));
|
---|
| 67 | if (parkingZoneOpt.isPresent()) {
|
---|
| 68 | throw new IllegalStateException("Name already taken, try adding a ParkingZone with a different name");
|
---|
| 69 | } else {
|
---|
| 70 | ParkingZone parkingZone = new ParkingZone(name.trim());
|
---|
| 71 | parkingZoneRepository.save(parkingZone);
|
---|
| 72 | return parkingZone;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | //TODO proveri dali ima odgovorni i dokolku ima dali postojat vo bazata
|
---|
[ce6ad22] | 77 | @Transactional
|
---|
[9ff45d6] | 78 | public ParkingZone updateParkingZone(int parkingZoneId, String pzName, int price,
|
---|
[97fbc67] | 79 | String location, int from, int to, String color,
|
---|
[9ff45d6] | 80 | List<ParkingSpace> parkingSpaces, ParkingZoneLocation parkingZoneLocation, List<Integer> odgovorniLica) {
|
---|
| 81 |
|
---|
| 82 | //TODO prati niza od objekti ParkingSpaces i prati objekt ParkingZoneLocation
|
---|
[97fbc67] | 83 |
|
---|
[ce6ad22] | 84 | ParkingZone parkingZoneNov = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
[9ff45d6] | 85 | if (parkingZoneNov!=null) {
|
---|
| 86 | if (pzName != null && pzName.length() > 1) { ;
|
---|
| 87 | if (parkingZoneNov.getPzName().equals(pzName)&&parkingZoneNov.getId()!=parkingZoneId) {
|
---|
| 88 | throw new IllegalStateException("There is already a ParkingZone with the same name");
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | parkingZoneNov.setPzName(pzName);
|
---|
[ce6ad22] | 92 | }
|
---|
| 93 |
|
---|
[9ff45d6] | 94 | if (price != 0 && !Objects.equals(parkingZoneNov.getPrice(), price)) {
|
---|
| 95 | parkingZoneNov.setPrice(price);
|
---|
| 96 | }
|
---|
[ce6ad22] | 97 |
|
---|
[9ff45d6] | 98 | if (location != null && location.length() > 0 && !Objects.equals(parkingZoneNov.getAddress(), location)) {
|
---|
| 99 | parkingZoneNov.setAddress(location);
|
---|
| 100 | }
|
---|
[ce6ad22] | 101 |
|
---|
[9ff45d6] | 102 | if (from != 0 && !Objects.equals(parkingZoneNov.getFrom(), from)) {
|
---|
| 103 | parkingZoneNov.setFrom(from);
|
---|
| 104 | }
|
---|
[ce6ad22] | 105 |
|
---|
[9ff45d6] | 106 | if (to != 0 && !Objects.equals(parkingZoneNov.getTo(), to)) {
|
---|
| 107 | parkingZoneNov.setTo(to);
|
---|
| 108 | }
|
---|
[97fbc67] | 109 |
|
---|
[9ff45d6] | 110 | if (color != null && color.length() > 0 && !Objects.equals(parkingZoneNov.getColor(), color)) {
|
---|
| 111 | parkingZoneNov.setColor(color);
|
---|
| 112 | }
|
---|
[97fbc67] | 113 |
|
---|
[9ff45d6] | 114 | if (parkingZoneLocation != null) {
|
---|
| 115 | parkingZoneNov.setParkingZoneLocation(parkingZoneLocation);
|
---|
| 116 | }
|
---|
[97fbc67] | 117 |
|
---|
[9ff45d6] | 118 | if (!parkingSpaces.isEmpty()) {
|
---|
| 119 | ParkingZone parkingZone = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
| 120 | // for(ParkingSpace space :parkingZone.getParkingSpaces()){
|
---|
| 121 | // if(!parkingSpaces.contains(space)){
|
---|
| 122 | // for(ParkingSession session: parkingSessionRepository.findByParkingZonePzName(pzName)){
|
---|
| 123 | // if(session.getParkingSpace().getPsId()==space.getPsId())
|
---|
| 124 | // throw new IllegalStateException("Vlegov vo iffot");
|
---|
| 125 | // }
|
---|
| 126 | // }
|
---|
| 127 | // }
|
---|
| 128 | //TODO: treba da se opravi
|
---|
| 129 | parkingZone.setParkingSpaces(null);
|
---|
| 130 | List<ParkingSpace> spacesToAdd = new ArrayList<>(parkingSpaces);
|
---|
| 131 | parkingZone.setParkingSpaces(spacesToAdd);
|
---|
| 132 | }
|
---|
| 133 | if (!odgovorniLica.isEmpty()) {
|
---|
| 134 | ParkingZone parkingZone = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
| 135 | for (Integer vrabotenId : odgovorniLica) {
|
---|
| 136 | Vraboten vrabotenToAdd = vrabotenRepository.findByVrabotenId(vrabotenId);
|
---|
| 137 | if(vrabotenToAdd!=null)
|
---|
| 138 | vrabotenToAdd.getParkingZones().add(parkingZone);
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
[97fbc67] | 141 |
|
---|
| 142 |
|
---|
[9ff45d6] | 143 | return parkingZoneNov;
|
---|
| 144 | } else {
|
---|
| 145 | throw new IllegalStateException("There ParkingZone does not exist");
|
---|
[97fbc67] | 146 | }
|
---|
[9ff45d6] | 147 | }
|
---|
[97fbc67] | 148 |
|
---|
| 149 |
|
---|
[9ff45d6] | 150 | @Transactional
|
---|
| 151 | public Optional<ParkingZone> deleteParkingZone ( int parkingZoneId){
|
---|
| 152 | Optional<ParkingZone> parkingZoneOpt = Optional.ofNullable(parkingZoneRepository.findByPzId(parkingZoneId));
|
---|
| 153 | if (parkingZoneOpt.isPresent()) {
|
---|
| 154 | parkingZoneRepository.deleteByPzId(parkingZoneId);
|
---|
| 155 | } else {
|
---|
| 156 | throw new IllegalStateException("ParkingZone doesn't exist, therefore can't be deleted");
|
---|
| 157 | }
|
---|
| 158 | return parkingZoneOpt;
|
---|
[ce6ad22] | 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|