From c6125deaf7523d16fbdacdcb9da741bca3728179 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Wed, 16 Apr 2025 09:35:48 +0000 Subject: [PATCH] feat: Generated endpoint endpoints/books.get.py via AI for Book --- .../20250416_093527_2f996c61_update_book.py | 32 ++++++ endpoints/books.get.py | 16 +++ helpers/book_helpers.py | 97 +++++++++++++++++++ models/book.py | 16 +++ schemas/book.py | 27 ++++++ 5 files changed, 188 insertions(+) create mode 100644 alembic/versions/20250416_093527_2f996c61_update_book.py create mode 100644 helpers/book_helpers.py create mode 100644 models/book.py create mode 100644 schemas/book.py diff --git a/alembic/versions/20250416_093527_2f996c61_update_book.py b/alembic/versions/20250416_093527_2f996c61_update_book.py new file mode 100644 index 0000000..8078473 --- /dev/null +++ b/alembic/versions/20250416_093527_2f996c61_update_book.py @@ -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') \ No newline at end of file diff --git a/endpoints/books.get.py b/endpoints/books.get.py index e69de29..8d2874a 100644 --- a/endpoints/books.get.py +++ b/endpoints/books.get.py @@ -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 \ No newline at end of file diff --git a/helpers/book_helpers.py b/helpers/book_helpers.py new file mode 100644 index 0000000..0dde1f6 --- /dev/null +++ b/helpers/book_helpers.py @@ -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 \ No newline at end of file diff --git a/models/book.py b/models/book.py new file mode 100644 index 0000000..128c900 --- /dev/null +++ b/models/book.py @@ -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()) \ No newline at end of file diff --git a/schemas/book.py b/schemas/book.py new file mode 100644 index 0000000..2df4aa2 --- /dev/null +++ b/schemas/book.py @@ -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 \ No newline at end of file