-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,48 @@ | ||
from typing import Union | ||
from fastapi import FastAPI | ||
from fastapi import FastAPI, HTTPException | ||
from models.todo import Todo | ||
|
||
app = FastAPI() | ||
|
||
todos = [] | ||
|
||
|
||
@app.get("/") | ||
async def read_root(): | ||
return { | ||
"Hello": "World" | ||
} | ||
return {"Hello": "World"} | ||
|
||
|
||
@app.post("/todo/") | ||
async def create_todo(todo: Todo): | ||
todos.append(todo) | ||
return todo | ||
|
||
|
||
@app.get("/todos/") | ||
async def get_todos(): | ||
return todos | ||
|
||
|
||
@app.get("/todo/{todo_title}/") | ||
async def get_todo(todo_title: str): | ||
for todo in todos: | ||
if todo.title == todo_title: | ||
return todo | ||
raise HTTPException(status_code=404) | ||
|
||
|
||
@app.put("/todos/{todo_title}/") | ||
async def update_todo(todo_title: str, new_todo: Todo): | ||
for todo in todos: | ||
if todo.title == todo_title: | ||
todo.title = new_todo.title | ||
todo.description = new_todo.description | ||
return todo | ||
raise HTTPException(status_code=404, detail="Todo not found") | ||
|
||
|
||
@app.delete("/todos/{todo_title}/") | ||
async def delete_todo(todo_title: str): | ||
global todos | ||
todos = [todo for todo in todos if todo.title != todo_title] | ||
return {"message": "Todo deleted successfully"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class Todo(BaseModel): | ||
title: str | ||
description: str = None |