-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathshortcut_service.go
executable file
·94 lines (84 loc) · 3 KB
/
shortcut_service.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
package services
import (
"encoding/base64"
"kefu_server/models"
"time"
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego/orm"
)
// ShortcutRepositoryInterface interface
type ShortcutRepositoryInterface interface {
Add(admin *models.Shortcut, col1 string, cols ...string) (bool, int64, error)
GetShortcut(id int64) *models.Shortcut
GetShortcuts(uid int64) []models.Shortcut
Update(id int64, params *orm.Params) (int64, error)
Delete(id int64, uid int64) (int64, error)
}
// ShortcutRepository struct
type ShortcutRepository struct {
BaseRepository
}
// GetShortcutRepositoryInstance get instance
func GetShortcutRepositoryInstance() *ShortcutRepository {
instance := new(ShortcutRepository)
instance.Init(new(models.Shortcut))
return instance
}
// Add create a shortcut
func (r *ShortcutRepository) Add(shortcut *models.Shortcut, col1 string, cols ...string) (bool, int64, error) {
shortcut.Title = base64.StdEncoding.EncodeToString([]byte(shortcut.Title))
shortcut.Content = base64.StdEncoding.EncodeToString([]byte(shortcut.Content))
shortcut.CreateAt = time.Now().Unix()
_bool, id, err := r.o.ReadOrCreate(shortcut, col1, cols...)
if err != nil {
logs.Warn("Add create a shortcut------------", err)
}
return _bool, id, err
}
// GetShortcuts get user shortcut List
func (r *ShortcutRepository) GetShortcuts(uid int64) []models.Shortcut {
var shortcuts []models.Shortcut
if _, err := r.q.Filter("uid", uid).OrderBy("-create_at").All(&shortcuts); err != nil {
logs.Warn("GetShortcuts get user shortcut List------------", err)
return []models.Shortcut{}
}
// base 64转换回来
for index, shortcut := range shortcuts {
title, _ := base64.StdEncoding.DecodeString(shortcut.Title)
content, _ := base64.StdEncoding.DecodeString(shortcut.Content)
shortcuts[index].Title = string(title)
shortcuts[index].Content = string(content)
}
return shortcuts
}
// Delete delete a shortcut
func (r *ShortcutRepository) Delete(id int64, uid int64) (int64, error) {
index, err := r.q.Filter("id", id).Filter("uid", uid).Delete()
if err != nil {
logs.Warn("Delete delete a shortcut------------", err)
}
return index, err
}
// GetShortcut get one Shortcut
func (r *ShortcutRepository) GetShortcut(id int64) *models.Shortcut {
var shortcut models.Shortcut
if err := r.q.Filter("id", id).One(&shortcut); err != nil {
logs.Warn("GetShortcut get one shortcut------------", err)
return nil
}
title, _ := base64.StdEncoding.DecodeString(shortcut.Title)
content, _ := base64.StdEncoding.DecodeString(shortcut.Content)
shortcut.Title = string(title)
shortcut.Content = string(content)
return &shortcut
}
// Update shortcut
func (r *ShortcutRepository) Update(id int64, params orm.Params) (int64, error) {
params["Title"] = base64.StdEncoding.EncodeToString([]byte(params["Title"].(string)))
params["Content"] = base64.StdEncoding.EncodeToString([]byte(params["Content"].(string)))
index, err := r.q.Filter("id", id).Update(params)
if err != nil {
logs.Warn("Update shortcut------------", err)
}
return index, err
}