Add Book schema

This commit is contained in:
Backend IM Bot 2025-03-26 19:29:53 +00:00
parent e03e26bda4
commit 49cae9142b

View File

@ -1,16 +1,13 @@
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from typing import Optional from typing import Optional
from datetime import datetime from datetime import datetime
from uuid import UUID
class BookBase(BaseModel): class BookBase(BaseModel):
title: str = Field(..., min_length=1, max_length=200, description="Book title") title: str = Field(..., min_length=1, max_length=200, description="Book title")
author: str = Field(..., min_length=1, max_length=100, description="Book author") author: str = Field(..., min_length=1, max_length=100, description="Book author")
isbn: str = Field(..., min_length=10, max_length=13, description="Book ISBN") isbn: str = Field(..., min_length=10, max_length=13, description="Book ISBN")
publication_year: Optional[int] = Field(None, ge=1000, le=2100, description="Year of publication")
genre: Optional[str] = Field(None, max_length=50, description="Book genre")
description: Optional[str] = Field(None, max_length=1000, description="Book description") description: Optional[str] = Field(None, max_length=1000, description="Book description")
price: Optional[int] = Field(None, ge=0, description="Book price")
available: bool = Field(default=True, description="Book availability status")
class BookCreate(BookBase): class BookCreate(BookBase):
class Config: class Config:
@ -19,16 +16,12 @@ class BookCreate(BookBase):
"title": "The Great Gatsby", "title": "The Great Gatsby",
"author": "F. Scott Fitzgerald", "author": "F. Scott Fitzgerald",
"isbn": "9780743273565", "isbn": "9780743273565",
"publication_year": 1925, "description": "A story of the fabulously wealthy Jay Gatsby"
"genre": "Fiction",
"description": "A story of the fabulously wealthy Jay Gatsby",
"price": 1499,
"available": True
} }
} }
class Book(BookBase): class Book(BookBase):
id: int = Field(..., description="Book ID") id: UUID
created_at: datetime created_at: datetime
updated_at: datetime updated_at: datetime
@ -36,15 +29,11 @@ class Book(BookBase):
orm_mode = True orm_mode = True
schema_extra = { schema_extra = {
"example": { "example": {
"id": 1, "id": "550e8400-e29b-41d4-a716-446655440000",
"title": "The Great Gatsby", "title": "The Great Gatsby",
"author": "F. Scott Fitzgerald", "author": "F. Scott Fitzgerald",
"isbn": "9780743273565", "isbn": "9780743273565",
"publication_year": 1925,
"genre": "Fiction",
"description": "A story of the fabulously wealthy Jay Gatsby", "description": "A story of the fabulously wealthy Jay Gatsby",
"price": 1499,
"available": True,
"created_at": "2023-01-01T00:00:00", "created_at": "2023-01-01T00:00:00",
"updated_at": "2023-01-01T00:00:00" "updated_at": "2023-01-01T00:00:00"
} }