Update code in endpoints\library.post.py
This commit is contained in:
parent
1aa22cda74
commit
297094c549
@ -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)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user