From 78a4ca2984269dfa590ce4f150e1060921a50147 Mon Sep 17 00:00:00 2001 From: april Date: Fri, 5 Jan 2024 10:11:13 -0600 Subject: [PATCH] Add endpoint for getting flights structured by date --- api/routes/flights.py | 33 ++++++++++++++++++++++++++++++--- api/schemas/flight.py | 5 ++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/api/routes/flights.py b/api/routes/flights.py index fc005b2..32292f7 100644 --- a/api/routes/flights.py +++ b/api/routes/flights.py @@ -1,10 +1,11 @@ import logging +from typing import Dict, Union, List from fastapi import APIRouter, HTTPException, Depends from app.deps import get_current_user, admin_required from database import flights as db -from schemas.flight import FlightConciseSchema, FlightDisplaySchema, FlightCreateSchema +from schemas.flight import FlightConciseSchema, FlightDisplaySchema, FlightCreateSchema, FlightByDateSchema from schemas.user import UserDisplaySchema, AuthLevel @@ -13,13 +14,15 @@ router = APIRouter() logger = logging.getLogger("flights") -@router.get('/', summary="Get flights logged by the currently logged-in user", status_code=200, - response_model=list[FlightConciseSchema]) +@router.get('/', summary="Get flights logged by the currently logged-in user", status_code=200) async def get_flights(user: UserDisplaySchema = Depends(get_current_user), sort: str = "date", order: int = -1) -> list[ FlightConciseSchema]: """ Get a list of the flights logged by the currently logged-in user + :param user: Current user + :param sort: Attribute to sort results by + :param order: Order of sorting (asc/desc) :return: List of flights """ # l = get_flight_list(filters=[[{"field": "user", "operator": "eq", "value": user.id}]]) @@ -27,12 +30,36 @@ async def get_flights(user: UserDisplaySchema = Depends(get_current_user), sort: return flights +@router.get('/by-date', summary="Get flights logged by the current user, categorized by date", status_code=200, + response_model=dict) +async def get_flights_by_date(user: UserDisplaySchema = Depends(get_current_user), sort: str = "date", + order: int = -1) -> dict: + """ + Get a list of the flights logged by the currently logged-in user, categorized by year, month, and day + + :param user: Current user + :param sort: Attribute to sort results by + :param order: Order of sorting (asc/desc) + :return: + """ + flights = await db.retrieve_flights(user.id, sort, order) + flights_ordered: FlightByDateSchema = {} + + for flight in flights: + date = flight.date + flights_ordered.setdefault(date.year, {}).setdefault(date.month, {}).setdefault(date.day, []).append(flight) + + return flights_ordered + + @router.get('/all', summary="Get all flights logged by all users", status_code=200, dependencies=[Depends(admin_required)], response_model=list[FlightConciseSchema]) async def get_all_flights(sort: str = "date", order: int = -1) -> list[FlightConciseSchema]: """ Get a list of all flights logged by any user + :param sort: Attribute to sort results by + :param order: Order of sorting (asc/desc) :return: List of flights """ flights = await db.retrieve_flights(sort, order) diff --git a/api/schemas/flight.py b/api/schemas/flight.py index 560b21e..4a75d25 100644 --- a/api/schemas/flight.py +++ b/api/schemas/flight.py @@ -1,5 +1,5 @@ import datetime -from typing import Optional, Annotated, Any +from typing import Optional, Annotated, Any, Dict, Union, List, Literal from bson import ObjectId from pydantic import BaseModel, Field @@ -102,3 +102,6 @@ class FlightConciseSchema(BaseModel): time_total: PositiveFloat comments: Optional[str] = None + + +FlightByDateSchema = Dict[int, Union[List['FlightByDateSchema'], FlightConciseSchema]]