forked from getfider/fider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
135 lines (121 loc) · 3.09 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
package inmemory
import (
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/models"
"github.com/gosimple/slug"
)
// TagStorage contains read and write operations for tags
type TagStorage struct {
lastID int
tags []*models.Tag
user *models.User
tenant *models.Tenant
assigned map[int][]*models.Tag
}
// NewTagStorage creates a new TagStorage
func NewTagStorage() *TagStorage {
return &TagStorage{
tags: make([]*models.Tag, 0),
assigned: make(map[int][]*models.Tag),
}
}
// 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) {
s.lastID = s.lastID + 1
tag := &models.Tag{
ID: s.lastID,
Name: name,
Slug: slug.Make(name),
Color: color,
IsPublic: isPublic,
}
s.tags = append(s.tags, tag)
return tag, nil
}
// GetBySlug returns tag by given slug
func (s *TagStorage) GetBySlug(slug string) (*models.Tag, error) {
for _, tag := range s.tags {
if tag.Slug == slug {
return tag, nil
}
}
return nil, app.ErrNotFound
}
// Update a tag with given input
func (s *TagStorage) Update(tag *models.Tag, name, color string, isPublic bool) (*models.Tag, error) {
for _, storedTag := range s.tags {
if storedTag.ID == tag.ID {
storedTag.Name = name
storedTag.Slug = slug.Make(name)
storedTag.Color = color
storedTag.IsPublic = isPublic
return tag, nil
}
}
return nil, app.ErrNotFound
}
// Delete a tag by its id
func (s *TagStorage) Delete(tag *models.Tag) error {
for i, storedTag := range s.tags {
if storedTag.ID == tag.ID {
s.tags = append(s.tags[:i], s.tags[i+1:]...)
break
}
}
return nil
}
// AssignTag adds a tag to an post
func (s *TagStorage) AssignTag(tag *models.Tag, post *models.Post) error {
assigned := s.assigned[post.ID]
if assigned != nil {
for _, assignedTag := range assigned {
if assignedTag.ID == tag.ID {
return nil
}
}
}
s.assigned[post.ID] = append(s.assigned[post.ID], tag)
return nil
}
// UnassignTag removes a tag from an post
func (s *TagStorage) UnassignTag(tag *models.Tag, post *models.Post) error {
assigned := s.assigned[post.ID]
if assigned != nil {
for i, assignedTag := range assigned {
if assignedTag.ID == tag.ID {
s.assigned[post.ID] = append(s.assigned[post.ID][:i], s.assigned[post.ID][i+1:]...)
return nil
}
}
}
return nil
}
// GetAssigned returns all tags assigned to given post
func (s *TagStorage) GetAssigned(post *models.Post) ([]*models.Tag, error) {
assigned := s.assigned[post.ID]
if assigned != nil {
return assigned, nil
}
return make([]*models.Tag, 0), nil
}
// GetAll returns all tags
func (s *TagStorage) GetAll() ([]*models.Tag, error) {
if s.user != nil && s.user.IsCollaborator() {
return s.tags, nil
}
tags := make([]*models.Tag, 0)
for _, tag := range s.tags {
if tag.IsPublic {
tags = append(tags, tag)
}
}
return tags, nil
}