added delete task endpoint

parent fa1b4264
...@@ -4,10 +4,12 @@ from pydantic import BaseModel ...@@ -4,10 +4,12 @@ from pydantic import BaseModel
router = fastapi.APIRouter(prefix='/api') router = fastapi.APIRouter(prefix='/api')
words = [
class Tasks:
tasks = [
{"id": 1, "name": "Баг"}, {"id": 2, "name": "Задача"}, {"id": 3, "name": "Тикет"}, {"id": 4, "name": "Приоритет"}, {"id": 1, "name": "Баг"}, {"id": 2, "name": "Задача"}, {"id": 3, "name": "Тикет"}, {"id": 4, "name": "Приоритет"},
{"id": 5, "name": "Отчёт об ошибке"} {"id": 5, "name": "Отчёт об ошибке"}
] ]
class Task(BaseModel): class Task(BaseModel):
...@@ -16,12 +18,12 @@ class Task(BaseModel): ...@@ -16,12 +18,12 @@ class Task(BaseModel):
@router.get('/tasks') @router.get('/tasks')
async def tasks(): async def tasks():
return words return Tasks.tasks
@router.get('/tasks/{id_}') @router.get('/tasks/{id_}')
async def get_task(id_: int): async def get_task(id_: int):
for word in words: for word in Tasks.tasks:
if word['id'] == id_: if word['id'] == id_:
return word return word
return {"error": "Задача не найдена"} return {"error": "Задача не найдена"}
...@@ -29,21 +31,31 @@ async def get_task(id_: int): ...@@ -29,21 +31,31 @@ async def get_task(id_: int):
@router.post('/tasks') @router.post('/tasks')
async def create_task(task: Task): async def create_task(task: Task):
id_ = len(words) + 1 id_ = len(Tasks.tasks) + 1
task = {"id": id_, "name": task.name} task = {"id": id_, "name": task.name}
words.append(task) Tasks.tasks.append(task)
return task return task
@router.put('/tasks/{id_}') @router.put('/tasks/{id_}')
async def update_task(id_: int, task: Task): async def update_task(id_: int, task: Task):
for word in words: for word in Tasks.tasks:
if word['id'] == id_: if word['id'] == id_:
word['name'] = task.name word['name'] = task.name
return word return word
return {"error": "Задача не найдена"} return {"error": "Задача не найдена"}
@router.delete('/tasks/{id_}')
async def delete_task(id_: int):
for word in Tasks.tasks:
if word['id'] == id_:
Tasks.tasks.remove(word)
return {"message": "Задача успешно удалена"}
return {"error": "Задача не найдена"}
app = fastapi.FastAPI() app = fastapi.FastAPI()
app.include_router(router) app.include_router(router)
...@@ -51,4 +63,4 @@ app.include_router(router) ...@@ -51,4 +63,4 @@ app.include_router(router)
if __name__ == '__main__': if __name__ == '__main__':
import uvicorn import uvicorn
uvicorn.run('main:app', host='localhost', port=8000, reload=True) uvicorn.run('main:app', host='0.0.0.0', port=8000, reload=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment