Simple todo

master
Drew Bednar 1 year ago
parent c7d4477beb
commit 61138f7fcb

@ -1,10 +1,58 @@
from dataclasses import dataclass
from litestar import Litestar from litestar import Litestar
from litestar import get from litestar import get
from litestar import post
from litestar import put
from litestar.exceptions import NotFoundException
@dataclass
class TodoItem:
title: str
done: bool
TODO_LIST: list[TodoItem] = [
TodoItem(title="Start writing TODO list", done=True),
TodoItem(title="???", done=False),
TodoItem(title="Profit", done=False),
]
def get_todo_by_title(todo_name) -> TodoItem:
for item in TODO_LIST:
if item.title == todo_name:
return item
raise NotFoundException(detail=f"TODO {todo_name!r} not found")
@get("/") @get("/")
async def index() -> str: async def get_list(done: bool | None = None) -> list[TodoItem]:
return "<h1>Hello Litestar</h1>" if done is None:
return TODO_LIST
return [item for item in TODO_LIST if item.done == done]
@post("/")
async def add_item(data: TodoItem) -> list[TodoItem]:
TODO_LIST.append(data)
return TODO_LIST
@put("/{item_title:str}")
async def update_item(item_title: str, data: TodoItem) -> list[TodoItem]:
todo_item = get_todo_by_title(item_title)
todo_item.title = data.title
todo_item.done = data.done
return TODO_LIST
app = Litestar([index]) app = Litestar([get_list, add_item, update_item])

@ -1,11 +1,11 @@
from invoke import task from invoke import task
@task @task(name="dev")
def serve_dev(c): def serve_dev(c):
"""Runs the development server of this litestar project""" """Runs the development server of this litestar project"""
c.run("echo 'Starting dev server'") c.run("echo 'Starting dev server'")
c.run("LITESTAR_APP=learn_litestar.app:app litestar run") c.run("LITESTAR_APP=learn_litestar.app:app litestar run --reload")
@task @task

Loading…
Cancel
Save