summaryrefslogtreecommitdiff
path: root/internal/store/labels.go
blob: 3c5f5eb59de2811ccb68beb0ed0396fc0cc3d1b7 (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
package store

import (
	"encoding/json"

	"task-dashboard/internal/models"
)

// SetTaskLabels replaces a task's label set entirely (not an add/remove
// single-label API), matching UpdateNativeTask's replace-whole-value style.
// Returns ErrNativeTaskNotFound if id doesn't match any row.
func (s *Store) SetTaskLabels(id string, labels []string) error {
	labelsJSON, _ := json.Marshal(labels)
	result, err := s.db.Exec(`
		UPDATE native_tasks SET labels = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?
	`, string(labelsJSON), id)
	if err != nil {
		return err
	}
	return checkRowsAffected(result)
}

// GetLabelColors returns every label that has been assigned a color.
func (s *Store) GetLabelColors() ([]models.LabelColor, error) {
	rows, err := s.db.Query(`SELECT name, color FROM labels ORDER BY name ASC`)
	if err != nil {
		return nil, err
	}
	defer func() { _ = rows.Close() }()

	var colors []models.LabelColor
	for rows.Next() {
		var c models.LabelColor
		if err := rows.Scan(&c.Name, &c.Color); err != nil {
			return nil, err
		}
		colors = append(colors, c)
	}
	return colors, rows.Err()
}

// SetLabelColor assigns (or reassigns) a label's display color.
func (s *Store) SetLabelColor(name, color string) error {
	_, err := s.db.Exec(`INSERT OR REPLACE INTO labels (name, color) VALUES (?, ?)`, name, color)
	return err
}