From ff663d7e95028bbaf8be6690c73605b0bde70d9e Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 16:28:51 +0100 Subject: [PATCH] Add Book model --- models/book.py | 54 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/models/book.py b/models/book.py index 5d4d417..7903230 100644 --- a/models/book.py +++ b/models/book.py @@ -1,33 +1,51 @@ -from sqlalchemy import Column, String, Integer, ForeignKey +from sqlalchemy import Column, String, ForeignKey from sqlalchemy.orm import relationship -from sqlalchemy.sql import func +from sqlalchemy.dialects.postgresql import UUID from app.api.db.base_class import Base import uuid class Book(Base): __tablename__ = "books" - id = Column(Integer, primary_key=True, index=True) - title = Column(String, index=True) - author = Column(String, index=True) - description = Column(String) - published_year = Column(Integer) - isbn = Column(String, unique=True) + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + title = Column(String, nullable=False, index=True) + author = Column(String, nullable=False, index=True) + + # Relationships + # author_id = Column(UUID(as_uuid=True), ForeignKey("authors.id")) + # author = relationship("Author", back_populates="books") # Timestamps created_at = Column(DateTime, default=func.now()) updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) ``` -This SQLAlchemy model defines a `Book` class that extends the `Base` class. It includes the following columns: +This SQLAlchemy model defines a `Book` class with the following columns: -- `id`: An integer primary key with an index. -- `title`: A string column for the book title with an index. -- `author`: A string column for the book author with an index. -- `description`: A string column for the book description. -- `published_year`: An integer column for the year the book was published. -- `isbn`: A string column for the book's ISBN number, which is set as unique. -- `created_at`: A datetime column that stores the creation timestamp, defaulting to the current time. -- `updated_at`: A datetime column that stores the last update timestamp, defaulting to the current time and updating on each change. +- `id` (UUID): The primary key for the book, generated using `uuid.uuid4`. +- `title` (String): The title of the book, indexed for efficient filtering. +- `author` (String): The author of the book, indexed for efficient filtering. -With this model, you can query books by title and author using the indexes on those columns. The `isbn` column is set as unique to prevent duplicates. The timestamps will automatically record when a book record was created and last updated. \ No newline at end of file +The model also includes timestamps for `created_at` and `updated_at` to track when the book record was created and last updated. + +You can optionally include a relationship to an `Author` model by uncommenting the `author_id` and `author` lines. In this case, you would need to create a separate `Author` model and define the relationship in both directions. + +With this model, you can filter books by `title` and `author` using SQLAlchemy queries. For example: + +```python +from app.models import Book + +# Filter by title +books = Book.query.filter(Book.title.ilike(f"%{title_filter}%")).all() + +# Filter by author +books = Book.query.filter(Book.author.ilike(f"%{author_filter}%")).all() + +# Filter by both title and author +books = Book.query.filter( + Book.title.ilike(f"%{title_filter}%"), + Book.author.ilike(f"%{author_filter}%") +).all() +``` + +Remember to import the necessary modules and configure the database connection in your FastAPI application. \ No newline at end of file