/
tag.go
86 lines (71 loc) · 1.94 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
package service
import (
"github.com/GarnBarn/garnbarn-backend-go/model"
"github.com/GarnBarn/garnbarn-backend-go/repository"
"github.com/pquerna/otp/totp"
"github.com/sirupsen/logrus"
)
type tag struct {
tagRepository repository.Tag
}
type Tag interface {
GetAllTag(author string) ([]model.Tag, error)
CreateTag(tag *model.Tag) error
UpdateTag(tagId int, tagUpdateRequest *model.UpdateTagRequest) (*model.Tag, error)
GetTagById(tagId int) (model.TagPublic, error)
DeleteTag(tagId int) error
IsTagExist(tagId int) bool
}
func NewTagService(tagRepository repository.Tag) Tag {
return &tag{
tagRepository: tagRepository,
}
}
func (t *tag) GetAllTag(author string) ([]model.Tag, error) {
return t.tagRepository.GetAllTag(author)
}
func (t *tag) CreateTag(tag *model.Tag) error {
// Create the otp secret
totpKeyResult, err := totp.Generate(totp.GenerateOpts{Issuer: "GarnBarn", AccountName: "GarnBarn"})
if err != nil {
logrus.Error(err)
return err
}
totpPrivateKey := totpKeyResult.Secret()
tag.SecretKeyTotp = totpPrivateKey
return t.tagRepository.Create(tag)
}
func (t *tag) UpdateTag(tagId int, tagUpdateRequest *model.UpdateTagRequest) (*model.Tag, error) {
// Get current tag
tag, err := t.tagRepository.GetByID(tagId)
if err != nil {
logrus.Error(err)
return nil, err
}
// Update the tag object
tagUpdateRequest.UpdateTag(tag)
// Update the data in db.
err = t.tagRepository.Update(tag)
return tag, err
}
func (t *tag) GetTagById(tagId int) (model.TagPublic, error) {
tag, err := t.tagRepository.GetByID(tagId)
if err != nil {
logrus.Error(err)
return model.TagPublic{}, err
}
return tag.ToTagPublic(true), nil
}
func (t *tag) DeleteTag(tagId int) error {
logrus.Info("Check delete tag")
defer logrus.Info("Complete delete tag")
err := t.tagRepository.DeleteTag(tagId)
return err
}
func (t *tag) IsTagExist(tagId int) bool {
_, err := t.GetTagById(tagId)
if err != nil {
logrus.Warn(err)
}
return err == nil
}