-
Notifications
You must be signed in to change notification settings - Fork 34
/
star.go
73 lines (56 loc) · 1.54 KB
/
star.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
package sqlstore
import (
"github.com/go-xorm/xorm"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
)
func init() {
bus.AddHandler("sql", StarDashboard)
bus.AddHandler("sql", UnstarDashboard)
bus.AddHandler("sql", GetUserStars)
bus.AddHandler("sql", IsStarredByUser)
}
func IsStarredByUser(query *m.IsStarredByUserQuery) error {
rawSql := "SELECT 1 from star where user_id=? and dashboard_id=?"
results, err := x.Query(rawSql, query.UserId, query.DashboardId)
if err != nil {
return err
}
if len(results) == 0 {
return nil
}
query.Result = true
return nil
}
func StarDashboard(cmd *m.StarDashboardCommand) error {
if cmd.DashboardId == 0 || cmd.UserId == 0 {
return m.ErrCommandValidationFailed
}
return inTransaction(func(sess *xorm.Session) error {
entity := m.Star{
UserId: cmd.UserId,
DashboardId: cmd.DashboardId,
}
_, err := sess.Insert(&entity)
return err
})
}
func UnstarDashboard(cmd *m.UnstarDashboardCommand) error {
if cmd.DashboardId == 0 || cmd.UserId == 0 {
return m.ErrCommandValidationFailed
}
return inTransaction(func(sess *xorm.Session) error {
var rawSql = "DELETE FROM star WHERE user_id=? and dashboard_id=?"
_, err := sess.Exec(rawSql, cmd.UserId, cmd.DashboardId)
return err
})
}
func GetUserStars(query *m.GetUserStarsQuery) error {
var stars = make([]m.Star, 0)
err := x.Where("user_id=?", query.UserId).Find(&stars)
query.Result = make(map[int64]bool)
for _, star := range stars {
query.Result[star.DashboardId] = true
}
return err
}