VILLASweb-backend-go/routes/dashboard/dashboard_methods.go
Sonja Happ dab027eef6 - revise testing of dashboard enpoints
- clean up testdata, serializers and responses
- add validators for dashboards
- revise documentation of dashboard endpoints for swaggo
- revise endpoint implementations
2019-09-06 14:07:36 +02:00

74 lines
1.4 KiB
Go

package dashboard
import (
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/scenario"
)
type Dashboard struct {
common.Dashboard
}
func (v *Dashboard) save() error {
db := common.GetDB()
err := db.Create(v).Error
return err
}
func (d *Dashboard) ByID(id uint) error {
db := common.GetDB()
err := db.Find(d, id).Error
if err != nil {
return err
}
return nil
}
func (d *Dashboard) addToScenario() error {
db := common.GetDB()
var sim scenario.Scenario
err := sim.ByID(d.ScenarioID)
if err != nil {
return err
}
// save dashboard to DB
err = d.save()
if err != nil {
return err
}
// associate dashboard with scenario
err = db.Model(&sim).Association("Dashboards").Append(d).Error
return err
}
func (d *Dashboard) update(modifiedDab Dashboard) error {
db := common.GetDB()
// TODO do we allow to update scenarioID here as well?
err := db.Model(d).Updates(map[string]interface{}{
"Name": modifiedDab.Name,
"Grid": modifiedDab.Grid,
}).Error
return err
}
func (d *Dashboard) delete() error {
db := common.GetDB()
var sim scenario.Scenario
err := sim.ByID(d.ScenarioID)
if err != nil {
return err
}
// remove association between Dashboard and Scenario
// Dashboard itself is not deleted from DB, it remains as "dangling"
err = db.Model(&sim).Association("Dashboards").Delete(d).Error
return err
}