File size: 864 Bytes
06b349d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from fastapi import APIRouter, Depends, HTTPException

from app.models import Book, BookStats, BookStatusUpdate, Status
from app.store import BookStore, get_store

router = APIRouter()


@router.get("/api/books", response_model=list[Book])
def list_books(
    status: Status | None = None, store: BookStore = Depends(get_store)
) -> list[Book]:
    return store.list_books(status)


@router.patch("/api/books/{book_id}", response_model=Book)
def update_book_status(
    book_id: int, update: BookStatusUpdate, store: BookStore = Depends(get_store)
) -> Book:
    book = store.update_status(book_id, update.status)
    if book is None:
        raise HTTPException(status_code=404, detail="Book not found")
    return book


@router.get("/api/stats", response_model=BookStats)
def stats(store: BookStore = Depends(get_store)) -> BookStats:
    return store.counts()