27 lines
909 B
Python
27 lines
909 B
Python
from typing import List
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
from kontor_model.db.models.bookshelf import Author
|
|
from src.db.session import SessionDep
|
|
from kontor_model.schema.bookshelf.author import AuthorResponse, to_response
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/authors", response_model=List[AuthorResponse])
|
|
def get_all_authors(db: SessionDep) -> List[AuthorResponse]:
|
|
results: List[AuthorResponse] = []
|
|
authors = db.query(Author).all()
|
|
for author in authors:
|
|
response = to_response(author)
|
|
results.append(response)
|
|
return results
|
|
|
|
@router.get("/authors/{author_id}", response_model=AuthorResponse)
|
|
def get_author(author_id: str, db: SessionDep) -> AuthorResponse:
|
|
author = db.get(Author, author_id)
|
|
if author is None:
|
|
raise HTTPException(status_code=404, detail="Author could not be found")
|
|
response = to_response(author)
|
|
return response
|