32 lines
827 B
Python
32 lines
827 B
Python
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)
|
|
}
|
|
} |