[ce6ad22] | 1 | package parkup.services;
|
---|
| 2 |
|
---|
| 3 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 4 | import org.springframework.stereotype.Service;
|
---|
| 5 | import parkup.entities.Administrator;
|
---|
| 6 | import parkup.entities.ParkingZone;
|
---|
| 7 | import parkup.repositories.ParkingZoneRepository;
|
---|
| 8 |
|
---|
| 9 | import javax.transaction.Transactional;
|
---|
| 10 | import java.util.List;
|
---|
| 11 | import java.util.Objects;
|
---|
| 12 | import java.util.Optional;
|
---|
| 13 |
|
---|
| 14 | @Service
|
---|
| 15 | public class ParkingZoneService {
|
---|
| 16 | private final ParkingZoneRepository parkingZoneRepository;
|
---|
| 17 |
|
---|
| 18 | @Autowired
|
---|
| 19 | public ParkingZoneService(ParkingZoneRepository parkingZoneRepository) {
|
---|
| 20 | this.parkingZoneRepository = parkingZoneRepository;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | public List<ParkingZone> getAllParkingZones() {
|
---|
| 24 | return parkingZoneRepository.findAll();
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | public ParkingZone findById(int parkingZoneId) {
|
---|
| 28 | Optional<ParkingZone> parkingZone = Optional.ofNullable(this.parkingZoneRepository.findByPzId(parkingZoneId));
|
---|
| 29 | return parkingZone.orElse(null);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | public void addParkingZone(ParkingZone parkingZone) { //zavisno vo koj grad ili opstina i napravi proverki pred dodavanje, implementiraj u naredna faza
|
---|
| 33 | Optional<ParkingZone> parkingZoneOpt = Optional.ofNullable(parkingZoneRepository.findByPzName(parkingZone.getPzName()));
|
---|
| 34 | if (parkingZoneOpt.isPresent()) {
|
---|
| 35 | throw new IllegalStateException("Name already taken, try adding a ParkingZone with a different name");
|
---|
| 36 | } else {
|
---|
| 37 | System.out.println(parkingZone);
|
---|
| 38 | parkingZoneRepository.save(parkingZone);
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | @Transactional
|
---|
| 43 | public void updateParkingZone(int parkingZoneId, String pzName, int price, int capacity, String workHours, String location) {
|
---|
| 44 | Optional<ParkingZone> parkingZoneOpt = Optional.ofNullable(parkingZoneRepository.findByPzId(parkingZoneId));
|
---|
| 45 | if (parkingZoneOpt.isPresent()) {
|
---|
| 46 | ParkingZone parkingZoneNov = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
| 47 | if (pzName != null && pzName.length() > 1 && !Objects.equals(parkingZoneNov.getPzName(), pzName)) {
|
---|
| 48 | Optional<ParkingZone> parkingZoneOpt1 = Optional.ofNullable(parkingZoneRepository.findByPzName(pzName));
|
---|
| 49 | if (parkingZoneOpt1.isPresent()) {
|
---|
| 50 | throw new IllegalStateException("There is already a ParkingZone with the same name");
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | parkingZoneNov.setPzName(pzName);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | if (price != 0 && !Objects.equals(parkingZoneNov.getPrice(), price)) {
|
---|
| 57 | parkingZoneNov.setPrice(price);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | if (capacity != 0 && !Objects.equals(parkingZoneNov.getCapacity(), capacity)) {
|
---|
| 61 | parkingZoneNov.setCapacity(capacity);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | if (workHours != null && workHours.length() > 0 && !Objects.equals(parkingZoneNov.getWorkHours(), workHours)) {
|
---|
| 65 | parkingZoneNov.setWorkHours(workHours);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | if (location != null && location.length() > 0 && !Objects.equals(parkingZoneNov.getLocation(), location)) {
|
---|
| 69 | parkingZoneNov.setLocation(location);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public void deleteParkingZone(int parkingZoneId) {
|
---|
| 75 | Optional<ParkingZone> parkingZoneOpt = Optional.ofNullable(parkingZoneRepository.findByPzId(parkingZoneId));
|
---|
| 76 | if (parkingZoneOpt.isPresent()) {
|
---|
| 77 | parkingZoneRepository.deleteByPzId(parkingZoneId);
|
---|
| 78 | } else {
|
---|
| 79 | throw new IllegalStateException("ParkingZone doesn't exist, therefore can't be deleted");
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|