Skip to content

Commit

Permalink
Added small API functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dahbar committed Oct 20, 2023
1 parent 1ef7a8b commit 5a056b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
46 changes: 42 additions & 4 deletions main.py
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"}
6 changes: 6 additions & 0 deletions models/todo.py
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

0 comments on commit 5a056b7

Please sign in to comment.