feat: Generated endpoint endpoints/books.get.py via AI for Book
This commit is contained in:
parent
152ccaee2e
commit
c6125deaf7
32
alembic/versions/20250416_093527_2f996c61_update_book.py
Normal file
32
alembic/versions/20250416_093527_2f996c61_update_book.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
"""create table for books
|
||||||
|
Revision ID: 2c5f9e7a1b2d
|
||||||
|
Revises: 0001
|
||||||
|
Create Date: 2023-05-26 14:48:12.450589
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '2c5f9e7a1b2d'
|
||||||
|
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('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,16 @@
|
|||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
from typing import List, Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from core.database import get_db
|
||||||
|
from schemas.book import BookSchema
|
||||||
|
from helpers.book_helpers import get_books
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.get("/books", response_model=List[BookSchema])
|
||||||
|
async def get_all_books(
|
||||||
|
search_term: Optional[str] = None,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
books = get_books(db, search_term=search_term)
|
||||||
|
return books
|
97
helpers/book_helpers.py
Normal file
97
helpers/book_helpers.py
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy import or_
|
||||||
|
from models.book import Book
|
||||||
|
from schemas.book import BookSchema, BookCreate, BookUpdate
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
def get_books(db: Session, search_term: Optional[str] = None) -> List[BookSchema]:
|
||||||
|
"""
|
||||||
|
Retrieves a list of books from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
search_term (Optional[str]): An optional search term to filter books by title or author.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[BookSchema]: A list of book objects matching the search criteria.
|
||||||
|
"""
|
||||||
|
query = db.query(Book)
|
||||||
|
if search_term:
|
||||||
|
query = query.filter(or_(
|
||||||
|
Book.title.ilike(f"%{search_term}%"),
|
||||||
|
Book.author.ilike(f"%{search_term}%")
|
||||||
|
))
|
||||||
|
books = query.all()
|
||||||
|
return [BookSchema.from_orm(book) for book in books]
|
||||||
|
|
||||||
|
def get_book_by_id(db: Session, book_id: uuid.UUID) -> Optional[BookSchema]:
|
||||||
|
"""
|
||||||
|
Retrieves a single book by its ID.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
book_id (uuid.UUID): The ID of the book to retrieve.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[BookSchema]: The book object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
book = db.query(Book).filter(Book.id == book_id).first()
|
||||||
|
return BookSchema.from_orm(book) if book else None
|
||||||
|
|
||||||
|
def create_book(db: Session, book_data: BookCreate) -> BookSchema:
|
||||||
|
"""
|
||||||
|
Creates a new book in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
book_data (BookCreate): The data for the book to create.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
BookSchema: The newly created book object.
|
||||||
|
"""
|
||||||
|
db_book = Book(**book_data.dict())
|
||||||
|
db.add(db_book)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_book)
|
||||||
|
return BookSchema.from_orm(db_book)
|
||||||
|
|
||||||
|
def update_book(db: Session, book_id: uuid.UUID, book_data: BookUpdate) -> Optional[BookSchema]:
|
||||||
|
"""
|
||||||
|
Updates an existing book in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
book_id (uuid.UUID): The ID of the book to update.
|
||||||
|
book_data (BookUpdate): The updated data for the book.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[BookSchema]: The updated book object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
book = db.query(Book).filter(Book.id == book_id).first()
|
||||||
|
if book:
|
||||||
|
update_data = book_data.dict(exclude_unset=True)
|
||||||
|
for key, value in update_data.items():
|
||||||
|
setattr(book, key, value)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(book)
|
||||||
|
return BookSchema.from_orm(book)
|
||||||
|
return None
|
||||||
|
|
||||||
|
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.UUID): The ID of the book to delete.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the book was successfully deleted, False otherwise.
|
||||||
|
"""
|
||||||
|
book = db.query(Book).filter(Book.id == book_id).first()
|
||||||
|
if book:
|
||||||
|
db.delete(book)
|
||||||
|
db.commit()
|
||||||
|
return True
|
||||||
|
return False
|
16
models/book.py
Normal file
16
models/book.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
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)
|
||||||
|
pages = Column(Integer, nullable=True)
|
||||||
|
created_at = Column(DateTime, default=func.now())
|
||||||
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
27
schemas/book.py
Normal file
27
schemas/book.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
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")
|
||||||
|
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")
|
||||||
|
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