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