-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (51 loc) · 2.12 KB
/
main.py
File metadata and controls
62 lines (51 loc) · 2.12 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.requests import Request
from db import DatabaseManager
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
dbm = DatabaseManager("./privatekey.json")
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.get("/api/books/all", response_class=HTMLResponse)
async def get_all_books(request: Request):
books = dbm.list_books()
return templates.TemplateResponse(
"index.html", {"request": request, "books": books}
)
# @app.get("/api/books/{book_id}")
# async def get_book(book_id: str):
# book = dbm.get_book(book_id)
# if not book:
# raise HTTPException(status_code=404, detail="Book not found")
# return book
@app.get("/api/books/search",response_class=HTMLResponse)
#take the name of the book as a path parameter for the search query
async def search_books(request: Request, title: str):
if not title:
raise HTTPException(status_code=400, detail="Title query parameter is required")
books = dbm.list_books()
filtered_books=[book for book in books if title.lower() in book.get("title","").lower()]
return templates.TemplateResponse(
"books.html", {"request": request, "books": filtered_books}
)
@app.post("/api/books")
async def create_book(book: dict):
new_book = dbm.create_book(book)
return new_book
@app.put("/api/books/{book_id}")
async def update_book(book_id: str, book: dict):
updated_book = dbm.update_book(book_id, book)
if not updated_book:
raise HTTPException(status_code=404, detail="Book not found")
return updated_book
@app.delete("/api/books/{book_id}")
async def delete_book(book_id: str):
result = dbm.delete_book(book_id)
if not result:
raise HTTPException(status_code=404, detail="Book not found")
return {"detail": "Book deleted"}