Skip to content

Commit 5e60b38

Browse files
Cli TODO app v0.1.1 Refactor TaskManagerSQLite to use TaskQueries for SQL commands.
- Created a new class TaskQueries in config.py to store all SQL queries. - Updated TaskManagerSQLite to utilize TaskQueries for executing SQL commands. - Improved code readability and maintainability by separating SQL logic from business logic.
1 parent 7ad2b0e commit 5e60b38

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

utils/configs.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,34 @@ class Config:
1313
url = 'https://github.com/smartlegionlab/'
1414
copyright_ = 'Copyright © 2024, A.A. Suvorov'
1515
help_url = 'https://github.com/smartlegionlab/todo_app_cli/'
16-
db = 'json' # json | sqlite
16+
db = 'sqlite' # json | sqlite
17+
18+
19+
class TaskQueries:
20+
CREATE_TABLE = '''
21+
CREATE TABLE IF NOT EXISTS tasks (
22+
id TEXT PRIMARY KEY,
23+
title TEXT NOT NULL,
24+
description TEXT,
25+
due_date TEXT,
26+
completed BOOLEAN NOT NULL,
27+
created_at TEXT NOT NULL,
28+
updated_at TEXT NOT NULL
29+
)
30+
'''
31+
32+
SELECT_ALL_TASKS = 'SELECT * FROM tasks'
33+
INSERT_TASK = '''
34+
INSERT INTO tasks (id, title, description, due_date, completed, created_at, updated_at)
35+
VALUES (?, ?, ?, ?, ?, ?, ?)
36+
'''
37+
SELECT_TASK_BY_ID = 'SELECT * FROM tasks WHERE id = ?'
38+
UPDATE_TASK = '''
39+
UPDATE tasks
40+
SET title = ?, description = ?, due_date = ?, completed = ?, updated_at = ?
41+
WHERE id = ?
42+
'''
43+
DELETE_TASK = 'DELETE FROM tasks WHERE id = ?'
44+
SELECT_TASK_TITLES = 'SELECT title FROM tasks'
45+
UPDATE_TASK_COMPLETED = 'UPDATE tasks SET completed = ? WHERE id = ?'
46+

utils/task_managers.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import sqlite3
1313
import uuid
1414

15+
from utils.configs import TaskQueries
16+
1517

1618
class Task:
1719
def __init__(self, title, description, due_date, completed=False):
@@ -150,17 +152,7 @@ def __init__(self, db_name='todo.db'):
150152
self.tasks = self.load()
151153

152154
def create_table(self):
153-
self.cursor.execute('''
154-
CREATE TABLE IF NOT EXISTS tasks (
155-
id TEXT PRIMARY KEY,
156-
title TEXT NOT NULL,
157-
description TEXT,
158-
due_date TEXT,
159-
completed BOOLEAN NOT NULL,
160-
created_at TEXT NOT NULL,
161-
updated_at TEXT NOT NULL
162-
)
163-
''')
155+
self.cursor.execute(TaskQueries.CREATE_TABLE)
164156
self.connection.commit()
165157

166158
@property
@@ -172,7 +164,7 @@ def completed_count(self):
172164
return sum(1 for task in self.tasks if task.completed)
173165

174166
def load(self):
175-
self.cursor.execute('SELECT * FROM tasks')
167+
self.cursor.execute(TaskQueries.SELECT_ALL_TASKS)
176168
return [
177169
Task.from_dict({
178170
'id': row[0],
@@ -192,16 +184,13 @@ def save(self):
192184
def create_task(self, title, description, due_date, completed=False):
193185
title = self.get_unique_title(title)
194186
task = Task(title, description, due_date, completed)
195-
self.cursor.execute('''
196-
INSERT INTO tasks (id, title, description, due_date, completed, created_at, updated_at)
197-
VALUES (?, ?, ?, ?, ?, ?, ?)
198-
''', (task.id, task.title, task.description, task.due_date, task.completed, task.created_at, task.updated_at))
187+
self.cursor.execute(TaskQueries.INSERT_TASK, (task.id, task.title, task.description, task.due_date, task.completed, task.created_at, task.updated_at))
199188
self.save()
200189
self.tasks = self.load()
201190
return True
202191

203192
def get_unique_title(self, title):
204-
self.cursor.execute('SELECT title FROM tasks')
193+
self.cursor.execute(TaskQueries.SELECT_TASK_TITLES)
205194
existing_titles = {row[0] for row in self.cursor.fetchall()}
206195
if title not in existing_titles:
207196
return title
@@ -218,7 +207,7 @@ def read_tasks(self):
218207
return self.tasks
219208

220209
def get_task(self, task_id):
221-
self.cursor.execute('SELECT * FROM tasks WHERE id = ?', (task_id,))
210+
self.cursor.execute(TaskQueries.SELECT_TASK_BY_ID, (task_id,))
222211
row = self.cursor.fetchone()
223212
if row:
224213
return Task.from_dict({
@@ -250,23 +239,19 @@ def update_task(self, task_id, title=None, description=None, due_date=None, comp
250239
completed = task.completed
251240

252241
updated_at = datetime.datetime.now().isoformat()
253-
self.cursor.execute('''
254-
UPDATE tasks
255-
SET title = ?, description = ?, due_date = ?, completed = ?, updated_at = ?
256-
WHERE id = ?
257-
''', (title, description, due_date, completed, updated_at, task_id))
242+
self.cursor.execute(TaskQueries.UPDATE_TASK, (title, description, due_date, completed, updated_at, task_id))
258243
self.save()
259244
self.tasks = self.load()
260245
return True
261246

262247
def delete_task(self, task_id):
263-
self.cursor.execute('DELETE FROM tasks WHERE id = ?', (task_id,))
248+
self.cursor.execute(TaskQueries.DELETE_TASK, (task_id,))
264249
self.save()
265250
self.tasks = self.load()
266251
return True
267252

268253
def mark_task_as_completed(self, task_id, completed):
269-
self.cursor.execute('UPDATE tasks SET completed = ? WHERE id = ?', (completed, task_id))
254+
self.cursor.execute(TaskQueries.UPDATE_TASK_COMPLETED, (completed, task_id))
270255
self.save()
271256
self.tasks = self.load()
272257
return True

0 commit comments

Comments
 (0)