[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;
|
---|
[ce6ad22] | 10 | import parkup.repositories.ParkingSessionRepository;
|
---|
[9ff45d6] | 11 | import parkup.repositories.ParkingSpaceRepository;
|
---|
| 12 | import parkup.repositories.ParkingZoneRepository;
|
---|
[9dd526f] | 13 | import parkup.repositories.PlateRepository;
|
---|
[ce6ad22] | 14 |
|
---|
[97fbc67] | 15 | import javax.transaction.Transactional;
|
---|
[9ff45d6] | 16 | import java.time.Duration;
|
---|
| 17 | import java.time.LocalDateTime;
|
---|
[ce6ad22] | 18 | import java.util.List;
|
---|
| 19 | import java.util.Optional;
|
---|
[9ff45d6] | 20 | import java.util.stream.Collectors;
|
---|
[ce6ad22] | 21 |
|
---|
| 22 | @Service
|
---|
| 23 | public class ParkingSessionService {
|
---|
| 24 | private final ParkingSessionRepository parkingSessionRepository;
|
---|
[9ff45d6] | 25 | private final ParkingZoneRepository parkingZoneRepository;
|
---|
| 26 | private final ParkingSpaceRepository parkingSpaceRepository;
|
---|
[9dd526f] | 27 | private final PlateRepository plateRepository;
|
---|
[ce6ad22] | 28 |
|
---|
| 29 | @Autowired
|
---|
[9dd526f] | 30 | public ParkingSessionService(ParkingSessionRepository parkingSessionRepository, ParkingZoneRepository parkingZoneRepository, ParkingSpaceRepository parkingSpaceRepository, PlateRepository plateRepository) {
|
---|
[ce6ad22] | 31 | this.parkingSessionRepository = parkingSessionRepository;
|
---|
[9ff45d6] | 32 | this.parkingZoneRepository = parkingZoneRepository;
|
---|
| 33 | this.parkingSpaceRepository = parkingSpaceRepository;
|
---|
[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 |
|
---|
| 46 | public ParkingSession findById(int parkingSessionId) {
|
---|
| 47 | Optional<ParkingSession> parkingSessionOpt = Optional.ofNullable(parkingSessionRepository.findByPssId(parkingSessionId));
|
---|
| 48 | return parkingSessionOpt.orElse(null);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[9ff45d6] | 51 | @Transactional
|
---|
[97fbc67] | 52 | public Optional<ParkingSession> deleteParkingSession(int parkingSessionId) {
|
---|
[ce6ad22] | 53 | Optional<ParkingSession> parkingSessionOpt = Optional.ofNullable(parkingSessionRepository.findByPssId(parkingSessionId));
|
---|
| 54 | if (parkingSessionOpt.isPresent()) {
|
---|
[9ff45d6] | 55 | parkingSessionOpt.get().getParkingSpace().setTaken(false);
|
---|
[ce6ad22] | 56 | parkingSessionRepository.deleteByPssId(parkingSessionId);
|
---|
[97fbc67] | 57 | return parkingSessionOpt;
|
---|
[ce6ad22] | 58 | } else {
|
---|
| 59 | throw new IllegalStateException("ParkingSpace doesn't exist, therefore can't be deleted");
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[9ff45d6] | 63 | @Transactional
|
---|
| 64 | public Optional<ParkingSession> startParkingSession(String tablica, int parkingZoneId) {
|
---|
| 65 | ParkingZone parkingZone = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
[9dd526f] | 66 | Plate plate = plateRepository.findByPlate(tablica);
|
---|
| 67 | if(plate ==null) {
|
---|
| 68 | plateRepository.save(new Plate(tablica));
|
---|
[9ff45d6] | 69 | }
|
---|
[9dd526f] | 70 | plate = plateRepository.findByPlate(tablica);
|
---|
| 71 | ParkingSession sessionToAdd = new ParkingSession(plate);
|
---|
[9ff45d6] | 72 | sessionToAdd.setParkingZone(parkingZone);
|
---|
| 73 | parkingSessionRepository.save(sessionToAdd);
|
---|
| 74 | return Optional.of(sessionToAdd);
|
---|
| 75 |
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | // public void executePayment(int id){
|
---|
| 79 | // ParkingSession session = parkingSessionRepository.findByPssId(id);
|
---|
| 80 | // if(session==null){
|
---|
| 81 | // throw new IllegalStateException("No such session exists");
|
---|
| 82 | // }
|
---|
| 83 | // else if(session.getStatus()== SessionStatus.ENDED_UNPAID){
|
---|
| 84 | // throw new IllegalStateException("Cannot execute payment on an ended session");
|
---|
| 85 | // }
|
---|
| 86 | // session.setStatus(SessionStatus.ENDED_PAID);
|
---|
| 87 | // }
|
---|
| 88 |
|
---|
| 89 | public int calculatePayment(int id){
|
---|
| 90 | ParkingSession session = parkingSessionRepository.findByPssId(id);
|
---|
| 91 | if(session==null){
|
---|
| 92 | throw new IllegalStateException("No such session exists");
|
---|
| 93 | }
|
---|
| 94 | else if(session.getStatus()== SessionStatus.ENDED_UNPAID){
|
---|
| 95 | throw new IllegalStateException("Cannot calculate payment on an ended session");
|
---|
| 96 | }
|
---|
| 97 | int price = session.getParkingZone().getPrice();
|
---|
| 98 | return (int) (Math.ceil(Duration.between(session.getTimeEnd(),session.getTimeStart()).toMinutes()/60.0)*price);
|
---|
| 99 | }
|
---|
| 100 | @Transactional
|
---|
| 101 | public ParkingSession verifyParkingSession(int id,String parkingSpaceName,int parkingZoneId){
|
---|
| 102 | ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
|
---|
| 103 | ParkingZone parkingZone = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
| 104 | if(parkingSession==null){
|
---|
| 105 | throw new IllegalStateException("No such session exists");
|
---|
| 106 | }
|
---|
| 107 | else if(parkingSession.getStatus()==SessionStatus.STARTED_VERIFIED){
|
---|
| 108 | throw new IllegalStateException("The session you are trying to verify has already been verified");
|
---|
| 109 | }
|
---|
| 110 | // else if(parkingSession.getStatus()!=SessionStatus.STARTED_)
|
---|
| 111 | if(!parkingZone.getParkingSpaces().contains(parkingSpaceRepository.findByPsName(parkingSpaceName))){
|
---|
| 112 | 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");
|
---|
| 113 | }
|
---|
| 114 | parkingSession.setStatus(SessionStatus.STARTED_VERIFIED);
|
---|
| 115 | ParkingSpace parkingSpace = parkingZoneRepository.findByPzId(parkingZoneId).getParkingSpaces().stream().filter(ps->ps.getPsName().equals(parkingSpaceName)).findFirst().get();
|
---|
| 116 | parkingSpace.setTaken(true);
|
---|
| 117 | parkingSession.setParkingSpace(parkingSpace);
|
---|
| 118 | return parkingSession;
|
---|
| 119 | }
|
---|
| 120 | @Transactional
|
---|
| 121 | public ParkingSession endParkingSession(int id){
|
---|
| 122 | ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
|
---|
| 123 | if(parkingSession==null){
|
---|
| 124 | throw new IllegalStateException("No such session exists");
|
---|
| 125 | }
|
---|
| 126 | else if(parkingSession.getStatus()==SessionStatus.ENDED_UNPAID){
|
---|
| 127 | throw new IllegalStateException("The session you are trying to end has already been ended");
|
---|
| 128 | }
|
---|
| 129 | parkingSession.setStatus(SessionStatus.ENDED_UNPAID);
|
---|
| 130 | parkingSession.setTimeEnd(LocalDateTime.now());
|
---|
| 131 | return parkingSession;
|
---|
| 132 | }
|
---|
| 133 | @Transactional
|
---|
| 134 | public boolean payParkingSession(int id,String date){
|
---|
| 135 | ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
|
---|
| 136 | String expMonthString=date.split("/")[0];
|
---|
| 137 | String expYearString=date.split("/")[1];
|
---|
| 138 | if(expMonthString.startsWith("0"))
|
---|
[98f448a] | 139 | expMonthString=expMonthString.substring(1);
|
---|
[9ff45d6] | 140 | if(expYearString.startsWith("0"))
|
---|
| 141 | expYearString=expYearString.substring(1);
|
---|
| 142 | int expMonth=Integer.parseInt(expMonthString);
|
---|
| 143 | int expYear=Integer.parseInt(expYearString);
|
---|
| 144 | int month = LocalDateTime.now().getMonth().getValue();
|
---|
| 145 | int year = LocalDateTime.now().getYear()%100;
|
---|
| 146 | if(year>expYear){
|
---|
| 147 | throw new IllegalStateException("Your card has expired or the date that you have entered is incorrect");
|
---|
| 148 | }
|
---|
| 149 | else if(year==expYear){
|
---|
| 150 | if(month>=expMonth)
|
---|
| 151 | throw new IllegalStateException("Your card has expired or the date that you have entered is incorrect");
|
---|
| 152 | }
|
---|
| 153 | if(parkingSession==null){
|
---|
| 154 | throw new IllegalStateException("No such session exists");
|
---|
| 155 | }
|
---|
| 156 | else if(parkingSession.getStatus()==SessionStatus.ENDED_PAID){
|
---|
| 157 | throw new IllegalStateException("The session has already been paid for");
|
---|
[ce6ad22] | 158 | }
|
---|
[9ff45d6] | 159 | parkingSession.setStatus(SessionStatus.ENDED_PAID);
|
---|
| 160 | parkingSession.getParkingSpace().setTaken(false);
|
---|
| 161 | return true;
|
---|
[ce6ad22] | 162 | }
|
---|
| 163 | }
|
---|