VILLASweb-backend-go/routes/simulator/simulator_methods.go
2019-07-18 13:25:41 +02:00

66 lines
1.6 KiB
Go

package simulator
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
)
type Simulator struct {
common.Simulator
}
func (s *Simulator) save() error {
db := common.GetDB()
err := db.Create(s).Error
return err
}
func (s *Simulator) ByID(id uint) error {
db := common.GetDB()
err := db.Find(s, id).Error
if err != nil {
return fmt.Errorf("Simulator with id=%v does not exist", id)
}
return nil
}
func (s *Simulator) update(modifiedSimulator Simulator) error {
db := common.GetDB()
err := db.Model(s).Updates(map[string]interface{}{
"UUID": modifiedSimulator.UUID,
"Host": modifiedSimulator.Host,
"Modeltype": modifiedSimulator.Modeltype,
"Uptime": modifiedSimulator.Uptime,
"State": modifiedSimulator.State,
"StateUpdateAt": modifiedSimulator.StateUpdateAt,
"Properties": modifiedSimulator.Properties,
"RawProperties": modifiedSimulator.RawProperties,
}).Error
return err
}
func (s *Simulator) delete() error {
db := common.GetDB()
no_simulationmodels := db.Model(s).Association("SimulationModels").Count()
if no_simulationmodels > 0 {
return fmt.Errorf("Simulator cannot be deleted as it is still used in SimulationModels (active or dangling)")
}
// delete Simulator from DB (does NOT remain as dangling)
err := db.Delete(s).Error
return err
}
func (s *Simulator) getModels() ([]common.SimulationModel, int, error) {
db := common.GetDB()
var models []common.SimulationModel
err := db.Order("ID asc").Model(s).Related(&models, "SimulationModels").Error
return models, len(models), err
}