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