-
-
Notifications
You must be signed in to change notification settings - Fork 619
/
tag.go
172 lines (145 loc) · 4.33 KB
/
tag.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package postgres
import (
"fmt"
"time"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/dbx"
"github.com/gosimple/slug"
)
type dbTag struct {
ID int `db:"id"`
Name string `db:"name"`
Slug string `db:"slug"`
Color string `db:"color"`
IsPublic bool `db:"is_public"`
}
func (t *dbTag) toModel() *models.Tag {
return &models.Tag{
ID: t.ID,
Name: t.Name,
Slug: t.Slug,
Color: t.Color,
IsPublic: t.IsPublic,
}
}
// TagStorage contains read and write operations for tags
type TagStorage struct {
trx *dbx.Trx
tenant *models.Tenant
user *models.User
}
// NewTagStorage creates a new TagStorage
func NewTagStorage(trx *dbx.Trx) *TagStorage {
return &TagStorage{
trx: trx,
}
}
// SetCurrentTenant to current context
func (s *TagStorage) SetCurrentTenant(tenant *models.Tenant) {
s.tenant = tenant
}
// SetCurrentUser to current context
func (s *TagStorage) SetCurrentUser(user *models.User) {
s.user = user
}
// Add creates a new tag with given input
func (s *TagStorage) Add(name, color string, isPublic bool) (*models.Tag, error) {
tagSlug := slug.Make(name)
var id int
err := s.trx.Get(&id, `
INSERT INTO tags (name, slug, color, is_public, created_on, tenant_id)
VALUES ($1, $2, $3, $4, $5, $6) RETURNING id
`, name, tagSlug, color, isPublic, time.Now(), s.tenant.ID)
if err != nil {
return nil, err
}
return s.GetBySlug(tagSlug)
}
// GetBySlug returns tag by given slug
func (s *TagStorage) GetBySlug(slug string) (*models.Tag, error) {
tag := dbTag{}
err := s.trx.Get(&tag, "SELECT id, name, slug, color, is_public FROM tags WHERE tenant_id = $1 AND slug = $2", s.tenant.ID, slug)
if err != nil {
return nil, err
}
return tag.toModel(), nil
}
// Update a tag with given input
func (s *TagStorage) Update(tag *models.Tag, name, color string, isPublic bool) (*models.Tag, error) {
tagSlug := slug.Make(name)
_, err := s.trx.Execute(`UPDATE tags SET name = $1, slug = $2, color = $3, is_public = $4
WHERE id = $5 AND tenant_id = $6`, name, tagSlug, color, isPublic, tag.ID, s.tenant.ID)
if err != nil {
return nil, err
}
return s.GetBySlug(tagSlug)
}
// Delete a tag by its id
func (s *TagStorage) Delete(tag *models.Tag) error {
_, err := s.trx.Execute(`DELETE FROM idea_tags WHERE tag_id = $1 AND tenant_id = $2`, tag.ID, s.tenant.ID)
if err != nil {
return err
}
_, err = s.trx.Execute(`DELETE FROM tags WHERE id = $1 AND tenant_id = $2`, tag.ID, s.tenant.ID)
return err
}
// AssignTag adds a tag to an idea
func (s *TagStorage) AssignTag(tag *models.Tag, idea *models.Idea) error {
alreadyAssigned, err := s.trx.Exists("SELECT 1 FROM idea_tags WHERE idea_id = $1 AND tag_id = $2 AND tenant_id = $3", idea.ID, tag.ID, s.tenant.ID)
if err != nil {
return err
}
if alreadyAssigned {
return nil
}
_, err = s.trx.Execute(
`INSERT INTO idea_tags (tag_id, idea_id, created_on, created_by_id, tenant_id) VALUES ($1, $2, $3, $4, $5)`,
tag.ID, idea.ID, time.Now(), s.user.ID, s.tenant.ID,
)
return err
}
// UnassignTag removes a tag from an idea
func (s *TagStorage) UnassignTag(tag *models.Tag, idea *models.Idea) error {
_, err := s.trx.Execute(
`DELETE FROM idea_tags WHERE tag_id = $1 AND idea_id = $2 AND tenant_id = $3`,
tag.ID, idea.ID, s.tenant.ID,
)
return err
}
// GetAssigned returns all tags assigned to given idea
func (s *TagStorage) GetAssigned(idea *models.Idea) ([]*models.Tag, error) {
return s.getTags(`
SELECT t.id, t.name, t.slug, t.color, t.is_public
FROM tags t
INNER JOIN idea_tags it
ON it.tag_id = t.id
AND it.tenant_id = t.tenant_id
WHERE it.idea_id = $1 AND t.tenant_id = $2
`, idea.ID, s.tenant.ID)
}
// GetAll returns all tags
func (s *TagStorage) GetAll() ([]*models.Tag, error) {
condition := `AND t.is_public = true`
if s.user != nil && s.user.IsCollaborator() {
condition = ``
}
query := fmt.Sprintf(`
SELECT t.id, t.name, t.slug, t.color, t.is_public
FROM tags t
WHERE t.tenant_id = $1 %s
`, condition)
return s.getTags(query, s.tenant.ID)
}
// GetAll returns all tags
func (s *TagStorage) getTags(query string, args ...interface{}) ([]*models.Tag, error) {
tags := []*dbTag{}
err := s.trx.Select(&tags, query, args...)
if err != nil {
return nil, err
}
var result = make([]*models.Tag, len(tags))
for i, tag := range tags {
result[i] = tag.toModel()
}
return result, nil
}