diff --git a/.gitignore b/.gitignore index 9fe2dd2..9f798c2 100644 --- a/.gitignore +++ b/.gitignore @@ -166,3 +166,4 @@ cython_debug/ #.idea/ data/books.db *.db +.aider* diff --git a/fastapi_demo/dtos.py b/fastapi_demo/dtos.py index a1e9f87..5a22f63 100644 --- a/fastapi_demo/dtos.py +++ b/fastapi_demo/dtos.py @@ -8,9 +8,11 @@ class BookCreate(BaseModel): pages: int category: str = "Fiction" favorite: bool = False + read: bool = False class BookInfo(BookCreate): id: Optional[int] = None class BookFavorite(BaseModel): - favorite: bool \ No newline at end of file + favorite: bool + read: bool diff --git a/fastapi_demo/models.py b/fastapi_demo/models.py index 8a02d49..2f2c8a9 100644 --- a/fastapi_demo/models.py +++ b/fastapi_demo/models.py @@ -11,3 +11,4 @@ class Book(Base): pages = Column(Integer) category = Column(String, index=True, default="Fiction") favorite = Column(Boolean, default=False, index=True) + read = Column(Boolean, default=False, index=True) diff --git a/fastapi_demo/routers/books.py b/fastapi_demo/routers/books.py index a71ba2d..ffde8fe 100644 --- a/fastapi_demo/routers/books.py +++ b/fastapi_demo/routers/books.py @@ -26,7 +26,7 @@ def read_books(db: Session = Depends(get_db)): def create_book( book: BookCreate = Body(..., description="The details of the book to be created", examples={"title": "Example Book", "author": "John Doe", "year": 2021}), db: Session = Depends(get_db)): - db_book = Book(**book.model_dump()) + db_book = Book(**book.model_dump(), read=book.read) db.add(db_book) db.commit() db.refresh(db_book) @@ -56,6 +56,8 @@ def update_book(book_id: int, book: BookCreate, db: Session = Depends(get_db)): if db_book is None: raise HTTPException(status_code=404, detail="Book not found") for key, value in book.model_dump().items(): + if key == "read": + continue setattr(db_book, key, value) db.commit() db.refresh(db_book) @@ -86,4 +88,4 @@ def toggle_favorite(book_id: int, favorite: BookFavorite, db: Session = Depends( db_book.favorite = favorite.favorite db.commit() db.refresh(db_book) - return BookInfo(**db_book.__dict__) \ No newline at end of file + return BookInfo(**db_book.__dict__) diff --git a/fastapi_demo/static/index.html b/fastapi_demo/static/index.html index ec0e719..ff30ada 100644 --- a/fastapi_demo/static/index.html +++ b/fastapi_demo/static/index.html @@ -55,6 +55,10 @@