diff options
Diffstat (limited to 'src/db/tags.py')
-rw-r--r-- | src/db/tags.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/db/tags.py b/src/db/tags.py new file mode 100644 index 0000000..0f0d345 --- /dev/null +++ b/src/db/tags.py @@ -0,0 +1,71 @@ +from sqlite3 import Cursor +import time + +from model.tag import Tag, ValidTagForm + +def get(cursor: Cursor) -> 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) |