1 | package parkup.services;
|
---|
2 |
|
---|
3 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
4 | import org.springframework.security.core.Authentication;
|
---|
5 | import org.springframework.stereotype.Service;
|
---|
6 | import parkup.ParkUpApplication;
|
---|
7 | import parkup.data.Plate;
|
---|
8 | import parkup.data.enumarations.SessionStatus;
|
---|
9 | import parkup.entities.*;
|
---|
10 | import parkup.repositories.*;
|
---|
11 |
|
---|
12 | import javax.transaction.Transactional;
|
---|
13 | import java.time.Duration;
|
---|
14 | import java.time.LocalDateTime;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Optional;
|
---|
17 | import java.util.stream.Collectors;
|
---|
18 |
|
---|
19 | @Service
|
---|
20 | public class ParkingSessionService {
|
---|
21 | private final ParkingSessionRepository parkingSessionRepository;
|
---|
22 | private final ParkingZoneRepository parkingZoneRepository;
|
---|
23 | private final ParkingSpaceRepository parkingSpaceRepository;
|
---|
24 | private final RegisteredUserRepository registeredUserRepository;
|
---|
25 | private final PlateRepository plateRepository;
|
---|
26 | private final GuestRepository guestRepository;
|
---|
27 |
|
---|
28 | @Autowired
|
---|
29 | public ParkingSessionService(ParkingSessionRepository parkingSessionRepository, ParkingZoneRepository parkingZoneRepository, ParkingSpaceRepository parkingSpaceRepository, RegisteredUserRepository registeredUserRepository, PlateRepository plateRepository, GuestRepository guestRepository) {
|
---|
30 | this.parkingSessionRepository = parkingSessionRepository;
|
---|
31 | this.parkingZoneRepository = parkingZoneRepository;
|
---|
32 | this.parkingSpaceRepository = parkingSpaceRepository;
|
---|
33 | this.registeredUserRepository = registeredUserRepository;
|
---|
34 | this.plateRepository = plateRepository;
|
---|
35 | this.guestRepository = guestRepository;
|
---|
36 | }
|
---|
37 |
|
---|
38 | //TODO add getMethod for paid sessions or all sessions
|
---|
39 |
|
---|
40 | public List<ParkingSession> getAllParkingSessions() {
|
---|
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());
|
---|
45 | }
|
---|
46 |
|
---|
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 |
|
---|
55 | public ParkingSession findById(int parkingSessionId) {
|
---|
56 | Optional<ParkingSession> parkingSessionOpt = Optional.ofNullable(parkingSessionRepository.findByPssId(parkingSessionId));
|
---|
57 | return parkingSessionOpt.orElse(null);
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Transactional
|
---|
61 | public Optional<ParkingSession> deleteParkingSession(int parkingSessionId) {
|
---|
62 | Optional<ParkingSession> parkingSessionOpt = Optional.ofNullable(parkingSessionRepository.findByPssId(parkingSessionId));
|
---|
63 | if (parkingSessionOpt.isPresent()) {
|
---|
64 | if(parkingSessionOpt.get().getStatus()!=SessionStatus.STARTED_UNVERIFIED)
|
---|
65 | parkingSessionOpt.get().getParkingSpace().setTaken(false);
|
---|
66 | parkingSessionOpt.get().setPlate(null);
|
---|
67 | parkingSessionRepository.deleteByPssId(parkingSessionId);
|
---|
68 | return parkingSessionOpt;
|
---|
69 | } else {
|
---|
70 | throw new IllegalStateException("ParkingSpace doesn't exist, therefore can't be deleted");
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Transactional
|
---|
75 | public Optional<ParkingSession> startParkingSession(String tablica, String parkingZoneName) {
|
---|
76 | ParkingZone parkingZone = parkingZoneRepository.findByPzName(parkingZoneName);
|
---|
77 | Authentication token = ParkUpApplication.getToken();
|
---|
78 | Plate plate = plateRepository.findByPlate(tablica);
|
---|
79 | ParkingSession sessionToAdd = new ParkingSession(plate);
|
---|
80 | sessionToAdd.setParkingZone(parkingZone);
|
---|
81 | parkingSessionRepository.save(sessionToAdd);
|
---|
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 | }
|
---|
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 | }
|
---|
112 | // else if(session.getStatus()!= SessionStatus.ENDED_PAID){
|
---|
113 | // throw new IllegalStateException("Cannot calculate payment on a paid session");
|
---|
114 | // }
|
---|
115 | int price = session.getParkingZone().getPrice();
|
---|
116 | return (int) (Math.ceil(Duration.between(session.getTimeStart(),session.getTimeEnd()).toMinutes()/60.0)*price);
|
---|
117 | }
|
---|
118 | @Transactional
|
---|
119 | public ParkingSession verifyParkingSession(int id,String parkingSpaceName){
|
---|
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 | }
|
---|
127 | ParkingZone parkingZone = parkingSession.getParkingZone();
|
---|
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 | }
|
---|
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 | }
|
---|
135 | parkingSession.setStatus(SessionStatus.STARTED_VERIFIED);
|
---|
136 | ParkingSpace parkingSpace = parkingZoneRepository.findByPzId(parkingZone.getId()).getParkingSpaces().stream().filter(ps->ps.getPsName().equals(parkingSpaceName)).findFirst().get();
|
---|
137 | parkingSpace.setTaken(true);
|
---|
138 | parkingSession.setParkingSpace(parkingSpace);
|
---|
139 | return parkingSession;
|
---|
140 | }
|
---|
141 | @Transactional
|
---|
142 | public ParkingSession endParkingSession(){
|
---|
143 | ParkingSession parkingSession=getParkingSession();
|
---|
144 | if(parkingSession==null){
|
---|
145 | throw new IllegalStateException("No such session exists");
|
---|
146 | }
|
---|
147 | else if(parkingSession.getStatus()==SessionStatus.ENDED_UNPAID){
|
---|
148 | throw new IllegalStateException("The session you are trying to end has already been ended");
|
---|
149 | }
|
---|
150 | parkingSession.setStatus(SessionStatus.ENDED_UNPAID);
|
---|
151 | parkingSession.setTimeEnd(LocalDateTime.now());
|
---|
152 | return parkingSession;
|
---|
153 | }
|
---|
154 | @Transactional
|
---|
155 | public boolean payParkingSession(String date){
|
---|
156 | String expMonthString=date.split("/")[0];
|
---|
157 | String expYearString=date.split("/")[1];
|
---|
158 | if(expMonthString.startsWith("0"))
|
---|
159 | expMonthString=expMonthString.substring(1);
|
---|
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 | }
|
---|
173 | ParkingSession parkingSession=getParkingSession();
|
---|
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");
|
---|
179 | }
|
---|
180 | parkingSession.setStatus(SessionStatus.ENDED_PAID);
|
---|
181 | parkingSession.getParkingSpace().setTaken(false);
|
---|
182 | return true;
|
---|
183 | }
|
---|
184 |
|
---|
185 | public SessionStatus getStatusOfParkirac(int regParkId){
|
---|
186 | RegisteredUser registeredUser = registeredUserRepository.findByRegParkId(regParkId);
|
---|
187 | List<String> plates =parkingSessionRepository.findAll().stream().filter(ps->registeredUser.getPlates().contains(ps.getPlate())).map(ps->ps.getPlate().getPlate()).collect(Collectors.toList());
|
---|
188 | if(plates.size()>0)
|
---|
189 | return parkingSessionRepository.findAll().stream().filter(ps->ps.getPlate().getPlate().equals(plates.get(0))).collect(Collectors.toList()).get(0).getStatus();
|
---|
190 | return null;
|
---|
191 | }
|
---|
192 |
|
---|
193 | public ParkingSession getParkingSession() {
|
---|
194 | Authentication token = ParkUpApplication.getToken();
|
---|
195 | String role =token.getAuthorities().stream().findFirst().get().getAuthority();
|
---|
196 | String email =token.getName();
|
---|
197 | ParkingSession parkingSession;
|
---|
198 | if(role.equals("ROLE_REG_USER")){
|
---|
199 | RegisteredUser user = registeredUserRepository.findRegisteredUserByEmail(email).get();
|
---|
200 | parkingSession=user.getSession();
|
---|
201 | }
|
---|
202 | else{
|
---|
203 | Guest user = guestRepository.findGuestByEmail(email).get();
|
---|
204 | parkingSession=user.getSession();
|
---|
205 | }
|
---|
206 | return parkingSession;
|
---|
207 | }
|
---|
208 | }
|
---|