| 1 |
from dataclasses import dataclass |
| 2 |
from typing import List |
| 3 |
|
| 4 |
from litestar import Litestar, get |
| 5 |
from litestar.exceptions import HTTPException |
| 6 |
from litestar.params import FromQuery |
| 7 |
|
| 8 |
|
| 9 |
@dataclass |
| 10 |
class TodoItem: |
| 11 |
title: str |
| 12 |
done: bool |
| 13 |
|
| 14 |
|
| 15 |
TODO_LIST: List[TodoItem] = [ |
| 16 |
TodoItem(title="Start writing TODO list", done=True), |
| 17 |
TodoItem(title="???", done=False), |
| 18 |
TodoItem(title="Profit", done=False), |
| 19 |
] |
| 20 |
|
| 21 |
|
| 22 |
@get("/") |
| 23 |
async def get_list(done: FromQuery[str]) -> List[TodoItem]: |
| 24 |
if done == "1": |
| 25 |
return [item for item in TODO_LIST if item.done] |
| 26 |
if done == "0": |
| 27 |
return [item for item in TODO_LIST if not item.done] |
| 28 |
raise HTTPException(f"Invalid query parameter value: {done!r}", status_code=400) |
| 29 |
|
| 30 |
|
| 31 |
app = Litestar([get_list]) |