aboutsummaryrefslogtreecommitdiff
path: root/todo/db/tags.py
diff options
context:
space:
mode:
Diffstat (limited to 'todo/db/tags.py')
-rw-r--r--todo/db/tags.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/todo/db/tags.py b/todo/db/tags.py
new file mode 100644
index 0000000..c5ce33c
--- /dev/null
+++ b/todo/db/tags.py
@@ -0,0 +1,72 @@
+from sqlite3 import Cursor
+import time
+from typing import List
+
+from todo.model.tag import Tag, ValidTagForm
+
+def get(cursor: Cursor) -> List[Tag]:
+ cursor.execute(
+ " SELECT"
+ " id,"
+ " created_at,"
+ " updated_at,"
+ " name,"
+ " color"
+ " FROM tags")
+
+ res = []
+
+ for tag in cursor.fetchall():
+ res.append(Tag(
+ id = tag[0],
+ created_at = tag[1],
+ updated_at = tag[2],
+ name = tag[3],
+ color = tag[4]
+ ))
+
+ return res
+
+def insert(cursor: Cursor, form: ValidTagForm):
+ now = int(time.time())
+ cursor.execute(
+ " INSERT INTO tags("
+ " created_at,"
+ " updated_at,"
+ " name,"
+ " color"
+ " ) VALUES (?, ?, ?, ?)",
+ (now, now, form.name, form.color))
+
+ return Tag(
+ id = cursor.lastrowid,
+ created_at = now,
+ updated_at = now,
+ name = form.name,
+ color = form.color
+ )
+
+def update(cursor: Cursor, tag: Tag, form: ValidTagForm):
+ now = int(time.time())
+
+ cursor.execute(
+ " UPDATE tags SET"
+ " updated_at = ?,"
+ " name = ?,"
+ " color = ?"
+ " WHERE id = ?",
+ (now, form.name, form.color, tag.id))
+
+ return Tag(
+ id = tag.id,
+ created_at = tag.created_at,
+ updated_at = now,
+ name = form.name,
+ color = form.color
+ )
+
+def delete(cursor: Cursor, ids):
+ if len(ids) >= 1:
+ cursor.execute(
+ "DELETE FROM tags WHERE id IN (%s)" % ",".join("?"*len(ids)),
+ ids)