-
Notifications
You must be signed in to change notification settings - Fork 68
/
book_query_model.py
34 lines (27 loc) · 1.02 KB
/
book_query_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from typing import cast
from pydantic import BaseModel, Field
from dddpy.domain.book import Book
class BookReadModel(BaseModel):
"""BookReadModel represents data structure as a read model."""
id: str = Field(examples=["vytxeTZskVKR7C7WgdSP3d"])
isbn: str = Field(examples=["978-0321125217"])
title: str = Field(
examples=["Domain-Driven Design: Tackling Complexity in the Heart of Softwares"]
)
page: int = Field(ge=0, examples=[320])
read_page: int = Field(ge=0, examples=[120])
created_at: int = Field(examples=[1136214245000])
updated_at: int = Field(examples=[1136214245000])
class Config:
orm_mode = True
@staticmethod
def from_entity(book: Book) -> "BookReadModel":
return BookReadModel(
id=book.book_id,
isbn=book.isbn.value,
title=book.title,
page=book.page,
read_page=book.read_page,
created_at=cast(int, book.created_at),
updated_at=cast(int, book.updated_at),
)