aboutsummaryrefslogtreecommitdiff
path: root/src/db/tags.py
blob: 0f0d3459c2293e103b85e1fac6600deb331a0275 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)