mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00

- rename common package to database - move all code not related to database to new helper package - add more test in database package to improve code coverage - add a new (own) package for AMQP client
111 lines
2.6 KiB
Go
111 lines
2.6 KiB
Go
package simulationmodel
|
|
|
|
import (
|
|
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
|
|
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/scenario"
|
|
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulator"
|
|
)
|
|
|
|
type SimulationModel struct {
|
|
database.SimulationModel
|
|
}
|
|
|
|
func (m *SimulationModel) save() error {
|
|
db := database.GetDB()
|
|
err := db.Create(m).Error
|
|
return err
|
|
}
|
|
|
|
func (m *SimulationModel) ByID(id uint) error {
|
|
db := database.GetDB()
|
|
err := db.Find(m, id).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *SimulationModel) addToScenario() error {
|
|
db := database.GetDB()
|
|
var so scenario.Scenario
|
|
err := so.ByID(m.ScenarioID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// save simulation model to DB
|
|
err = m.save()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// associate simulator with simulation model
|
|
var simltr simulator.Simulator
|
|
err = simltr.ByID(m.SimulatorID)
|
|
err = db.Model(&simltr).Association("SimulationModels").Append(m).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// associate simulation model with scenario
|
|
err = db.Model(&so).Association("SimulationModels").Append(m).Error
|
|
|
|
return err
|
|
}
|
|
|
|
func (m *SimulationModel) Update(modifiedSimulationModel SimulationModel) error {
|
|
db := database.GetDB()
|
|
|
|
if m.SimulatorID != modifiedSimulationModel.SimulatorID {
|
|
// update simulator
|
|
var s simulator.Simulator
|
|
var s_old simulator.Simulator
|
|
err := s.ByID(modifiedSimulationModel.SimulatorID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = s_old.ByID(m.SimulatorID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// remove simulation model from old simulator
|
|
err = db.Model(&s_old).Association("SimulationModels").Delete(m).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// add simulation model to new simulator
|
|
err = db.Model(&s).Association("SimulationModels").Append(m).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if m.ScenarioID != modifiedSimulationModel.ScenarioID {
|
|
// TODO do we allow this case?
|
|
}
|
|
|
|
err := db.Model(m).Updates(map[string]interface{}{
|
|
"Name": modifiedSimulationModel.Name,
|
|
"StartParameters": modifiedSimulationModel.StartParameters,
|
|
"SimulatorID": modifiedSimulationModel.SimulatorID,
|
|
//"ScenarioID": modifiedSimulationModel.ScenarioID,
|
|
}).Error
|
|
|
|
return err
|
|
}
|
|
|
|
func (m *SimulationModel) delete() error {
|
|
|
|
db := database.GetDB()
|
|
var so scenario.Scenario
|
|
err := so.ByID(m.ScenarioID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// remove association between SimulationModel and Scenario
|
|
// SimulationModel itself is not deleted from DB, it remains as "dangling"
|
|
err = db.Model(&so).Association("SimulationModels").Delete(m).Error
|
|
|
|
return err
|
|
}
|