aboutsummaryrefslogtreecommitdiff
path: root/src/db/tasks.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/db/tasks.py')
-rw-r--r--src/db/tasks.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/db/tasks.py b/src/db/tasks.py
new file mode 100644
index 0000000..26a430a
--- /dev/null
+++ b/src/db/tasks.py
@@ -0,0 +1,50 @@
+from sqlite3 import Cursor
+import time
+
+from model.task import Task, TaskForm
+
+def get(cursor: Cursor) -> Task:
+ cursor.execute('SELECT id, created_at, modified_at, name, tag FROM tasks')
+
+ res = []
+
+ for task in cursor.fetchall():
+ res.append(Task(
+ id = task[0],
+ created_at = task[1],
+ modified_at = task[2],
+ name = task[3],
+ tag = task[4]
+ ))
+
+ return res
+
+def insert(cursor: Cursor, taskForm):
+ now = int(time.time())
+ cursor.execute('INSERT INTO tasks(created_at, modified_at, name, tag) VALUES (?, ?, ?, ?)', (now, now, taskForm.name, taskForm.tag))
+ return Task(
+ id = cursor.lastrowid,
+ created_at = now,
+ modified_at = now,
+ name = taskForm.name,
+ tag = taskForm.tag
+ )
+
+def update(cursor: Cursor, task: Task, taskForm: TaskForm):
+ now = int(time.time())
+
+ cursor.execute(
+ 'UPDATE tasks SET modified_at = ?, name = ?, tag = ? WHERE id = ?',
+ (now, taskForm.name, taskForm.tag, task.id))
+
+ return Task(
+ id = task.id,
+ created_at = task.created_at,
+ modified_at = now,
+ name = taskForm.name,
+ tag = taskForm.tag
+ )
+
+def delete(cursor: Cursor, ids):
+ if len(ids) >= 1:
+ cursor.execute('DELETE FROM tasks WHERE id IN (%s)' % ','.join('?'*len(ids)), ids)