Skip to content

Commit b02c81a

Browse files
authored
Merge pull request #62 from ZFordDev/56-integrate-storage-layer-into-scheduler
rebase schedular to work with new db logic
2 parents 112182d + 02dc3be commit b02c81a

1 file changed

Lines changed: 117 additions & 37 deletions

File tree

src/logic/storage.py

Lines changed: 117 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,128 @@
1-
import json
2-
import os
3-
from .scheduler import Task # import the dataclass Task
1+
"""
2+
scheduler.py
3+
------------
4+
The Scheduler is now a thin façade over the Repository.
45
5-
SCHEMA_VERSION = 1
6+
IMPORTANT:
7+
- All JSON save/load logic has been removed.
8+
- All persistence is now handled by the Repository (SQLite).
9+
- The old Task dataclass is deprecated and replaced by Entry.
10+
- Method names are preserved for UI compatibility.
611
7-
# Absolute / robust path relative to this file
8-
DATA_FILE = os.path.join(os.path.dirname(__file__), "..", "data", "tasks.json")
9-
os.makedirs(os.path.dirname(DATA_FILE), exist_ok=True)
12+
This module does NOT:
13+
- Read or write JSON
14+
- Manage file paths
15+
- Perform schema logic
16+
- Talk directly to SQLite
1017
18+
It ONLY delegates to the Repository.
19+
"""
1120

21+
from typing import Optional, List
22+
from storage.repository import Repository
23+
from storage.models import Entry, Comment, Tag
1224

13-
def save_tasks(tasks, filepath=DATA_FILE):
25+
26+
class Scheduler:
1427
"""
15-
Save list of Task objects to JSON file
28+
Scheduler provides a UI-friendly API for task operations.
29+
30+
NOTE:
31+
- This class used to manage JSON storage.
32+
- It now depends entirely on the Repository for persistence.
33+
- All CRUD operations are DB-backed.
1634
"""
17-
data = {
18-
"version": SCHEMA_VERSION,
19-
"tasks": [t.to_dict() for t in tasks], # convert each Task → dict
20-
}
2135

22-
try:
23-
directory = os.path.dirname(filepath)
24-
if directory:
25-
os.makedirs(directory, exist_ok=True)
36+
def __init__(self, repo: Repository):
37+
# NEW RULE: Scheduler requires a Repository instance.
38+
self.repo = repo
2639

27-
with open(filepath, "w", encoding="utf-8") as f:
28-
json.dump(data, f, indent=2, ensure_ascii=False)
29-
except Exception as e:
30-
print(f"[ERROR] Failed to save tasks: {e}")
40+
# ---------------------------------------------------------
41+
# Task CRUD (Entry CRUD)
42+
# ---------------------------------------------------------
3143

44+
def add_task(
45+
self,
46+
title: str,
47+
description: Optional[str] = None,
48+
due_date: Optional[str] = None,
49+
) -> Entry:
50+
"""
51+
Create a new task (Entry) in the database.
52+
"""
53+
return self.repo.create_entry(
54+
title=title,
55+
description=description,
56+
due_date=due_date,
57+
)
3258

33-
def load_tasks(filepath=DATA_FILE):
34-
"""
35-
Load JSON file → return list of Task objects
36-
"""
37-
if not os.path.exists(filepath):
38-
return []
39-
40-
try:
41-
with open(filepath, "r", encoding="utf-8") as f:
42-
data = json.load(f)
43-
44-
tasks_data = data.get("tasks", [])
45-
return [Task.from_dict(t) for t in tasks_data] # convert dict → Task
46-
except Exception as e:
47-
print(f"[ERROR] Failed to load tasks: {e}")
48-
return []
59+
def update_task(
60+
self,
61+
task_id: int,
62+
title: Optional[str] = None,
63+
description: Optional[str] = None,
64+
due_date: Optional[str] = None,
65+
completed: Optional[bool] = None,
66+
) -> Optional[Entry]:
67+
"""
68+
Update an existing task.
69+
Only fields provided will be updated.
70+
"""
71+
return self.repo.update_entry(
72+
entry_id=task_id,
73+
title=title,
74+
description=description,
75+
due_date=due_date,
76+
completed=completed,
77+
)
78+
79+
def delete_task(self, task_id: int) -> None:
80+
"""
81+
Delete a task from the database.
82+
"""
83+
self.repo.delete_entry(task_id)
84+
85+
def get_task(self, task_id: int) -> Optional[Entry]:
86+
"""
87+
Retrieve a single task by ID.
88+
"""
89+
return self.repo.get_entry(task_id)
90+
91+
def list_tasks(self) -> List[Entry]:
92+
"""
93+
Return all tasks, newest first.
94+
"""
95+
return self.repo.list_entries()
96+
97+
# ---------------------------------------------------------
98+
# Comments
99+
# ---------------------------------------------------------
100+
101+
def add_comment(self, task_id: int, text: str) -> Comment:
102+
"""
103+
Add a comment to a task.
104+
"""
105+
return self.repo.add_comment(task_id, text)
106+
107+
def list_comments(self, task_id: int) -> List[Comment]:
108+
"""
109+
List all comments for a task.
110+
"""
111+
return self.repo.list_comments(task_id)
112+
113+
# ---------------------------------------------------------
114+
# Tags
115+
# ---------------------------------------------------------
116+
117+
def assign_tag(self, task_id: int, tag_name: str) -> Tag:
118+
"""
119+
Assign a tag to a task.
120+
Creates the tag if it does not exist.
121+
"""
122+
return self.repo.assign_tag(task_id, tag_name)
123+
124+
def get_tags(self) -> List[Tag]:
125+
"""
126+
Return all tags in the system.
127+
"""
128+
return self.repo.get_tags()

0 commit comments

Comments
 (0)