From 297094c549341055e1904e4a2ef5e41f6641a885 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Thu, 20 Mar 2025 18:39:17 +0100 Subject: [PATCH] Update code in endpoints\library.post.py --- endpoints/library.post.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/endpoints/library.post.py b/endpoints/library.post.py index e69de29..e48e3f4 100644 --- a/endpoints/library.post.py +++ b/endpoints/library.post.py @@ -0,0 +1,32 @@ +from fastapi import APIRouter, Depends, HTTPException +from core.database import fake_library_db +import uuid + +router = APIRouter() + +@router.post("/library") +async def add_book_handler( + title: str, + author: str, + db: Session = Depends(get_db), + token: str = Depends(oauth2_scheme) +): + """Add a new book to the library""" + book_id = str(uuid.uuid4()) + if title in [book["title"] for book in fake_library_db.values()]: + raise HTTPException(status_code=400, detail="Book already exists") + + new_book = { + "id": book_id, + "title": title, + "author": author + } + fake_library_db[book_id] = new_book + + return { + "message": "Book added successfully", + "data": new_book, + "metadata": { + "total_books": len(fake_library_db) + } + } \ No newline at end of file