Update code in endpoints\library.post.py

This commit is contained in:
Backend IM Bot 2025-03-20 18:39:17 +01:00
parent 1aa22cda74
commit 297094c549

View File

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