Skip to content

Commit

Permalink
add more data to admin parking lot pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
wuttinanhi committed Nov 14, 2022
1 parent 1f4d9ac commit 8eeb1c7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
6 changes: 3 additions & 3 deletions parking_lot/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

from http.client import CREATED, FORBIDDEN, NOT_FOUND, OK

from auth.decorator import admin_only, login_required
from flask import Blueprint, request
from marshmallow import Schema, fields, validate
from util.validate_request import validate_request

from auth.decorator import admin_only, login_required
from parking_lot.service import ParkingLotService
from util.validate_request import validate_request

blueprint = Blueprint("parking_lot", __name__, url_prefix="/parking_lot")

Expand Down Expand Up @@ -74,7 +74,7 @@ def admin_available_parking_lot():
response = []
parking_lots = ParkingLotService.get_all_parking_lot_with_available_status()
for obj in parking_lots:
response.append(obj.json())
response.append(obj.json_full())
return response


Expand Down
46 changes: 46 additions & 0 deletions parking_lot/service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from typing import List

from flask import current_app
from sqlalchemy.orm import Query
from sqlalchemy.sql import and_, desc
from werkzeug.exceptions import InternalServerError, NotFound

from car.model import Car
from database.database import db_session
from parking_lot.model import ParkingLot
from reservation.model import Reservation
from user.service import UserService


class CustomAvailableParkingLot:
Expand All @@ -17,6 +22,9 @@ def __init__(self, parking_lot: ParkingLot) -> None:
ParkingLotService.is_parking_lot_available(self.parking_lot)
and self.parking_lot.open_status
)
self.reservation = None
self.car = None
self.user = None

def json(self):
return {
Expand All @@ -26,6 +34,24 @@ def json(self):
"available": self.available,
}

def json_full(self):
self.reservation = ParkingLotService.get_last_reservation(self.parking_lot)
if self.reservation is not None:
self.car = ParkingLotService.get_parking_car(self.reservation)
self.user = UserService.find_by_id(self.reservation.user_id)

return {
"id": self.parking_lot.id,
"location": self.parking_lot.location,
"open_status": self.parking_lot.open_status,
"available": self.available,
"reservation": self.reservation.json()
if self.reservation is not None
else None,
"car": self.car.json() if self.car is not None else None,
"user": self.user.json_shareable() if self.user is not None else None,
}


class ParkingLotService:
@staticmethod
Expand Down Expand Up @@ -112,3 +138,23 @@ def get_all_parking_lot_with_available_status() -> List[CustomAvailableParkingLo
new_obj = CustomAvailableParkingLot(parking_lot)
parsed.append(new_obj)
return parsed

@staticmethod
def get_last_reservation(parking_lot: ParkingLot) -> Reservation:
query_reservation: Query = db_session.query(Reservation)
query_reservation = query_reservation.order_by(desc(Reservation.id)).where(
and_(
Reservation.end_time == None,
Reservation.parking_lot_id == parking_lot.id,
)
)
query_reservation = query_reservation.limit(1)
busy_reservation = query_reservation.first()
return busy_reservation

@staticmethod
def get_parking_car(reservation: Reservation) -> Car:
if reservation is None:
return None
car = Car.query.where(Car.id == reservation.car_id).first()
return car

0 comments on commit 8eeb1c7

Please sign in to comment.