| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from dataclasses import dataclass |
| 4 |
|
| 5 |
from litestar import Litestar, patch |
| 6 |
from litestar.dto import DataclassDTO, DTOConfig, DTOData |
| 7 |
from litestar.params import FromPath |
| 8 |
|
| 9 |
|
| 10 |
@dataclass |
| 11 |
class Person: |
| 12 |
name: str |
| 13 |
age: int |
| 14 |
email: str |
| 15 |
id: int |
| 16 |
|
| 17 |
|
| 18 |
class ReadDTO(DataclassDTO[Person]): |
| 19 |
config = DTOConfig(exclude={"email"}) |
| 20 |
|
| 21 |
|
| 22 |
class PatchDTO(DataclassDTO[Person]): |
| 23 |
config = DTOConfig(exclude={"id"}, partial=True) |
| 24 |
|
| 25 |
|
| 26 |
@patch("/person/{person_id:int}", dto=PatchDTO, return_dto=ReadDTO, sync_to_thread=False) |
| 27 |
def update_person(person_id: FromPath[int], data: DTOData[Person]) -> Person: |
| 28 |
# Usually the Person would be retrieved from a database |
| 29 |
person = Person(id=person_id, name="John", age=50, email="[email protected]") |
| 30 |
return data.update_instance(person) |
| 31 |
|
| 32 |
|
| 33 |
app = Litestar(route_handlers=[update_person]) |