Index: sources/app/src/main/java/parkup/services/ParkingSessionService.java
===================================================================
--- sources/app/src/main/java/parkup/services/ParkingSessionService.java	(revision 97fbc67a2265c2ab4f8d16b1d71ea5d43a129fa5)
+++ sources/app/src/main/java/parkup/services/ParkingSessionService.java	(revision 9ff45d65e1d0058eb0c79395ba62a726630c2daa)
@@ -4,23 +4,42 @@
 import org.springframework.stereotype.Service;
 import parkup.data.Tablicka;
+import parkup.data.enumarations.SessionStatus;
 import parkup.entities.ParkingSession;
+import parkup.entities.ParkingSpace;
+import parkup.entities.ParkingZone;
 import parkup.repositories.ParkingSessionRepository;
+import parkup.repositories.ParkingSpaceRepository;
+import parkup.repositories.ParkingZoneRepository;
+import parkup.repositories.TablickaRepository;
 
 import javax.transaction.Transactional;
+import java.time.Duration;
+import java.time.LocalDateTime;
 import java.util.List;
-import java.util.Objects;
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 @Service
 public class ParkingSessionService {
     private final ParkingSessionRepository parkingSessionRepository;
+    private final ParkingZoneRepository parkingZoneRepository;
+    private final ParkingSpaceRepository parkingSpaceRepository;
+    private final TablickaRepository tablickaRepository;
 
     @Autowired
-    public ParkingSessionService(ParkingSessionRepository parkingSessionRepository) {
+    public ParkingSessionService(ParkingSessionRepository parkingSessionRepository, ParkingZoneRepository parkingZoneRepository, ParkingSpaceRepository parkingSpaceRepository, TablickaRepository tablickaRepository) {
         this.parkingSessionRepository = parkingSessionRepository;
+        this.parkingZoneRepository = parkingZoneRepository;
+        this.parkingSpaceRepository = parkingSpaceRepository;
+        this.tablickaRepository = tablickaRepository;
     }
 
+    //TODO add getMethod for paid sessions or all sessions
+
     public List<ParkingSession> getAllParkingSessions() {
-        return parkingSessionRepository.findAll();
+        //Get all sessions that arent paid
+        return parkingSessionRepository.findAll().stream()
+                .filter(s->!s.getStatus().equals(SessionStatus.ENDED_PAID))
+                .collect(Collectors.toList());
     }
 
@@ -30,5 +49,5 @@
     }
 
-    @Transactional
+   /* @Transactional
     public void updateParkingSession(int parkingSessionId, String timeStart, String timeEnd, Tablicka tablicka) {
         Optional<ParkingSession> parkingSessionOpt = Optional.ofNullable(parkingSessionRepository.findByPssId(parkingSessionId));
@@ -48,9 +67,10 @@
             throw new IllegalStateException("The ParkingSession does not exist");
         }
-    }
-
+    }*/
+    @Transactional
     public Optional<ParkingSession> deleteParkingSession(int parkingSessionId) {
         Optional<ParkingSession> parkingSessionOpt = Optional.ofNullable(parkingSessionRepository.findByPssId(parkingSessionId));
         if (parkingSessionOpt.isPresent()) {
+            parkingSessionOpt.get().getParkingSpace().setTaken(false);
             parkingSessionRepository.deleteByPssId(parkingSessionId);
             return parkingSessionOpt;
@@ -60,13 +80,103 @@
     }
 
-    public Optional<ParkingSession> addParkingSession(ParkingSession parkingSession) {
-        Optional<ParkingSession> parkingSessionOpt = Optional.ofNullable(parkingSessionRepository.findByPssId(parkingSession.getPssId()));
-        if (parkingSessionOpt.isPresent()) {
-            throw new IllegalStateException("ID already taken, ParkingSession cannot be added");
-        } else {
-            System.out.println(parkingSession);
-            parkingSessionRepository.save(parkingSession);
-            return parkingSessionOpt;
+    @Transactional
+    public Optional<ParkingSession> startParkingSession(String tablica, int parkingZoneId) {
+        ParkingZone parkingZone = parkingZoneRepository.findByPzId(parkingZoneId);
+        Tablicka tablicka = tablickaRepository.findByTablica(tablica);
+        if(tablicka==null) {
+            tablickaRepository.save(new Tablicka(tablica));
         }
+        tablicka=tablickaRepository.findByTablica(tablica);
+        ParkingSession sessionToAdd = new ParkingSession(tablicka);
+        sessionToAdd.setParkingZone(parkingZone);
+        parkingSessionRepository.save(sessionToAdd);
+        return Optional.of(sessionToAdd);
+
+    }
+
+//    public void executePayment(int id){
+//        ParkingSession session = parkingSessionRepository.findByPssId(id);
+//        if(session==null){
+//            throw new IllegalStateException("No such session exists");
+//        }
+//        else if(session.getStatus()== SessionStatus.ENDED_UNPAID){
+//            throw new IllegalStateException("Cannot execute payment on an ended session");
+//        }
+//        session.setStatus(SessionStatus.ENDED_PAID);
+//    }
+
+    public int calculatePayment(int id){
+        ParkingSession session = parkingSessionRepository.findByPssId(id);
+        if(session==null){
+            throw new IllegalStateException("No such session exists");
+        }
+        else if(session.getStatus()== SessionStatus.ENDED_UNPAID){
+            throw new IllegalStateException("Cannot calculate payment on an ended session");
+        }
+        int price = session.getParkingZone().getPrice();
+        return (int) (Math.ceil(Duration.between(session.getTimeEnd(),session.getTimeStart()).toMinutes()/60.0)*price);
+    }
+    @Transactional
+    public ParkingSession verifyParkingSession(int id,String parkingSpaceName,int parkingZoneId){
+        ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
+        ParkingZone parkingZone = parkingZoneRepository.findByPzId(parkingZoneId);
+        if(parkingSession==null){
+            throw new IllegalStateException("No such session exists");
+        }
+        else if(parkingSession.getStatus()==SessionStatus.STARTED_VERIFIED){
+            throw new IllegalStateException("The session you are trying to verify has already been verified");
+        }
+//        else if(parkingSession.getStatus()!=SessionStatus.STARTED_)
+        if(!parkingZone.getParkingSpaces().contains(parkingSpaceRepository.findByPsName(parkingSpaceName))){
+            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");
+        }
+        parkingSession.setStatus(SessionStatus.STARTED_VERIFIED);
+        ParkingSpace parkingSpace = parkingZoneRepository.findByPzId(parkingZoneId).getParkingSpaces().stream().filter(ps->ps.getPsName().equals(parkingSpaceName)).findFirst().get();
+        parkingSpace.setTaken(true);
+        parkingSession.setParkingSpace(parkingSpace);
+        return parkingSession;
+    }
+    @Transactional
+    public ParkingSession endParkingSession(int id){
+        ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
+        if(parkingSession==null){
+            throw new IllegalStateException("No such session exists");
+        }
+        else if(parkingSession.getStatus()==SessionStatus.ENDED_UNPAID){
+            throw new IllegalStateException("The session you are trying to end has already been ended");
+        }
+        parkingSession.setStatus(SessionStatus.ENDED_UNPAID);
+        parkingSession.setTimeEnd(LocalDateTime.now());
+        return parkingSession;
+    }
+    @Transactional
+    public boolean payParkingSession(int id,String date){
+        ParkingSession parkingSession = parkingSessionRepository.findByPssId(id);
+        String expMonthString=date.split("/")[0];
+        String expYearString=date.split("/")[1];
+        if(expMonthString.startsWith("0"))
+            expMonthString=expYearString.substring(1);
+        if(expYearString.startsWith("0"))
+            expYearString=expYearString.substring(1);
+        int expMonth=Integer.parseInt(expMonthString);
+        int expYear=Integer.parseInt(expYearString);
+        int month = LocalDateTime.now().getMonth().getValue();
+        int year = LocalDateTime.now().getYear()%100;
+        if(year>expYear){
+            throw new IllegalStateException("Your card has expired or the date that you have entered is incorrect");
+        }
+        else if(year==expYear){
+            if(month>=expMonth)
+                throw new IllegalStateException("Your card has expired or the date that you have entered is incorrect");
+        }
+        if(parkingSession==null){
+            throw new IllegalStateException("No such session exists");
+        }
+        else if(parkingSession.getStatus()==SessionStatus.ENDED_PAID){
+            throw new IllegalStateException("The session has already been paid for");
+        }
+        parkingSession.setStatus(SessionStatus.ENDED_PAID);
+        parkingSession.getParkingSpace().setTaken(false);
+        return true;
     }
 }
