39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
from pydantic import BaseModel, Field
|
|
from typing import Optional
|
|
from datetime import datetime
|
|
from uuid import UUID
|
|
|
|
# Base schema for Book, used for inheritance
|
|
class BookBase(BaseModel):
|
|
title: str = Field(..., description="Book title")
|
|
author: str = Field(..., description="Book author")
|
|
description: Optional[str] = Field(None, description="Book description")
|
|
|
|
# Schema for creating a new Book
|
|
class BookCreate(BookBase):
|
|
pass
|
|
|
|
# Schema for updating an existing Book (all fields optional)
|
|
class BookUpdate(BaseModel):
|
|
title: Optional[str] = Field(None, description="Book title")
|
|
author: Optional[str] = Field(None, description="Book author")
|
|
description: Optional[str] = Field(None, description="Book description")
|
|
|
|
# Schema for representing a Book in responses
|
|
class BookSchema(BookBase):
|
|
id: UUID
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
schema_extra = {
|
|
"example": {
|
|
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
|
|
"title": "The Great Gatsby",
|
|
"author": "F. Scott Fitzgerald",
|
|
"description": "A novel about the American Dream",
|
|
"created_at": "2023-01-01T12:00:00",
|
|
"updated_at": "2023-01-01T12:00:00"
|
|
}
|
|
} |