aboutsummaryrefslogtreecommitdiff
path: root/src/db/tasks.py
blob: 26a430aab2c4c310c0df95968e751cf9ae7518fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)