from sqlite3 import Cursor import time from model.task import Task, ValidTaskForm def get(cursor: Cursor) -> Task: cursor.execute( " SELECT" " id," " created_at," " updated_at," " name," " duration," " tag," " difficulty," " priority," " description" " FROM tasks") res = [] for task in cursor.fetchall(): res.append(Task( id = task[0], created_at = task[1], updated_at = task[2], name = task[3], duration = task[4], tag = task[5], difficulty = task[6], priority = task[7], description = task[8] )) return res def insert(cursor: Cursor, form: ValidTaskForm): now = int(time.time()) cursor.execute( " INSERT INTO tasks(" " created_at," " updated_at," " name," " duration," " tag," " difficulty," " priority," " description" " ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (now, now, form.name, form.duration, form.tag, int(form.difficulty), int(form.priority), form.description)) return Task( id = cursor.lastrowid, created_at = now, updated_at = now, name = form.name, duration = form.duration, tag = form.tag, difficulty = form.difficulty, priority = form.priority, description = form.description ) def update(cursor: Cursor, task: Task, form: ValidTaskForm): now = int(time.time()) cursor.execute( " UPDATE tasks SET" " updated_at = ?," " name = ?," " duration = ?," " tag = ?," " difficulty = ?," " priority = ?," " description = ?" " WHERE id = ?", (now, form.name, form.duration, form.tag, int(form.difficulty), int(form.priority), form.description, task.id)) return Task( id = task.id, created_at = task.created_at, updated_at = now, name = form.name, duration = form.duration, tag = form.tag, difficulty = form.difficulty, priority = form.priority, description = form.description ) def delete(cursor: Cursor, ids): if len(ids) >= 1: cursor.execute( 'DELETE FROM tasks WHERE id IN (%s)' % ','.join('?'*len(ids)), ids)