-
Notifications
You must be signed in to change notification settings - Fork 1
/
tag.go
79 lines (70 loc) · 1.89 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
package model
import (
"github.com/go-pg/pg"
//uuidpkg "github.com/resonatecoop/user-api/pkg/uuid"
uuid "github.com/google/uuid"
pbUser "github.com/resonatecoop/user-api-template/proto/user"
)
// Tag provides basic tag structure
type Tag struct {
ID uuid.UUID `bun:"type:uuid,default:uuid_generate_v4()"`
Type string `bun:",notnull"`
Name string `bun:",notnull"`
}
// SearchTags find a tag by query string on Name
func SearchTags(query string, tagType string, db *pg.DB) ([]*Tag, error) {
var tags []*Tag
err := db.Model(&tags).
ColumnExpr("tag.id").
Where("to_tsvector('english'::regconfig, COALESCE(name, '')) @@ (plainto_tsquery('english'::regconfig, ?)) = true", query).
Where("type = ?", tagType).
Select()
if err != nil {
return nil, err
}
return tags, nil
}
// GetTags given tag UUID returns a Tag
func GetTags(tagIds []uuid.UUID, db *pg.DB) ([]*pbUser.Tag, error) {
tags := make([]*pbUser.Tag, len(tagIds))
if len(tags) > 0 {
var t []Tag
err := db.Model(&t).
Where("id in (?)", pg.In(tagIds)).
Select()
if err != nil {
return nil, err
}
for i, tag := range t {
tags[i] = &pbUser.Tag{Id: tag.ID.String(), Type: tag.Type, Name: tag.Name}
}
}
return tags, nil
}
// GetTagIDs accepts a slice tags
func GetTagIDs(t []*pbUser.Tag, db *pg.Tx) ([]uuid.UUID, error) {
tags := make([]*Tag, len(t))
tagIDs := make([]uuid.UUID, len(t))
for i, tag := range t {
if tag.Id == "" { // new tag to create and add
tags[i] = &Tag{Type: tag.Type, Name: tag.Name}
_, pgerr := db.Model(tags[i]).
Where("type = ?", tags[i].Type).
Where("lower(name) = lower(?)", tags[i].Name).
Returning("*").
SelectOrInsert()
if pgerr != nil {
return nil, pgerr
}
tagIDs[i] = tags[i].ID
tag.Id = tags[i].ID.String()
} else {
tagID, err := uuid.Parse(tag.Id)
if err != nil {
return nil, err
}
tagIDs[i] = tagID
}
}
return tagIDs, nil
}