Spaces:
Sleeping
Sleeping
Update main.py
Browse filesadd new function to search by variable
main.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException, Request, Form
|
| 2 |
from fastapi.responses import HTMLResponse, RedirectResponse
|
| 3 |
from fastapi.templating import Jinja2Templates
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
from typing import List
|
| 6 |
import json
|
| 7 |
import os
|
| 8 |
|
|
@@ -53,15 +53,33 @@ def create_book_api(book: Book):
|
|
| 53 |
Create a new book record via JSON API.
|
| 54 |
"""
|
| 55 |
books = read_books_file()
|
| 56 |
-
|
| 57 |
# Check if a book with the same id already exists
|
| 58 |
if any(existing_book["id"] == book.id for existing_book in books):
|
| 59 |
raise HTTPException(status_code=400, detail="Book with this id already exists.")
|
| 60 |
-
|
| 61 |
books.append(book.dict())
|
| 62 |
write_books_file(books)
|
| 63 |
return book
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
# ---------------------------
|
| 66 |
# HTML Endpoints using Jinja2 & Bootstrap
|
| 67 |
# ---------------------------
|
|
@@ -92,17 +110,17 @@ def add_book(
|
|
| 92 |
Process the form data and add a new book.
|
| 93 |
"""
|
| 94 |
books = read_books_file()
|
| 95 |
-
|
| 96 |
# Check if a book with the same id already exists
|
| 97 |
if any(existing_book["id"] == id for existing_book in books):
|
| 98 |
return templates.TemplateResponse(
|
| 99 |
"add_book.html",
|
| 100 |
{"request": request, "error": "Book with this id already exists."}
|
| 101 |
)
|
| 102 |
-
|
| 103 |
new_book = {"id": id, "book_name": book_name, "year": year, "the_author": the_author}
|
| 104 |
books.append(new_book)
|
| 105 |
write_books_file(books)
|
| 106 |
-
|
| 107 |
# Redirect back to the homepage after a successful submission
|
| 108 |
return RedirectResponse("/", status_code=303)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request, Form, Query
|
| 2 |
from fastapi.responses import HTMLResponse, RedirectResponse
|
| 3 |
from fastapi.templating import Jinja2Templates
|
| 4 |
from pydantic import BaseModel
|
| 5 |
+
from typing import List, Optional
|
| 6 |
import json
|
| 7 |
import os
|
| 8 |
|
|
|
|
| 53 |
Create a new book record via JSON API.
|
| 54 |
"""
|
| 55 |
books = read_books_file()
|
| 56 |
+
|
| 57 |
# Check if a book with the same id already exists
|
| 58 |
if any(existing_book["id"] == book.id for existing_book in books):
|
| 59 |
raise HTTPException(status_code=400, detail="Book with this id already exists.")
|
| 60 |
+
|
| 61 |
books.append(book.dict())
|
| 62 |
write_books_file(books)
|
| 63 |
return book
|
| 64 |
|
| 65 |
+
@app.get("/api/books/search", response_model=List[Book])
|
| 66 |
+
def search_books_api(
|
| 67 |
+
book_name: Optional[str] = Query(None),
|
| 68 |
+
year: Optional[int] = Query(None),
|
| 69 |
+
the_author: Optional[str] = Query(None)
|
| 70 |
+
):
|
| 71 |
+
"""
|
| 72 |
+
Search for books by book name, year, or author.
|
| 73 |
+
"""
|
| 74 |
+
books = read_books_file()
|
| 75 |
+
filtered_books = [
|
| 76 |
+
book for book in books
|
| 77 |
+
if (book_name and book_name.lower() in book["book_name"].lower()) or
|
| 78 |
+
(year and book["year"] == year) or
|
| 79 |
+
(the_author and the_author.lower() in book["the_author"].lower())
|
| 80 |
+
]
|
| 81 |
+
return filtered_books
|
| 82 |
+
|
| 83 |
# ---------------------------
|
| 84 |
# HTML Endpoints using Jinja2 & Bootstrap
|
| 85 |
# ---------------------------
|
|
|
|
| 110 |
Process the form data and add a new book.
|
| 111 |
"""
|
| 112 |
books = read_books_file()
|
| 113 |
+
|
| 114 |
# Check if a book with the same id already exists
|
| 115 |
if any(existing_book["id"] == id for existing_book in books):
|
| 116 |
return templates.TemplateResponse(
|
| 117 |
"add_book.html",
|
| 118 |
{"request": request, "error": "Book with this id already exists."}
|
| 119 |
)
|
| 120 |
+
|
| 121 |
new_book = {"id": id, "book_name": book_name, "year": year, "the_author": the_author}
|
| 122 |
books.append(new_book)
|
| 123 |
write_books_file(books)
|
| 124 |
+
|
| 125 |
# Redirect back to the homepage after a successful submission
|
| 126 |
return RedirectResponse("/", status_code=303)
|