1212import sqlite3
1313import uuid
1414
15+ from utils .configs import TaskQueries
16+
1517
1618class 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