From fef542a4066415bf0a049e115a4129bf312af31a Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 17:45:44 +0100 Subject: [PATCH] Add post endpoint for books --- endpoints/books.post.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 endpoints/books.post.py diff --git a/endpoints/books.post.py b/endpoints/books.post.py new file mode 100644 index 0000000..04cf222 --- /dev/null +++ b/endpoints/books.post.py @@ -0,0 +1,32 @@ +# Entity: Book + +from fastapi import APIRouter, Depends, HTTPException, status +from sqlalchemy.orm import Session +from app.api.db.database import get_db +# You'll need to add correct model and schema imports + +router = APIRouter() + +@router.post("/books", status_code=status.HTTP_201_CREATED) +async def create_book( + book_data: BookSchema, # Assuming BookSchema is the Pydantic model for book data + db: Session = Depends(get_db) +): + """ + Create a new book in the database. + + :param book_data: The book data to be saved. + :param db: The database session. + :return: The created book data. + """ + book = Book(**book_data.dict()) # Assuming Book is the SQLAlchemy model + db.add(book) + db.commit() + db.refresh(book) + return book + +``` + +This endpoint handles POST requests to the `/books` path. It expects the request body to contain the book data in the form of a `BookSchema` Pydantic model. The endpoint creates a new `Book` instance using the provided data, adds it to the database session, commits the changes, and returns the created book data. + +Note that you'll need to import the appropriate `Book` model and `BookSchema` Pydantic model in your actual implementation. \ No newline at end of file