Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update task management implementation #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions data/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,17 @@
{
"id": 1,
"task": {
"summary": "This is a summary of task",
"priority": 1
"summary": "Выучить Python",
"priority": 1,
"due_date": "2024-02-15T00:00:00"
}
},
{
"id": 3,
"id": 2,
"task": {
"summary": "new update",
"priority": 2
}
},
{
"id": 4,
"task": {
"summary": "New task",
"priority": 2
}
},
{
"id": 5,
"task": {
"summary": "task from API",
"priority": 2
"summary": "Создать Todo приложение",
"priority": 2,
"due_date": "2024-03-01T00:00:00"
}
}
]
]
37 changes: 14 additions & 23 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,33 @@

@app.get("/api/tasks")
async def get_tasks():
"""TODO
Fetch the list of all tasks
"""
return "TODO"
return await taskman.get_tasks()


@app.get("/api/tasks/{id}")
async def get_task(id: int):
"""TODO
Fetch the task by id
"""
return "TODO"
return await taskman.get_tasks(id)


@app.post("/api/tasks/create")
async def create_task(task: Task):
"""TODO
1. Create a new task and
2. Return the details of task
"""
return "TODO"
id = await taskman.create_task(task)
return await taskman.get_tasks(id)


@app.put("/api/tasks/{id}/update")
async def update_task(id: int, task: Task):
"""TODO
1. Update the task by id
2. Return the updated task
"""
return "TODO"
await taskman.update_task(id, task)
return await taskman.get_tasks(id)


@app.delete("/api/tasks/{id}/delete")
async def delete_task(id: int):
"""TODO
1. Delete the task by id
2. Return a confirmation of deletion
"""
return "TODO"
id = await taskman.delete_task(id)
response = {id: "Task successfully deleted"}
return jsonable_encoder(response)


if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
2 changes: 1 addition & 1 deletion model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Task(BaseModel):

summary: str
priority: int
# due_date: Optional[datetime]
due_date: Optional[datetime] = None


class TaskList(BaseModel):
Expand Down
57 changes: 42 additions & 15 deletions model/taskman.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,77 @@
from model.model import Task, TaskList
import json
from pydantic import parse_file_as
from typing import List, Optional
from .model import Task, TaskList, ID

filepath = "data/tasks.json"


async def data_to_json(data: List):
"""
TODO
1. Take input data, of a list of tasks
2. Write the data into a json file (tasks.json)
"""
pass
with open(filepath, "w") as f:
json.dump(data, f, indent=2, default=str)


async def get_tasks(id: Optional[int] = 0):
async def get_tasks(id: Optional[int] = None):
"""
TODO
1. Fetch all tasks if no argument (id) provided
2. Else fetch the task by id provided
"""
return "response"
with open(filepath, 'r') as f:
tasks = json.load(f)

if id is None:
return tasks

for task in tasks:
if task['id'] == id:
return task

return None


async def create_task(new_task: Task):
"""
TODO:
1. Create a new task and add it to the list of tasks
2. Write the updated tasklist to file
"""
return "id"
with open(filepath, 'r') as f:
tasks = json.load(f)

new_id = max([task['id'] for task in tasks], default=0) + 1
new_task_list = {'id': new_id, 'task': new_task.dict()}
tasks.append(new_task_list)

await data_to_json(tasks)
return new_id


async def delete_task(id):
async def delete_task(id: int):
"""
TODO:
1. Delete the task by id provided
"""
return "id"
with open(filepath, 'r') as f:
tasks = json.load(f)

tasks = [task for task in tasks if task['id'] != id]

await data_to_json(tasks)
return id


async def update_task(id: int, new_task: Task):
"""
TODO
1. Update the task by id based on new task details
2. write the updated tasklist to file
2. Write the updated tasklist to file
"""
return "id"
with open(filepath, 'r') as f:
tasks = json.load(f)

for task in tasks:
if task['id'] == id:
task['task'] = new_task.dict()
break

await data_to_json(tasks)