[ce6ad22] | 1 | package parkup.services;
|
---|
| 2 |
|
---|
| 3 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 4 | import org.springframework.stereotype.Service;
|
---|
[9dd526f] | 5 | import parkup.data.Plate;
|
---|
[9ff45d6] | 6 | import parkup.data.enumarations.SessionStatus;
|
---|
[ce6ad22] | 7 | import parkup.entities.ParkingSession;
|
---|
[9ff45d6] | 8 | import parkup.entities.ParkingSpace;
|
---|
| 9 | import parkup.entities.ParkingZone;
|
---|
[52df867] | 10 | import parkup.entities.RegisteredUser;
|
---|
| 11 | import parkup.repositories.*;
|
---|
[ce6ad22] | 12 |
|
---|
[97fbc67] | 13 | import javax.transaction.Transactional;
|
---|
[9ff45d6] | 14 | import java.time.Duration;
|
---|
| 15 | import java.time.LocalDateTime;
|
---|
[ce6ad22] | 16 | import java.util.List;
|
---|
| 17 | import java.util.Optional;
|
---|
[9ff45d6] | 18 | import java.util.stream.Collectors;
|
---|
[ce6ad22] | 19 |
|
---|
| 20 | @Service
|
---|
| 21 | public class ParkingSessionService {
|
---|
| 22 | private final ParkingSessionRepository parkingSessionRepository;
|
---|
[9ff45d6] | 23 | private final ParkingZoneRepository parkingZoneRepository;
|
---|
| 24 | private final ParkingSpaceRepository parkingSpaceRepository;
|
---|
[52df867] | 25 | private final RegisteredUserRepository registeredUserRepository;
|
---|
[9dd526f] | 26 | private final PlateRepository plateRepository;
|
---|
[ce6ad22] | 27 |
|
---|
| 28 | @Autowired
|
---|
[52df867] | 29 | public ParkingSessionService(ParkingSessionRepository parkingSessionRepository, ParkingZoneRepository parkingZoneRepository, ParkingSpaceRepository parkingSpaceRepository, RegisteredUserRepository registeredUserRepository, PlateRepository plateRepository) {
|
---|
[ce6ad22] | 30 | this.parkingSessionRepository = parkingSessionRepository;
|
---|
[9ff45d6] | 31 | this.parkingZoneRepository = parkingZoneRepository;
|
---|
| 32 | this.parkingSpaceRepository = parkingSpaceRepository;
|
---|
[52df867] | 33 | this.registeredUserRepository = registeredUserRepository;
|
---|
[9dd526f] | 34 | this.plateRepository = plateRepository;
|
---|
[ce6ad22] | 35 | }
|
---|
| 36 |
|
---|
[9ff45d6] | 37 | //TODO add getMethod for paid sessions or all sessions
|
---|
| 38 |
|
---|
[ce6ad22] | 39 | public List<ParkingSession> getAllParkingSessions() {
|
---|
[9ff45d6] | 40 | //Get all sessions that arent paid
|
---|
| 41 | return parkingSessionRepository.findAll().stream()
|
---|
| 42 | .filter(s->!s.getStatus().equals(SessionStatus.ENDED_PAID))
|
---|
| 43 | .collect(Collectors.toList());
|
---|
[ce6ad22] | 44 | }
|
---|
| 45 |
|
---|
[ad4da86] | 46 | public List<ParkingSession> getAllParkingSessionsFromZone(Integer pzId) {
|
---|
| 47 | //Get all sessions that arent paid
|
---|
| 48 |
|
---|
| 49 | return parkingSessionRepository.findAll().stream()
|
---|
| 50 | .filter(s->!s.getStatus().equals(SessionStatus.ENDED_PAID) && s.getParkingZone().getId()==pzId)
|
---|
| 51 | .collect(Collectors.toList());
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[ce6ad22] | 54 | public ParkingSession findById(int parkingSessionId) {
|
---|
| 55 | Optional<ParkingSession> parkingSessionOpt = Optional.ofNullable(parkingSessionRepository.findByPssId(parkingSessionId));
|
---|
| 56 | return parkingSessionOpt.orElse(null);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[9ff45d6] | 59 | @Transactional
|
---|
[97fbc67] | 60 | public Optional<ParkingSession> deleteParkingSession(int parkingSessionId) {
|
---|
[ce6ad22] | 61 | Optional<ParkingSession> parkingSessionOpt = Optional.ofNullable(parkingSessionRepository.findByPssId(parkingSessionId));
|
---|
| 62 | if (parkingSessionOpt.isPresent()) {
|
---|
[d770228] | 63 | if(parkingSessionOpt.get().getStatus()!=SessionStatus.STARTED_UNVERIFIED)
|
---|
[9ff45d6] | 64 | parkingSessionOpt.get().getParkingSpace().setTaken(false);
|
---|
[d770228] | 65 | parkingSessionOpt.get().setPlate(null);
|
---|
[ce6ad22] | 66 | parkingSessionRepository.deleteByPssId(parkingSessionId);
|
---|
[97fbc67] | 67 | return parkingSessionOpt;
|
---|
[ce6ad22] | 68 | } else {
|
---|
| 69 | throw new IllegalStateException("ParkingSpace doesn't exist, therefore can't be deleted");
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[9ff45d6] | 73 | @Transactional
|
---|
[0e407de] | 74 | public Optional<ParkingSession> startParkingSession(String tablica, String parkingZoneName) {
|
---|
| 75 | ParkingZone parkingZone = parkingZoneRepository.findByPzName(parkingZoneName);
|
---|
[9dd526f] | 76 | Plate plate = plateRepository.findByPlate(tablica);
|
---|
| 77 | if(plate ==null) {
|
---|
| 78 | plateRepository.save(new Plate(tablica));
|
---|
[9ff45d6] | 79 | }
|
---|
[9dd526f] | 80 | plate = plateRepository.findByPlate(tablica);
|
---|
| 81 | ParkingSession sessionToAdd = new ParkingSession(plate);
|
---|
[9ff45d6] | 82 | sessionToAdd.setParkingZone(parkingZone);
|
---|
| 83 | parkingSessionRepository.save(sessionToAdd);
|
---|
| 84 | return Optional.of(sessionToAdd);
|
---|
| 85 |
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | // public void executePayment(int id){
|
---|
| 89 | // ParkingSession session = parkingSessionRepository.findByPssId(id);
|
---|
| 90 | // if(session==null){
|
---|
| 91 | // throw new IllegalStateException("No such session exists");
|
---|
| 92 | // }
|
---|
| 93 | // else if(session.getStatus()== SessionStatus.ENDED_UNPAID){
|
---|
| 94 | // throw new IllegalStateException("Cannot execute payment on an ended session");
|
---|
| 95 | // }
|
---|
| 96 | // session.setStatus(SessionStatus.ENDED_PAID);
|
---|
| 97 | // }
|
---|
| 98 |
|
---|
| 99 | public int calculatePayment(int id){
|
---|
| 100 | ParkingSession session = parkingSessionRepository.findByPssId(id);
|
---|
| 101 | if(session==null){
|
---|
| 102 | throw new IllegalStateException("No such session exists");
|
---|
| 103 | }
|
---|
[ad4da86] | 104 | // else if(session.getStatus()!= SessionStatus.ENDED_PAID){
|
---|
| 105 | // throw new IllegalStateException("Cannot calculate payment on a paid session");
|
---|
| 106 | // }
|
---|
[9ff45d6] | 107 | int price = session.getParkingZone().getPrice();
|
---|
[ad4da86] | 108 | return (int) (Math.ceil(Duration.between(session.getTimeStart(),session.getTimeEnd()).toMinutes()/60.0)*price);
|
---|
[9ff45d6] | 109 | }
|
---|
| 110 | @Transactional
|
---|
[ccb2d72] | 111 | public ParkingSession verifyParkingSession(int id,String parkingSpaceName){
|
---|
[9ff45d6] | 112 | ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
|
---|
| 113 | if(parkingSession==null){
|
---|
| 114 | throw new IllegalStateException("No such session exists");
|
---|
| 115 | }
|
---|
| 116 | else if(parkingSession.getStatus()==SessionStatus.STARTED_VERIFIED){
|
---|
| 117 | throw new IllegalStateException("The session you are trying to verify has already been verified");
|
---|
| 118 | }
|
---|
[ccb2d72] | 119 | ParkingZone parkingZone = parkingSession.getParkingZone();
|
---|
[9ff45d6] | 120 | // else if(parkingSession.getStatus()!=SessionStatus.STARTED_)
|
---|
| 121 | if(!parkingZone.getParkingSpaces().contains(parkingSpaceRepository.findByPsName(parkingSpaceName))){
|
---|
| 122 | throw new IllegalStateException("Ivalid parking space inserted. Type in a new parking space or let the admin know that they should add the parking space to this zone");
|
---|
| 123 | }
|
---|
[ad4da86] | 124 | if(parkingSpaceRepository.findByPsName(parkingSpaceName).isTaken()){
|
---|
| 125 | throw new IllegalStateException("Ivalid parking space inserted. Type in a new parking space, this space is already in a session");
|
---|
| 126 | }
|
---|
[9ff45d6] | 127 | parkingSession.setStatus(SessionStatus.STARTED_VERIFIED);
|
---|
[ccb2d72] | 128 | ParkingSpace parkingSpace = parkingZoneRepository.findByPzId(parkingZone.getId()).getParkingSpaces().stream().filter(ps->ps.getPsName().equals(parkingSpaceName)).findFirst().get();
|
---|
[9ff45d6] | 129 | parkingSpace.setTaken(true);
|
---|
| 130 | parkingSession.setParkingSpace(parkingSpace);
|
---|
| 131 | return parkingSession;
|
---|
| 132 | }
|
---|
| 133 | @Transactional
|
---|
| 134 | public ParkingSession endParkingSession(int id){
|
---|
| 135 | ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
|
---|
| 136 | if(parkingSession==null){
|
---|
| 137 | throw new IllegalStateException("No such session exists");
|
---|
| 138 | }
|
---|
| 139 | else if(parkingSession.getStatus()==SessionStatus.ENDED_UNPAID){
|
---|
| 140 | throw new IllegalStateException("The session you are trying to end has already been ended");
|
---|
| 141 | }
|
---|
| 142 | parkingSession.setStatus(SessionStatus.ENDED_UNPAID);
|
---|
| 143 | parkingSession.setTimeEnd(LocalDateTime.now());
|
---|
| 144 | return parkingSession;
|
---|
| 145 | }
|
---|
| 146 | @Transactional
|
---|
| 147 | public boolean payParkingSession(int id,String date){
|
---|
| 148 | ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
|
---|
| 149 | String expMonthString=date.split("/")[0];
|
---|
| 150 | String expYearString=date.split("/")[1];
|
---|
| 151 | if(expMonthString.startsWith("0"))
|
---|
[98f448a] | 152 | expMonthString=expMonthString.substring(1);
|
---|
[9ff45d6] | 153 | if(expYearString.startsWith("0"))
|
---|
| 154 | expYearString=expYearString.substring(1);
|
---|
| 155 | int expMonth=Integer.parseInt(expMonthString);
|
---|
| 156 | int expYear=Integer.parseInt(expYearString);
|
---|
| 157 | int month = LocalDateTime.now().getMonth().getValue();
|
---|
| 158 | int year = LocalDateTime.now().getYear()%100;
|
---|
| 159 | if(year>expYear){
|
---|
| 160 | throw new IllegalStateException("Your card has expired or the date that you have entered is incorrect");
|
---|
| 161 | }
|
---|
| 162 | else if(year==expYear){
|
---|
| 163 | if(month>=expMonth)
|
---|
| 164 | throw new IllegalStateException("Your card has expired or the date that you have entered is incorrect");
|
---|
| 165 | }
|
---|
| 166 | if(parkingSession==null){
|
---|
| 167 | throw new IllegalStateException("No such session exists");
|
---|
| 168 | }
|
---|
| 169 | else if(parkingSession.getStatus()==SessionStatus.ENDED_PAID){
|
---|
| 170 | throw new IllegalStateException("The session has already been paid for");
|
---|
[ce6ad22] | 171 | }
|
---|
[9ff45d6] | 172 | parkingSession.setStatus(SessionStatus.ENDED_PAID);
|
---|
| 173 | parkingSession.getParkingSpace().setTaken(false);
|
---|
| 174 | return true;
|
---|
[ce6ad22] | 175 | }
|
---|
[52df867] | 176 |
|
---|
| 177 | public SessionStatus getStatusOfParkirac(int regParkId){
|
---|
| 178 | RegisteredUser registeredUser = registeredUserRepository.findByRegParkId(regParkId);
|
---|
| 179 | List<String> plates =parkingSessionRepository.findAll().stream().filter(ps->registeredUser.getPlates().contains(ps.getPlate())).map(ps->ps.getPlate().getPlate()).collect(Collectors.toList());
|
---|
| 180 | if(plates.size()>0)
|
---|
| 181 | return parkingSessionRepository.findAll().stream().filter(ps->ps.getPlate().getPlate().equals(plates.get(0))).collect(Collectors.toList()).get(0).getStatus();
|
---|
| 182 | return null;
|
---|
| 183 | }
|
---|
[ce6ad22] | 184 | }
|
---|