26 lines
846 B
Python
26 lines
846 B
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from core.database import get_db
|
|
from schemas.book import BookCreate, BookSchema
|
|
from helpers.book_helpers import create_book, book_exists
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/books", status_code=status.HTTP_201_CREATED, response_model=BookSchema)
|
|
async def add_book(
|
|
book_data: BookCreate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Add a new book to the database using title and author
|
|
"""
|
|
# Check if book already exists
|
|
if book_exists(db, book_data.title, book_data.author):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Book with this title and author already exists"
|
|
)
|
|
|
|
# Create the book
|
|
new_book = create_book(db, book_data)
|
|
return new_book |