[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 |
|
---|
[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()) {
|
---|
[9ff45d6] | 63 | parkingSessionOpt.get().getParkingSpace().setTaken(false);
|
---|
[ce6ad22] | 64 | parkingSessionRepository.deleteByPssId(parkingSessionId);
|
---|
[97fbc67] | 65 | return parkingSessionOpt;
|
---|
[ce6ad22] | 66 | } else {
|
---|
| 67 | throw new IllegalStateException("ParkingSpace doesn't exist, therefore can't be deleted");
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[9ff45d6] | 71 | @Transactional
|
---|
| 72 | public Optional<ParkingSession> startParkingSession(String tablica, int parkingZoneId) {
|
---|
| 73 | ParkingZone parkingZone = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
[9dd526f] | 74 | Plate plate = plateRepository.findByPlate(tablica);
|
---|
| 75 | if(plate ==null) {
|
---|
| 76 | plateRepository.save(new Plate(tablica));
|
---|
[9ff45d6] | 77 | }
|
---|
[9dd526f] | 78 | plate = plateRepository.findByPlate(tablica);
|
---|
| 79 | ParkingSession sessionToAdd = new ParkingSession(plate);
|
---|
[9ff45d6] | 80 | sessionToAdd.setParkingZone(parkingZone);
|
---|
| 81 | parkingSessionRepository.save(sessionToAdd);
|
---|
| 82 | return Optional.of(sessionToAdd);
|
---|
| 83 |
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | // public void executePayment(int id){
|
---|
| 87 | // ParkingSession session = parkingSessionRepository.findByPssId(id);
|
---|
| 88 | // if(session==null){
|
---|
| 89 | // throw new IllegalStateException("No such session exists");
|
---|
| 90 | // }
|
---|
| 91 | // else if(session.getStatus()== SessionStatus.ENDED_UNPAID){
|
---|
| 92 | // throw new IllegalStateException("Cannot execute payment on an ended session");
|
---|
| 93 | // }
|
---|
| 94 | // session.setStatus(SessionStatus.ENDED_PAID);
|
---|
| 95 | // }
|
---|
| 96 |
|
---|
| 97 | public int calculatePayment(int id){
|
---|
| 98 | ParkingSession session = parkingSessionRepository.findByPssId(id);
|
---|
| 99 | if(session==null){
|
---|
| 100 | throw new IllegalStateException("No such session exists");
|
---|
| 101 | }
|
---|
[ad4da86] | 102 | // else if(session.getStatus()!= SessionStatus.ENDED_PAID){
|
---|
| 103 | // throw new IllegalStateException("Cannot calculate payment on a paid session");
|
---|
| 104 | // }
|
---|
[9ff45d6] | 105 | int price = session.getParkingZone().getPrice();
|
---|
[ad4da86] | 106 | return (int) (Math.ceil(Duration.between(session.getTimeStart(),session.getTimeEnd()).toMinutes()/60.0)*price);
|
---|
[9ff45d6] | 107 | }
|
---|
| 108 | @Transactional
|
---|
| 109 | public ParkingSession verifyParkingSession(int id,String parkingSpaceName,int parkingZoneId){
|
---|
| 110 | ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
|
---|
| 111 | ParkingZone parkingZone = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
| 112 | if(parkingSession==null){
|
---|
| 113 | throw new IllegalStateException("No such session exists");
|
---|
| 114 | }
|
---|
| 115 | else if(parkingSession.getStatus()==SessionStatus.STARTED_VERIFIED){
|
---|
| 116 | throw new IllegalStateException("The session you are trying to verify has already been verified");
|
---|
| 117 | }
|
---|
| 118 | // else if(parkingSession.getStatus()!=SessionStatus.STARTED_)
|
---|
| 119 | if(!parkingZone.getParkingSpaces().contains(parkingSpaceRepository.findByPsName(parkingSpaceName))){
|
---|
| 120 | 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");
|
---|
| 121 | }
|
---|
[ad4da86] | 122 | if(parkingSpaceRepository.findByPsName(parkingSpaceName).isTaken()){
|
---|
| 123 | throw new IllegalStateException("Ivalid parking space inserted. Type in a new parking space, this space is already in a session");
|
---|
| 124 | }
|
---|
[9ff45d6] | 125 | parkingSession.setStatus(SessionStatus.STARTED_VERIFIED);
|
---|
| 126 | ParkingSpace parkingSpace = parkingZoneRepository.findByPzId(parkingZoneId).getParkingSpaces().stream().filter(ps->ps.getPsName().equals(parkingSpaceName)).findFirst().get();
|
---|
| 127 | parkingSpace.setTaken(true);
|
---|
| 128 | parkingSession.setParkingSpace(parkingSpace);
|
---|
| 129 | return parkingSession;
|
---|
| 130 | }
|
---|
| 131 | @Transactional
|
---|
| 132 | public ParkingSession endParkingSession(int id){
|
---|
| 133 | ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
|
---|
| 134 | if(parkingSession==null){
|
---|
| 135 | throw new IllegalStateException("No such session exists");
|
---|
| 136 | }
|
---|
| 137 | else if(parkingSession.getStatus()==SessionStatus.ENDED_UNPAID){
|
---|
| 138 | throw new IllegalStateException("The session you are trying to end has already been ended");
|
---|
| 139 | }
|
---|
| 140 | parkingSession.setStatus(SessionStatus.ENDED_UNPAID);
|
---|
| 141 | parkingSession.setTimeEnd(LocalDateTime.now());
|
---|
| 142 | return parkingSession;
|
---|
| 143 | }
|
---|
| 144 | @Transactional
|
---|
| 145 | public boolean payParkingSession(int id,String date){
|
---|
| 146 | ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
|
---|
| 147 | String expMonthString=date.split("/")[0];
|
---|
| 148 | String expYearString=date.split("/")[1];
|
---|
| 149 | if(expMonthString.startsWith("0"))
|
---|
[98f448a] | 150 | expMonthString=expMonthString.substring(1);
|
---|
[9ff45d6] | 151 | if(expYearString.startsWith("0"))
|
---|
| 152 | expYearString=expYearString.substring(1);
|
---|
| 153 | int expMonth=Integer.parseInt(expMonthString);
|
---|
| 154 | int expYear=Integer.parseInt(expYearString);
|
---|
| 155 | int month = LocalDateTime.now().getMonth().getValue();
|
---|
| 156 | int year = LocalDateTime.now().getYear()%100;
|
---|
| 157 | if(year>expYear){
|
---|
| 158 | throw new IllegalStateException("Your card has expired or the date that you have entered is incorrect");
|
---|
| 159 | }
|
---|
| 160 | else if(year==expYear){
|
---|
| 161 | if(month>=expMonth)
|
---|
| 162 | throw new IllegalStateException("Your card has expired or the date that you have entered is incorrect");
|
---|
| 163 | }
|
---|
| 164 | if(parkingSession==null){
|
---|
| 165 | throw new IllegalStateException("No such session exists");
|
---|
| 166 | }
|
---|
| 167 | else if(parkingSession.getStatus()==SessionStatus.ENDED_PAID){
|
---|
| 168 | throw new IllegalStateException("The session has already been paid for");
|
---|
[ce6ad22] | 169 | }
|
---|
[9ff45d6] | 170 | parkingSession.setStatus(SessionStatus.ENDED_PAID);
|
---|
| 171 | parkingSession.getParkingSpace().setTaken(false);
|
---|
| 172 | return true;
|
---|
[ce6ad22] | 173 | }
|
---|
| 174 | }
|
---|