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)