Commit e02e6017 by Torkel Ödegaard

Began work on user favorites backend support & storage

parent b25bf363
package models
type Favorite struct {
Id int64
UserId int64
DashboardId int64
}
type AddAsFavoriteCommand struct {
UserId int64
DashboardId int64
}
type RemoveAsFavoriteCommand struct {
UserId int64
DashboardId int64
}
package sqlstore
import (
"github.com/go-xorm/xorm"
"github.com/torkelo/grafana-pro/pkg/bus"
m "github.com/torkelo/grafana-pro/pkg/models"
)
func init() {
bus.AddHandler("sql", AddAsFavorite)
bus.AddHandler("sql", RemoveAsFavorite)
}
func AddAsFavorite(cmd *m.AddAsFavoriteCommand) error {
return inTransaction(func(sess *xorm.Session) error {
entity := m.Favorite{
UserId: cmd.UserId,
DashboardId: cmd.DashboardId,
}
_, err := sess.Insert(&entity)
return err
})
}
func RemoveAsFavorite(cmd *m.RemoveAsFavoriteCommand) error {
return inTransaction(func(sess *xorm.Session) error {
var rawSql = "DELETE FROM favorite WHERE user_id=? and dashboard_id=?"
_, err := sess.Exec(rawSql, cmd.UserId, cmd.DashboardId)
return err
})
}
......@@ -6,11 +6,11 @@ import . "github.com/torkelo/grafana-pro/pkg/services/sqlstore/migrator"
// 1. Never change a migration that is committed and pushed to master
// 2. Always add new migrations (to change or undo previous migrations)
// 3. Some migraitons are not yet written (rename column, table, drop table, index etc)
// 4
func addMigrations(mg *Migrator) {
addMigrationLogMigrations(mg)
addUserMigrations(mg)
addFavoritesMigrations(mg)
addAccountMigrations(mg)
addDashboardMigration(mg)
addDataSourceMigration(mg)
......@@ -55,6 +55,18 @@ func addUserMigrations(mg *Migrator) {
Table("user").Column(&Column{Name: "rands", Type: DB_NVarchar, Length: 255, Nullable: true}))
}
func addFavoritesMigrations(mg *Migrator) {
mg.AddMigration("create favorite table", new(AddTableMigration).
Name("favorite").WithColumns(
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
&Column{Name: "user_id", Type: DB_BigInt, Nullable: false},
&Column{Name: "dashboard_id", Type: DB_BigInt, Nullable: false},
))
mg.AddMigration("add unique index favorite.user_id_dashboard_id", new(AddIndexMigration).
Table("favorite").Columns("user_id", "dashboard_id").Unique())
}
func addAccountMigrations(mg *Migrator) {
mg.AddMigration("create account table", new(AddTableMigration).
Name("account").WithColumns(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment