Simple todo
							parent
							
								
									c7d4477beb
								
							
						
					
					
						commit
						61138f7fcb
					
				@ -1,10 +1,58 @@
 | 
			
		||||
from dataclasses import dataclass
 | 
			
		||||
 | 
			
		||||
from litestar import Litestar
 | 
			
		||||
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("/")
 | 
			
		||||
async def index() -> str:
 | 
			
		||||
    return "<h1>Hello Litestar</h1>"
 | 
			
		||||
async def get_list(done: bool | None = None) -> list[TodoItem]:
 | 
			
		||||
    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])
 | 
			
		||||
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue