-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_manager.py
More file actions
66 lines (57 loc) · 2.4 KB
/
task_manager.py
File metadata and controls
66 lines (57 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from task import Task
from storage import TaskStorage
from datetime import datetime
class TaskManager:
def __init__(self, storage: TaskStorage):
self.storage = storage
def add_task(self, description: str) -> Task:
# create the new task object
tasks = self.storage.load_tasks()
id = max([task.id for task in tasks]) + 1 if tasks else 1
task = Task(id, description, 'todo', datetime.now().isoformat(), datetime.now().isoformat())
tasks.append(task)
self.storage.save_tasks(tasks)
return task
def update_task(self, task_id: int, description: str) -> Task:
tasks = self.storage.load_tasks()
task = next((task for task in tasks if task.id == task_id), None)
if task:
task.description = description
task.updated_at = datetime.now().isoformat()
self.storage.save_tasks(tasks)
return task
else:
raise ValueError(f'Task with id {task_id} not found')
def delete_task(self, task_id: int) -> None:
tasks = self.storage.load_tasks()
# filter out tasks with matching id
new_tasks = [task for task in tasks if task.id != task_id]
if len(new_tasks) == len(tasks):
raise ValueError(f'Task with id {task_id} not found')
self.storage.save_tasks(new_tasks)
def mark_in_progress(self, task_id: int) -> Task:
tasks = self.storage.load_tasks()
task = next((task for task in tasks if task.id == task_id), None)
if task:
task.status = 'in-progress'
task.updated_at = datetime.now().isoformat()
self.storage.save_tasks(tasks)
return task
else:
raise ValueError(f'Task with id {task_id} not found')
def mark_done(self, task_id: int) -> Task:
tasks = self.storage.load_tasks()
task = next((task for task in tasks if task.id == task_id), None)
if task:
task.status = 'done'
task.updated_at = datetime.now().isoformat()
self.storage.save_tasks(tasks)
return task
else:
raise ValueError(f'Task with id {task_id} not found')
def list_tasks(self, status: str = None) -> list[Task]:
tasks = self.storage.load_tasks()
if status is None:
return tasks
else:
return [task for task in tasks if task.status == status]