Add endpoint for getting flights structured by date

This commit is contained in:
april 2024-01-05 10:11:13 -06:00
parent 5b6b5d819b
commit 78a4ca2984
2 changed files with 34 additions and 4 deletions

View File

@ -1,10 +1,11 @@
import logging import logging
from typing import Dict, Union, List
from fastapi import APIRouter, HTTPException, Depends from fastapi import APIRouter, HTTPException, Depends
from app.deps import get_current_user, admin_required from app.deps import get_current_user, admin_required
from database import flights as db 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 from schemas.user import UserDisplaySchema, AuthLevel
@ -13,13 +14,15 @@ router = APIRouter()
logger = logging.getLogger("flights") logger = logging.getLogger("flights")
@router.get('/', summary="Get flights logged by the currently logged-in user", status_code=200, @router.get('/', summary="Get flights logged by the currently logged-in user", status_code=200)
response_model=list[FlightConciseSchema])
async def get_flights(user: UserDisplaySchema = Depends(get_current_user), sort: str = "date", order: int = -1) -> list[ async def get_flights(user: UserDisplaySchema = Depends(get_current_user), sort: str = "date", order: int = -1) -> list[
FlightConciseSchema]: FlightConciseSchema]:
""" """
Get a list of the flights logged by the currently logged-in user 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 :return: List of flights
""" """
# l = get_flight_list(filters=[[{"field": "user", "operator": "eq", "value": user.id}]]) # 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 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, @router.get('/all', summary="Get all flights logged by all users", status_code=200,
dependencies=[Depends(admin_required)], response_model=list[FlightConciseSchema]) dependencies=[Depends(admin_required)], response_model=list[FlightConciseSchema])
async def get_all_flights(sort: str = "date", order: int = -1) -> 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 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 :return: List of flights
""" """
flights = await db.retrieve_flights(sort, order) flights = await db.retrieve_flights(sort, order)

View File

@ -1,5 +1,5 @@
import datetime import datetime
from typing import Optional, Annotated, Any from typing import Optional, Annotated, Any, Dict, Union, List, Literal
from bson import ObjectId from bson import ObjectId
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
@ -102,3 +102,6 @@ class FlightConciseSchema(BaseModel):
time_total: PositiveFloat time_total: PositiveFloat
comments: Optional[str] = None comments: Optional[str] = None
FlightByDateSchema = Dict[int, Union[List['FlightByDateSchema'], FlightConciseSchema]]