[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()
|
---|
[0707e4e] | 51 | .filter(s->!s.getStatus().equals(SessionStatus.ENDED_PAID) && s.getParkingZone()!=null && s.getParkingZone().getId()==pzId)
|
---|
[ad4da86] | 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 | ParkingSession sessionToAdd = new ParkingSession(plate);
|
---|
[9ff45d6] | 80 | sessionToAdd.setParkingZone(parkingZone);
|
---|
| 81 | parkingSessionRepository.save(sessionToAdd);
|
---|
[3f0bf09] | 82 | String role =token.getAuthorities().stream().findFirst().get().getAuthority();
|
---|
| 83 | String email =token.getName();
|
---|
| 84 | if(role.equals("ROLE_REG_USER")){
|
---|
| 85 | RegisteredUser user = registeredUserRepository.findRegisteredUserByEmail(email).get();
|
---|
| 86 | user.setSession(sessionToAdd);
|
---|
| 87 | }
|
---|
| 88 | else{
|
---|
| 89 | Guest user = guestRepository.findGuestByEmail(email).get();
|
---|
| 90 | user.setSession(sessionToAdd);
|
---|
| 91 | }
|
---|
[9ff45d6] | 92 | return Optional.of(sessionToAdd);
|
---|
| 93 |
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | // public void executePayment(int id){
|
---|
| 97 | // ParkingSession session = parkingSessionRepository.findByPssId(id);
|
---|
| 98 | // if(session==null){
|
---|
| 99 | // throw new IllegalStateException("No such session exists");
|
---|
| 100 | // }
|
---|
| 101 | // else if(session.getStatus()== SessionStatus.ENDED_UNPAID){
|
---|
| 102 | // throw new IllegalStateException("Cannot execute payment on an ended session");
|
---|
| 103 | // }
|
---|
| 104 | // session.setStatus(SessionStatus.ENDED_PAID);
|
---|
| 105 | // }
|
---|
| 106 |
|
---|
| 107 | public int calculatePayment(int id){
|
---|
| 108 | ParkingSession session = parkingSessionRepository.findByPssId(id);
|
---|
| 109 | if(session==null){
|
---|
| 110 | throw new IllegalStateException("No such session exists");
|
---|
| 111 | }
|
---|
[0707e4e] | 112 | else if(session.getStatus()!= SessionStatus.ENDED_PAID){
|
---|
| 113 | throw new IllegalStateException("Cannot calculate payment on a session that is not ended");
|
---|
| 114 | }
|
---|
[9ff45d6] | 115 | int price = session.getParkingZone().getPrice();
|
---|
[ad4da86] | 116 | return (int) (Math.ceil(Duration.between(session.getTimeStart(),session.getTimeEnd()).toMinutes()/60.0)*price);
|
---|
[9ff45d6] | 117 | }
|
---|
| 118 | @Transactional
|
---|
[ccb2d72] | 119 | public ParkingSession verifyParkingSession(int id,String parkingSpaceName){
|
---|
[9ff45d6] | 120 | ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
|
---|
| 121 | if(parkingSession==null){
|
---|
| 122 | throw new IllegalStateException("No such session exists");
|
---|
| 123 | }
|
---|
| 124 | else if(parkingSession.getStatus()==SessionStatus.STARTED_VERIFIED){
|
---|
| 125 | throw new IllegalStateException("The session you are trying to verify has already been verified");
|
---|
| 126 | }
|
---|
[ccb2d72] | 127 | ParkingZone parkingZone = parkingSession.getParkingZone();
|
---|
[9ff45d6] | 128 | // else if(parkingSession.getStatus()!=SessionStatus.STARTED_)
|
---|
| 129 | if(!parkingZone.getParkingSpaces().contains(parkingSpaceRepository.findByPsName(parkingSpaceName))){
|
---|
| 130 | 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");
|
---|
| 131 | }
|
---|
[ad4da86] | 132 | if(parkingSpaceRepository.findByPsName(parkingSpaceName).isTaken()){
|
---|
| 133 | throw new IllegalStateException("Ivalid parking space inserted. Type in a new parking space, this space is already in a session");
|
---|
| 134 | }
|
---|
[9ff45d6] | 135 | parkingSession.setStatus(SessionStatus.STARTED_VERIFIED);
|
---|
[ccb2d72] | 136 | ParkingSpace parkingSpace = parkingZoneRepository.findByPzId(parkingZone.getId()).getParkingSpaces().stream().filter(ps->ps.getPsName().equals(parkingSpaceName)).findFirst().get();
|
---|
[9ff45d6] | 137 | parkingSpace.setTaken(true);
|
---|
| 138 | parkingSession.setParkingSpace(parkingSpace);
|
---|
| 139 | return parkingSession;
|
---|
| 140 | }
|
---|
| 141 | @Transactional
|
---|
[3f0bf09] | 142 | public ParkingSession endParkingSession(){
|
---|
[3b17c69] | 143 | ParkingSession parkingSession=getParkingSession();
|
---|
| 144 | if(parkingSession==null){
|
---|
[9ff45d6] | 145 | throw new IllegalStateException("No such session exists");
|
---|
| 146 | }
|
---|
[3b17c69] | 147 | else if(parkingSession.getStatus()==SessionStatus.ENDED_UNPAID){
|
---|
[9ff45d6] | 148 | throw new IllegalStateException("The session you are trying to end has already been ended");
|
---|
| 149 | }
|
---|
[3b17c69] | 150 | parkingSession.setStatus(SessionStatus.ENDED_UNPAID);
|
---|
| 151 | parkingSession.setTimeEnd(LocalDateTime.now());
|
---|
| 152 | return parkingSession;
|
---|
[9ff45d6] | 153 | }
|
---|
| 154 | @Transactional
|
---|
[b6e52e1] | 155 | public boolean payParkingSession(String date){
|
---|
[9ff45d6] | 156 | String expMonthString=date.split("/")[0];
|
---|
| 157 | String expYearString=date.split("/")[1];
|
---|
| 158 | if(expMonthString.startsWith("0"))
|
---|
[98f448a] | 159 | expMonthString=expMonthString.substring(1);
|
---|
[9ff45d6] | 160 | if(expYearString.startsWith("0"))
|
---|
| 161 | expYearString=expYearString.substring(1);
|
---|
| 162 | int expMonth=Integer.parseInt(expMonthString);
|
---|
| 163 | int expYear=Integer.parseInt(expYearString);
|
---|
| 164 | int month = LocalDateTime.now().getMonth().getValue();
|
---|
| 165 | int year = LocalDateTime.now().getYear()%100;
|
---|
| 166 | if(year>expYear){
|
---|
| 167 | throw new IllegalStateException("Your card has expired or the date that you have entered is incorrect");
|
---|
| 168 | }
|
---|
| 169 | else if(year==expYear){
|
---|
| 170 | if(month>=expMonth)
|
---|
| 171 | throw new IllegalStateException("Your card has expired or the date that you have entered is incorrect");
|
---|
| 172 | }
|
---|
[3b17c69] | 173 | ParkingSession parkingSession=getParkingSession();
|
---|
[9ff45d6] | 174 | if(parkingSession==null){
|
---|
| 175 | throw new IllegalStateException("No such session exists");
|
---|
| 176 | }
|
---|
| 177 | else if(parkingSession.getStatus()==SessionStatus.ENDED_PAID){
|
---|
| 178 | throw new IllegalStateException("The session has already been paid for");
|
---|
[ce6ad22] | 179 | }
|
---|
[9ff45d6] | 180 | parkingSession.setStatus(SessionStatus.ENDED_PAID);
|
---|
| 181 | parkingSession.getParkingSpace().setTaken(false);
|
---|
| 182 | return true;
|
---|
[ce6ad22] | 183 | }
|
---|
[52df867] | 184 |
|
---|
[843a941] | 185 | public SessionStatus getStatusOfParkirac(){
|
---|
| 186 | ParkingSession session=getParkingSession();
|
---|
| 187 | return session.getStatus();
|
---|
[52df867] | 188 | }
|
---|
[3b17c69] | 189 |
|
---|
| 190 | public ParkingSession getParkingSession() {
|
---|
| 191 | Authentication token = ParkUpApplication.getToken();
|
---|
| 192 | String role =token.getAuthorities().stream().findFirst().get().getAuthority();
|
---|
| 193 | String email =token.getName();
|
---|
| 194 | ParkingSession parkingSession;
|
---|
| 195 | if(role.equals("ROLE_REG_USER")){
|
---|
| 196 | RegisteredUser user = registeredUserRepository.findRegisteredUserByEmail(email).get();
|
---|
| 197 | parkingSession=user.getSession();
|
---|
| 198 | }
|
---|
| 199 | else{
|
---|
| 200 | Guest user = guestRepository.findGuestByEmail(email).get();
|
---|
| 201 | parkingSession=user.getSession();
|
---|
| 202 | }
|
---|
| 203 | return parkingSession;
|
---|
| 204 | }
|
---|
[ce6ad22] | 205 | }
|
---|