✨ feat: Add shiny new endpoints/book.post.py endpoint for Book 🚀 📦 with updated dependencies ✅ (auto-linted)
This commit is contained in:
parent
a33f231d89
commit
237c45a074
32
alembic/versions/20250425_090107_5e16e111_update_book.py
Normal file
32
alembic/versions/20250425_090107_5e16e111_update_book.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
"""create table for books
|
||||||
|
Revision ID: 6a7d9e10f8c9
|
||||||
|
Revises: 0001
|
||||||
|
Create Date: 2023-05-22 15:35:31.614526
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '6a7d9e10f8c9'
|
||||||
|
down_revision = '0001'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table(
|
||||||
|
'books',
|
||||||
|
sa.Column('id', sa.String(36), primary_key=True),
|
||||||
|
sa.Column('title', sa.String(), nullable=False),
|
||||||
|
sa.Column('author', sa.String(), nullable=False),
|
||||||
|
sa.Column('description', sa.String(), nullable=True),
|
||||||
|
sa.Column('published_year', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('publisher', sa.String(), nullable=True),
|
||||||
|
sa.Column('genre', sa.String(), nullable=True),
|
||||||
|
sa.Column('pages', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(), server_default=func.now()),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now())
|
||||||
|
)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_table('books')
|
@ -0,0 +1,15 @@
|
|||||||
|
from fastapi import APIRouter, Depends, 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
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/book", status_code=status.HTTP_201_CREATED, response_model=BookSchema)
|
||||||
|
async def create_new_book(
|
||||||
|
book_data: BookCreate,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
new_book = create_book(db=db, book_data=book_data)
|
||||||
|
return new_book
|
90
helpers/book_helpers.py
Normal file
90
helpers/book_helpers.py
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from models.book import Book
|
||||||
|
from schemas.book import BookCreate, BookUpdate
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
def get_all_books(db: Session) -> List[Book]:
|
||||||
|
"""
|
||||||
|
Retrieves all books from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Book]: A list of all book objects.
|
||||||
|
"""
|
||||||
|
return db.query(Book).all()
|
||||||
|
|
||||||
|
def get_book_by_id(db: Session, book_id: uuid.UUID) -> Optional[Book]:
|
||||||
|
"""
|
||||||
|
Retrieves a single book by its ID.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
book_id (UUID): The ID of the book to retrieve.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Book]: The book object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
return db.query(Book).filter(Book.id == book_id).first()
|
||||||
|
|
||||||
|
def create_book(db: Session, book_data: BookCreate) -> Book:
|
||||||
|
"""
|
||||||
|
Creates a new book in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
book_data (BookCreate): The data for the book to create.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Book: The newly created book object.
|
||||||
|
"""
|
||||||
|
db_book = Book(**book_data.dict())
|
||||||
|
db.add(db_book)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_book)
|
||||||
|
return db_book
|
||||||
|
|
||||||
|
def update_book(db: Session, book_id: uuid.UUID, book_data: BookUpdate) -> Optional[Book]:
|
||||||
|
"""
|
||||||
|
Updates an existing book in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
book_id (UUID): The ID of the book to update.
|
||||||
|
book_data (BookUpdate): The updated data for the book.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Book]: The updated book object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
db_book = db.query(Book).filter(Book.id == book_id).first()
|
||||||
|
if not db_book:
|
||||||
|
return None
|
||||||
|
|
||||||
|
for field, value in book_data.dict(exclude_unset=True).items():
|
||||||
|
setattr(db_book, field, value)
|
||||||
|
|
||||||
|
db.add(db_book)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_book)
|
||||||
|
return db_book
|
||||||
|
|
||||||
|
def delete_book(db: Session, book_id: uuid.UUID) -> bool:
|
||||||
|
"""
|
||||||
|
Deletes a book from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
book_id (UUID): The ID of the book to delete.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the book was successfully deleted, False otherwise.
|
||||||
|
"""
|
||||||
|
db_book = db.query(Book).filter(Book.id == book_id).first()
|
||||||
|
if not db_book:
|
||||||
|
return False
|
||||||
|
|
||||||
|
db.delete(db_book)
|
||||||
|
db.commit()
|
||||||
|
return True
|
19
models/book.py
Normal file
19
models/book.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
from sqlalchemy import Column, String, Integer, DateTime
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from core.database import Base
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
class Book(Base):
|
||||||
|
__tablename__ = "books"
|
||||||
|
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
title = Column(String, nullable=False)
|
||||||
|
author = Column(String, nullable=False)
|
||||||
|
description = Column(String, nullable=True)
|
||||||
|
published_year = Column(Integer, nullable=True)
|
||||||
|
publisher = Column(String, nullable=True)
|
||||||
|
genre = Column(String, nullable=True)
|
||||||
|
pages = Column(Integer, nullable=True)
|
||||||
|
created_at = Column(DateTime, default=func.now())
|
||||||
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
@ -7,3 +7,6 @@ sqlalchemy>=1.4.0
|
|||||||
python-dotenv>=0.19.0
|
python-dotenv>=0.19.0
|
||||||
bcrypt>=3.2.0
|
bcrypt>=3.2.0
|
||||||
alembic>=1.13.1
|
alembic>=1.13.1
|
||||||
|
jose
|
||||||
|
passlib
|
||||||
|
pydantic
|
||||||
|
33
schemas/book.py
Normal file
33
schemas/book.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import Optional
|
||||||
|
from datetime import datetime
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
class BookBase(BaseModel):
|
||||||
|
title: str = Field(..., description="Book title")
|
||||||
|
author: str = Field(..., description="Book author")
|
||||||
|
description: Optional[str] = Field(None, description="Book description")
|
||||||
|
published_year: Optional[int] = Field(None, description="Book published year")
|
||||||
|
publisher: Optional[str] = Field(None, description="Book publisher")
|
||||||
|
genre: Optional[str] = Field(None, description="Book genre")
|
||||||
|
pages: Optional[int] = Field(None, description="Number of pages")
|
||||||
|
|
||||||
|
class BookCreate(BookBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class BookUpdate(BookBase):
|
||||||
|
title: Optional[str] = Field(None, description="Book title")
|
||||||
|
author: Optional[str] = Field(None, description="Book author")
|
||||||
|
description: Optional[str] = Field(None, description="Book description")
|
||||||
|
published_year: Optional[int] = Field(None, description="Book published year")
|
||||||
|
publisher: Optional[str] = Field(None, description="Book publisher")
|
||||||
|
genre: Optional[str] = Field(None, description="Book genre")
|
||||||
|
pages: Optional[int] = Field(None, description="Number of pages")
|
||||||
|
|
||||||
|
class BookSchema(BookBase):
|
||||||
|
id: uuid.UUID
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user