extract SQLAlchemy model in separate python library
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
|
||||
from typing import List
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
from kontor_model.db.models.media import MediaLofi
|
||||
from src.db.session import SessionDep
|
||||
from kontor_model.schema.media.lofi import MediaLofiResponse, lofi_to_response
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/lofi", response_model=List[MediaLofiResponse])
|
||||
def get_all_lofis(
|
||||
db: SessionDep, review: bool = False, download: bool = False
|
||||
) -> List[MediaLofiResponse]:
|
||||
"""
|
||||
Get all MediaLofis.
|
||||
"""
|
||||
results: List[MediaLofiResponse] = []
|
||||
lofis: List[MediaLofi]
|
||||
if review:
|
||||
lofis = db.query(MediaLofi).filter(MediaLofi.review.is_(True)).all()
|
||||
elif download:
|
||||
lofis = db.query(MediaLofi).filter(MediaLofi.should_download.is_(True)).all()
|
||||
else:
|
||||
lofis = db.query(MediaLofi).all()
|
||||
for medialofi in lofis:
|
||||
response = lofi_to_response(medialofi)
|
||||
results.append(response)
|
||||
return results
|
||||
|
||||
@router.get("/lofi/{lofi_id}", response_model=MediaLofiResponse)
|
||||
def get_lofi(lofi_id: str, db: SessionDep) -> MediaLofiResponse:
|
||||
"""
|
||||
Get MediaLofi by id.
|
||||
"""
|
||||
lofi = db.get(MediaLofi, lofi_id)
|
||||
if lofi is None:
|
||||
raise HTTPException(status_code=404, detail="MediaLofi could not be found")
|
||||
response = lofi_to_response(lofi)
|
||||
return response
|
||||
Reference in New Issue
Block a user