feat: Update endpoint books
This commit is contained in:
parent
a9f581af6f
commit
27137a6023
@ -0,0 +1,34 @@
|
|||||||
|
from typing import List
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.db import get_db
|
||||||
|
from app.models import Book
|
||||||
|
from app.schemas import BookCreate
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/books", response_model=List[Book])
|
||||||
|
async def create_book(book: BookCreate, db: Session = Depends(get_db)):
|
||||||
|
"""
|
||||||
|
Create a new book.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
book (BookCreate): The book data to create.
|
||||||
|
db (Session, optional): The database session. Defaults to Depends(get_db).
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
HTTPException: If the book already exists.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Book]: The created book.
|
||||||
|
"""
|
||||||
|
existing_book = db.query(Book).filter(Book.title == book.title).first()
|
||||||
|
if existing_book:
|
||||||
|
raise HTTPException(status_code=400, detail="Book already exists")
|
||||||
|
|
||||||
|
new_book = Book(**book.dict())
|
||||||
|
db.add(new_book)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(new_book)
|
||||||
|
|
||||||
|
return [new_book]
|
Loading…
x
Reference in New Issue
Block a user