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 @@

Add a New Book

+
+ + +
@@ -184,4 +188,4 @@

Edit Book

- \ No newline at end of file + diff --git a/fastapi_demo/static/script.js b/fastapi_demo/static/script.js index 4cc3c0d..c9bb5a1 100644 --- a/fastapi_demo/static/script.js +++ b/fastapi_demo/static/script.js @@ -254,6 +254,7 @@ function displayBooks(books) { +

${book.read ? 'Read' : 'Not Read'}

@@ -286,7 +287,8 @@ async function addBook(e) { return; } - const newBook = { title, author, pages, category }; + const read = document.getElementById('read').checked; + const newBook = { title, author, pages, category, read }; try { const response = await fetch(API_URL + '/', { @@ -326,6 +328,7 @@ function openEditModal(book) { document.getElementById('edit-category').value = book.category; } + document.getElementById('edit-read').checked = book.read; editModal.style.display = 'block'; } @@ -349,7 +352,8 @@ async function updateBook(e) { return; } - const updatedBook = { title, author, pages, category }; + const read = document.getElementById('edit-read').checked; + const updatedBook = { title, author, pages, category, read }; try { const response = await fetch(`${API_URL}/${id}`, { @@ -513,4 +517,4 @@ notificationStyles.innerHTML = ` padding: 2rem 0; } `; -document.head.appendChild(notificationStyles); \ No newline at end of file +document.head.appendChild(notificationStyles);