32 lines
1.2 KiB
Python
32 lines
1.2 KiB
Python
# 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. |