diff --git a/db.sql b/db.sql index 840a595..b4025e6 100644 --- a/db.sql +++ b/db.sql @@ -2,6 +2,7 @@ DROP TABLE IF EXISTS books; CREATE TABLE books ( id SERIAL PRIMARY KEY, - title VARCHAR(50) UNIQUE NOT NULL, - author VARCHAR(100) UNIQUE NOT NULL + title VARCHAR(255) NOT NULL, + author VARCHAR(255) NOT NULL, + publisher VARCHAR(255) ); diff --git a/models.py b/models.py index bd73c60..6546b85 100644 --- a/models.py +++ b/models.py @@ -1,6 +1,7 @@ from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column from sqlalchemy import String from pydantic import BaseModel, ConfigDict +from typing import Optional # SQLAlchemy models @@ -18,6 +19,7 @@ class Book(Base): id: Mapped[int] = mapped_column(primary_key=True, index=True) title: Mapped[str] = mapped_column(String(255), index=True) author: Mapped[str] = mapped_column(String(255)) + publisher: Mapped[Optional[str]] = mapped_column(String(255), nullable=True) # Pydantic models class BookIn(BaseModel): @@ -25,6 +27,7 @@ class BookIn(BaseModel): title: str author: str + publisher: Optional[str] = None class BookOut(BaseModel): @@ -33,5 +36,6 @@ class BookOut(BaseModel): id: int title: str author: str + publisher: Optional[str] = None model_config = ConfigDict(from_attributes=True) diff --git a/repositories.py b/repositories.py index dc9ff88..6c4642b 100644 --- a/repositories.py +++ b/repositories.py @@ -4,7 +4,11 @@ # Create a new book def create_book(db: Session, book: models.BookIn): - db_book = models.Book(title=book.title, author=book.author) + db_book = models.Book( + title=book.title, + author=book.author, + publisher=book.publisher + ) db.add(db_book) db.commit() db.refresh(db_book) @@ -27,6 +31,7 @@ def update_book(db: Session, book_id: int, book: models.BookIn): if db_book: db_book.title = book.title db_book.author = book.author + db_book.publisher = book.publisher db.commit() db.refresh(db_book) return db_book