refactoring Model to SimulationModel

This commit is contained in:
Sonja Happ 2019-06-05 13:12:23 +02:00
parent a293b1d954
commit babe1cb308
10 changed files with 184 additions and 191 deletions

View file

@ -55,7 +55,7 @@ func VerifyConnection(db *gorm.DB) error {
func DropTables(db *gorm.DB) {
db.DropTableIfExists(&Simulator{})
//db.DropTableIfExists(&Signal{})
db.DropTableIfExists(&Model{})
db.DropTableIfExists(&SimulationModel{})
db.DropTableIfExists(&File{})
db.DropTableIfExists(&Simulation{})
db.DropTableIfExists(&User{})
@ -67,7 +67,7 @@ func DropTables(db *gorm.DB) {
func MigrateModels(db *gorm.DB) {
db.AutoMigrate(&Simulator{})
//db.AutoMigrate(&Signal{})
db.AutoMigrate(&Model{})
db.AutoMigrate(&SimulationModel{})
db.AutoMigrate(&File{})
db.AutoMigrate(&Simulation{})
db.AutoMigrate(&User{})
@ -110,8 +110,8 @@ func DummyPopulateDB(test_db *gorm.DB) {
//checkErr(test_db.Create(&inSig_A).Error)
//checkErr(test_db.Create(&inSig_B).Error)
mo_A := Model{Name: "Model_A"}
mo_B := Model{Name: "Model_B"}
mo_A := SimulationModel{Name: "SimulationModel_A"}
mo_B := SimulationModel{Name: "SimulationModel_B"}
checkErr(test_db.Create(&mo_A).Error)
checkErr(test_db.Create(&mo_B).Error)
@ -157,7 +157,7 @@ func DummyPopulateDB(test_db *gorm.DB) {
checkErr(test_db.Create(&widg_A).Error)
checkErr(test_db.Create(&widg_B).Error)
// Associations betweend models
// Associations between models
// For `belongs to` use the model with id=1
// For `has many` use the models with id=1 and id=2
@ -167,9 +167,9 @@ func DummyPopulateDB(test_db *gorm.DB) {
checkErr(test_db.Model(&simn_B).Association("Users").Append(&usr_A).Error)
checkErr(test_db.Model(&simn_B).Association("Users").Append(&usr_B).Error)
// Simulation HM Model
checkErr(test_db.Model(&simn_A).Association("Models").Append(&mo_A).Error)
checkErr(test_db.Model(&simn_A).Association("Models").Append(&mo_B).Error)
// Simulation HM SimulationModels
checkErr(test_db.Model(&simn_A).Association("SimulationModels").Append(&mo_A).Error)
checkErr(test_db.Model(&simn_A).Association("SimulationModels").Append(&mo_B).Error)
// Simulation HM Visualizations
checkErr(test_db.Model(&simn_A).Association("Visualizations").Append(&vis_A).Error)
@ -179,17 +179,17 @@ func DummyPopulateDB(test_db *gorm.DB) {
checkErr(test_db.Model(&vis_A).Association("Widgets").Append(&widg_A).Error)
checkErr(test_db.Model(&vis_A).Association("Widgets").Append(&widg_B).Error)
// Model HM Signal
// SimulationModel HM Signal
//checkErr(test_db.Model(&mo_A).Association("InputMapping").Append(&inSig_A).Error)
//checkErr(test_db.Model(&mo_A).Association("InputMapping").Append(&inSig_B).Error)
//checkErr(test_db.Model(&mo_A).Association("OutputMapping").Append(&outSig_A).Error)
//checkErr(test_db.Model(&mo_A).Association("OutputMapping").Append(&outSig_B).Error)
// Model HM Files
// SimulationModel HM Files
checkErr(test_db.Model(&mo_A).Association("Files").Append(&file_A).Error)
checkErr(test_db.Model(&mo_A).Association("Files").Append(&file_B).Error)
// Simulator BT Model
// Simulator BT SimulationModel
checkErr(test_db.Model(&mo_A).Association("Simulator").Append(&simr_A).Error)
// Widget HM Files

View file

@ -29,7 +29,7 @@ func TestDummyDBAssociations(t *testing.T) {
// Variables for tests
var simr Simulator
var mo Model
var mo SimulationModel
var file File
var simn Simulation
var usr User
@ -38,7 +38,7 @@ func TestDummyDBAssociations(t *testing.T) {
var widg Widget
//var sigs []Signal
var mos []Model
var mos []SimulationModel
var files []File
var files_sm []File
var simns []Simulation
@ -71,10 +71,10 @@ func TestDummyDBAssociations(t *testing.T) {
"Expected to have %v Users. Has %v.", 2, len(usrs))
}
a.NoError(db.Model(&simn).Related(&mos, "Models").Error)
a.NoError(db.Model(&simn).Related(&mos, "SimulationModels").Error)
if len(mos) != 2 {
a.Fail("Simulation Associations",
"Expected to have %v Models. Has %v.", 2, len(mos))
"Expected to have %v simulation models. Has %v.", 2, len(mos))
}
a.NoError(db.Model(&simn).Related(&viss, "Visualizations").Error)
@ -83,12 +83,12 @@ func TestDummyDBAssociations(t *testing.T) {
"Expected to have %v Visualizations. Has %v.", 2, len(viss))
}
// Model
// SimulationModel
a.NoError(db.Find(&mo, 1).Error, fM("Model"))
a.EqualValues("Model_A", mo.Name)
a.NoError(db.Find(&mo, 1).Error, fM("SimulationModel"))
a.EqualValues("SimulationModel_A", mo.Name)
// Model Associations
// SimulationModel Associations
a.NoError(db.Model(&mo).Association("Simulator").Find(&simr).Error)
a.EqualValues("Host_A", simr.Host, "Expected Host_A")
@ -101,7 +101,7 @@ func TestDummyDBAssociations(t *testing.T) {
a.NoError(db.Model(&mo).Related(&files_sm, "Files").Error)
if len(files_sm) != 2 {
a.Fail("Model Associations",
a.Fail("SimulationModel Associations",
"Expected to have %v Files. Has %v.", 2, len(files_sm))
}

View file

@ -32,36 +32,35 @@ type Simulation struct {
StartParameters string
// Users that have access to the simulation
Users []*User `gorm:"not null;many2many:user_simulations"`
// Models that belong to the simulation
Models []Model `gorm:"foreignkey:SimulationID"`
// SimulationModels that belong to the simulation
SimulationModels []SimulationModel `gorm:"foreignkey:SimulationID"`
// Visualizations that belong to the simulation
Visualizations []Visualization `gorm:"foreignkey:SimulationID"`
}
// Model data model
// TODO: rename to SimulationModel
type Model struct {
// ID of model
// SimulationModel data model
type SimulationModel struct {
// ID of simulation model
ID uint `gorm:"primary_key;auto_increment"`
// Name of model
// Name of simulation model
Name string `gorm:"not null"`
// Number of output signals
OutputLength int `gorm:"default:1"`
// Number of input signals
InputLength int `gorm:"default:1"`
// Start parameters of model as JSON string
// Start parameters of simulation model as JSON string
StartParameters string
// ID of simulation to which model belongs
// ID of simulation to which simulation model belongs
SimulationID uint
// Simulator associated with model
// Simulator associated with simulation model
Simulator Simulator
// ID of simulator associated with model
// ID of simulator associated with simulation model
SimulatorID uint
// Mapping of output signals of the model, order of signals is important
// Mapping of output signals of the simulation model, order of signals is important
OutputMapping []Signal
// Mapping of input signals of the model, order of signals is important
// Mapping of input signals of the simulation model, order of signals is important
InputMapping []Signal
// Files of model (can be CIM and other model file formats)
// Files of simulation model (can be CIM and other simulation model file formats)
Files []File `gorm:"foreignkey:ModelID"`
}

View file

@ -5,75 +5,75 @@ import (
)
type UserResponse struct {
Username string `json:"Username"`
Role string `json:"Role"`
Mail string `json:"Mail"`
Username string `json:"Username"`
Role string `json:"Role"`
Mail string `json:"Mail"`
}
type SimulationResponse struct {
Name string `json:"Name"`
ID uint `json:"SimulationID"`
Running bool `json:"Running"`
Name string `json:"Name"`
ID uint `json:"SimulationID"`
Running bool `json:"Running"`
StartParams string `json:"Starting Parameters"`
}
type ModelResponse struct {
Name string `json:"Name"`
OutputLength int `json:"OutputLength"`
InputLength int `json:"InputLength"`
SimulationID uint `json:"SimulationID"`
SimulatorID uint `json:"SimulatorID"`
StartParams string `json:"StartParams"`
InputMapping []Signal `json:"InputMapping"`
type SimulationModelResponse struct {
Name string `json:"Name"`
OutputLength int `json:"OutputLength"`
InputLength int `json:"InputLength"`
SimulationID uint `json:"SimulationID"`
SimulatorID uint `json:"SimulatorID"`
StartParams string `json:"StartParams"`
InputMapping []Signal `json:"InputMapping"`
OutputMapping []Signal `json:"OutputMapping"`
}
type SimulatorResponse struct {
UUID string `json:"UUID"`
Host string `json:"Host"`
ModelType string `json:"ModelType"`
Uptime int `json:"Uptime"`
State string `json:"State"`
StateUpdateAt time.Time `json:"StateUpdateAt"`
Properties string `json:"Properties"`
RawProperties string `json:"RawProperties"`
UUID string `json:"UUID"`
Host string `json:"Host"`
ModelType string `json:"ModelType"`
Uptime int `json:"Uptime"`
State string `json:"State"`
StateUpdateAt time.Time `json:"StateUpdateAt"`
Properties string `json:"Properties"`
RawProperties string `json:"RawProperties"`
}
type VisualizationResponse struct {
Name string `json:"Name"`
Grid int `json:"Grid"`
SimulationID uint `json:"SimulationID"`
Name string `json:"Name"`
Grid int `json:"Grid"`
SimulationID uint `json:"SimulationID"`
}
type WidgetResponse struct {
Name string `json:"Name"`
Type string `json:"Type"`
Width uint `json:"Width"`
Height uint `json:"Height"`
MinWidth uint `json:"MinWidth"`
MinHeight uint `json:"MinHeight"`
X int `json:"X"`
Y int `json:"Y"`
Z int `json:"Z"`
VisualizationID uint `json:"VisualizationID"`
IsLocked bool `json:"IsLocked"`
Name string `json:"Name"`
Type string `json:"Type"`
Width uint `json:"Width"`
Height uint `json:"Height"`
MinWidth uint `json:"MinWidth"`
MinHeight uint `json:"MinHeight"`
X int `json:"X"`
Y int `json:"Y"`
Z int `json:"Z"`
VisualizationID uint `json:"VisualizationID"`
IsLocked bool `json:"IsLocked"`
CustomProperties string `json:"CustomProperties"`
}
type FileResponse struct {
Name string `json:"Name"`
ID uint `json:"FileID"`
Path string `json:"Path"`
Type string `json:"Type"`
Size uint `json:"Size"`
H uint `json:"ImageHeight"`
W uint `json:"ImageWidth"`
Name string `json:"Name"`
ID uint `json:"FileID"`
Path string `json:"Path"`
Type string `json:"Type"`
Size uint `json:"Size"`
H uint `json:"ImageHeight"`
W uint `json:"ImageWidth"`
Date time.Time `json:"Date"`
}
// Response messages
type ResponseMsg struct{
type ResponseMsg struct {
Message string `json:"message"`
}
@ -92,4 +92,3 @@ type ResponseMsgSimulations struct {
type ResponseMsgSimulation struct {
Simulation SimulationResponse `json:"simulation"`
}

View file

@ -25,7 +25,6 @@ type UserSerializer struct {
User
}
func (self *UserSerializer) Response(assoc bool) UserResponse {
response := UserResponse{
@ -72,47 +71,45 @@ type SimulationSerializer struct {
Simulation
}
func (self *SimulationSerializer) Response() SimulationResponse {
response := SimulationResponse{
Name: self.Name,
ID: self.ID,
Running: self.Running,
Name: self.Name,
ID: self.ID,
Running: self.Running,
StartParams: self.StartParameters,
}
return response
}
// Model/s Serializers
type ModelsSerializer struct {
Ctx *gin.Context
Models []Model
type SimulationModelsSerializer struct {
Ctx *gin.Context
SimulationModels []SimulationModel
}
func (self *ModelsSerializer) Response() []ModelResponse {
response := []ModelResponse{}
for _, model := range self.Models {
serializer := ModelSerializer{self.Ctx, model}
func (self *SimulationModelsSerializer) Response() []SimulationModelResponse {
response := []SimulationModelResponse{}
for _, simulationmodel := range self.SimulationModels {
serializer := SimulationModelSerializer{self.Ctx, simulationmodel}
response = append(response, serializer.Response())
}
return response
}
type ModelSerializer struct {
type SimulationModelSerializer struct {
Ctx *gin.Context
Model
SimulationModel
}
func (self *ModelSerializer) Response() ModelResponse {
response := ModelResponse{
Name: self.Name,
OutputLength: self.OutputLength,
InputLength: self.InputLength,
func (self *SimulationModelSerializer) Response() SimulationModelResponse {
response := SimulationModelResponse{
Name: self.Name,
OutputLength: self.OutputLength,
InputLength: self.InputLength,
SimulationID: self.SimulationID,
SimulatorID: self.SimulatorID,
StartParams: self.StartParameters,
SimulatorID: self.SimulatorID,
StartParams: self.StartParameters,
//InputMapping
//OutputMapping
}
@ -122,7 +119,7 @@ func (self *ModelSerializer) Response() ModelResponse {
// Simulator/s Serializers
type SimulatorsSerializer struct {
Ctx *gin.Context
Ctx *gin.Context
Simulators []Simulator
}
@ -143,12 +140,12 @@ type SimulatorSerializer struct {
func (self *SimulatorSerializer) Response() SimulatorResponse {
response := SimulatorResponse{
UUID: self.UUID,
Host: self.Host,
ModelType: self.Modeltype,
Uptime: self.Uptime,
State: self.State,
StateUpdateAt: self.StateUpdateAt,
UUID: self.UUID,
Host: self.Host,
ModelType: self.Modeltype,
Uptime: self.Uptime,
State: self.State,
StateUpdateAt: self.StateUpdateAt,
}
return response
}
@ -156,7 +153,7 @@ func (self *SimulatorSerializer) Response() SimulatorResponse {
// Visualization/s Serializers
type VisualizationsSerializer struct {
Ctx *gin.Context
Ctx *gin.Context
Visualizations []Visualization
}
@ -174,12 +171,11 @@ type VisualizationSerializer struct {
Visualization
}
func (self *VisualizationSerializer) Response() VisualizationResponse {
response := VisualizationResponse{
Name: self.Name,
Grid: self.Grid,
Name: self.Name,
Grid: self.Grid,
SimulationID: self.SimulationID,
}
return response
@ -188,7 +184,7 @@ func (self *VisualizationSerializer) Response() VisualizationResponse {
// Widget/s Serializers
type WidgetsSerializer struct {
Ctx *gin.Context
Ctx *gin.Context
Widgets []Widget
}
@ -209,17 +205,17 @@ type WidgetSerializer struct {
func (self *WidgetSerializer) Response() WidgetResponse {
response := WidgetResponse{
Name: self.Name,
Type: self.Type,
Width: self.Width,
Height: self.Height,
MinWidth: self.MinWidth,
MinHeight: self.MinHeight,
X: self.X,
Y: self.Y,
Z: self.Z,
VisualizationID: self.VisualizationID,
IsLocked: self.IsLocked,
Name: self.Name,
Type: self.Type,
Width: self.Width,
Height: self.Height,
MinWidth: self.MinWidth,
MinHeight: self.MinHeight,
X: self.X,
Y: self.Y,
Z: self.Z,
VisualizationID: self.VisualizationID,
IsLocked: self.IsLocked,
//CustomProperties
}
return response
@ -246,7 +242,6 @@ type FileSerializerNoAssoc struct {
File
}
func (self *FileSerializerNoAssoc) Response() FileResponse {
response := FileResponse{
Name: self.Name,

View file

@ -11,7 +11,7 @@ import (
"github.com/gin-gonic/gin"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/model"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulationmodel"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/widget"
)
@ -62,7 +62,7 @@ func (f *File) register(fileHeader *multipart.FileHeader, objectType string, obj
f.Path = filepath.Join(getFolderName(objectType, objectID), f.Name)
f.Size = uint(fileHeader.Size)
var m model.Model
var m simulationmodel.SimulationModel
var w widget.Widget
var err error
if objectType == "model" {
@ -188,7 +188,7 @@ func (f *File) download(c *gin.Context) {
c.File(f.Path)
}
func (f *File) addToModel(model model.Model) error {
func (f *File) addToModel(model simulationmodel.SimulationModel) error {
db := common.GetDB()
err := db.Model(&model).Association("Files").Append(f).Error
return err

View file

@ -78,7 +78,7 @@ func getSimulations(c *gin.Context) {
// @Accept json
// @Produce json
// @Tags simulations
// @Param inputModel body common.ModelResponse true "Simulation to be added"
// @Param inputSimulation body common.SimulationResponse true "Simulation to be added"
// @Success 200 "OK."
// @Failure 401 "Unauthorized Access"
// @Failure 403 "Access forbidden."

View file

@ -1,4 +1,4 @@
package model
package simulationmodel
import (
"net/http"
@ -10,30 +10,30 @@ import (
)
func RegisterModelEndpoints(r *gin.RouterGroup) {
r.GET("/", getModels)
r.POST("/", addModel)
//r.POST("/:modelID", cloneModel)
r.PUT("/:modelID", updateModel)
r.GET("/:modelID", getModel)
r.DELETE("/:modelID", deleteModel)
r.GET("/", getSimulationModels)
r.POST("/", addSimulationModel)
//r.POST("/:modelID", cloneSimulationModel)
r.PUT("/:modelID", updateSimulationModel)
r.GET("/:modelID", getSimulationModel)
r.DELETE("/:modelID", deleteSimulationModel)
r.GET("/:modelID/signals", getSignals)
r.POST("/:modelID/signals", addSignal)
r.DELETE("/:modelID/signals", deleteSignals)
}
// getModels godoc
// @Summary Get all models of simulation
// @ID getModels
// getSimulationModels godoc
// @Summary Get all simulation models of simulation
// @ID getSimulationModels
// @Produce json
// @Tags models
// @Success 200 {array} common.ModelResponse "Array of models to which belong to simulation"
// @Success 200 {array} common.SimulationModelResponse "Array of models to which belong to simulation"
// @Failure 401 "Unauthorized Access"
// @Failure 403 "Access forbidden."
// @Failure 404 "Not found"
// @Failure 500 "Internal server error"
// @Param simulationID query int true "Simulation ID"
// @Router /models [get]
func getModels(c *gin.Context) {
func getSimulationModels(c *gin.Context) {
simID, err := common.GetSimulationID(c)
if err != nil {
@ -41,7 +41,7 @@ func getModels(c *gin.Context) {
}
db := common.GetDB()
var models []common.Model
var models []common.SimulationModel
var sim simulation.Simulation
err = sim.ByID(uint(simID))
@ -54,33 +54,33 @@ func getModels(c *gin.Context) {
return
}
serializer := common.ModelsSerializer{c, models}
serializer := common.SimulationModelsSerializer{c, models}
c.JSON(http.StatusOK, gin.H{
"models": serializer.Response(),
})
}
// addModel godoc
// @Summary Add a model to a simulation
// @ID addModel
// addSimulationModel godoc
// @Summary Add a simulation model to a simulation
// @ID addSimulationModel
// @Accept json
// @Produce json
// @Tags models
// @Param inputModel body common.ModelResponse true "Model to be added incl. IDs of simulation and simulator"
// @Param inputSimulationModel body common.SimulationModelResponse true "Simulation model to be added incl. IDs of simulation and simulator"
// @Success 200 "OK."
// @Failure 401 "Unauthorized Access"
// @Failure 403 "Access forbidden."
// @Failure 404 "Not found"
// @Failure 500 "Internal server error"
// @Router /models [post]
func addModel(c *gin.Context) {
func addSimulationModel(c *gin.Context) {
simID, err := common.GetSimulationID(c)
if err != nil {
return
}
var m Model
var m SimulationModel
err = c.BindJSON(&m)
if err != nil {
errormsg := "Bad request. Error binding form data to JSON: " + err.Error()
@ -98,7 +98,7 @@ func addModel(c *gin.Context) {
}
}
func cloneModel(c *gin.Context) {
func cloneSimulationModel(c *gin.Context) {
// modelID, err := routes.GetModelID(c)
// if err != nil {
@ -132,13 +132,13 @@ func cloneModel(c *gin.Context) {
}
// updateModel godoc
// @Summary Update a model
// @ID updateModel
// updateSimulationModel godoc
// @Summary Update a simulation model
// @ID updateSimulationModel
// @Tags models
// @Accept json
// @Produce json
// @Param inputModel body common.ModelResponse true "Model to be updated"
// @Param inputSimulationModel body common.SimulationModelResponse true "Simulation model to be updated"
// @Success 200 "OK."
// @Failure 401 "Unauthorized Access"
// @Failure 403 "Access forbidden."
@ -146,14 +146,14 @@ func cloneModel(c *gin.Context) {
// @Failure 500 "Internal server error"
// @Param modelID path int true "Model ID"
// @Router /models/{modelID} [put]
func updateModel(c *gin.Context) {
func updateSimulationModel(c *gin.Context) {
modelID, err := common.GetModelID(c)
if err != nil {
return
}
var modifiedModel Model
var modifiedModel SimulationModel
err = c.BindJSON(&modifiedModel)
if err != nil {
errormsg := "Bad request. Error binding form data to JSON: " + err.Error()
@ -163,7 +163,7 @@ func updateModel(c *gin.Context) {
return
}
var m Model
var m SimulationModel
err = m.ByID(uint(modelID))
if common.ProvideErrorResponse(c, err) {
return
@ -178,40 +178,40 @@ func updateModel(c *gin.Context) {
}
// getModel godoc
// @Summary Get a model
// @ID getModel
// getSimulationModel godoc
// @Summary Get a simulation model
// @ID getSimulationModel
// @Tags models
// @Produce json
// @Success 200 {object} common.ModelResponse "Requested model."
// @Success 200 {object} common.SimulationModelResponse "Requested simulation model."
// @Failure 401 "Unauthorized Access"
// @Failure 403 "Access forbidden."
// @Failure 404 "Not found"
// @Failure 500 "Internal server error"
// @Param modelID path int true "Model ID"
// @Router /models/{modelID} [get]
func getModel(c *gin.Context) {
func getSimulationModel(c *gin.Context) {
modelID, err := common.GetModelID(c)
if err != nil {
return
}
var m Model
var m SimulationModel
err = m.ByID(uint(modelID))
if common.ProvideErrorResponse(c, err) {
return
}
serializer := common.ModelSerializer{c, m.Model}
serializer := common.SimulationModelSerializer{c, m.SimulationModel}
c.JSON(http.StatusOK, gin.H{
"model": serializer.Response(),
})
}
// deleteModel godoc
// @Summary Delete a model
// @ID deleteModel
// deleteSimulationModel godoc
// @Summary Delete a simulation model
// @ID deleteSimulationModel
// @Tags models
// @Produce json
// @Success 200 "OK."
@ -221,7 +221,7 @@ func getModel(c *gin.Context) {
// @Failure 500 "Internal server error"
// @Param modelID path int true "Model ID"
// @Router /models/{modelID} [delete]
func deleteModel(c *gin.Context) {
func deleteSimulationModel(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "Not implemented.",
@ -249,7 +249,7 @@ func addSignal(c *gin.Context) {
return
}
var m Model
var m SimulationModel
err = m.ByID(uint(modelID))
if common.ProvideErrorResponse(c, err) {
return
@ -302,7 +302,7 @@ func getSignals(c *gin.Context) {
return
}
var m Model
var m SimulationModel
err = m.ByID(uint(modelID))
if common.ProvideErrorResponse(c, err) {
return
@ -348,7 +348,7 @@ func deleteSignals(c *gin.Context) {
return
}
var m Model
var m SimulationModel
err = m.ByID(uint(modelID))
if common.ProvideErrorResponse(c, err) {
return

View file

@ -1,4 +1,4 @@
package model
package simulationmodel
import (
"fmt"
@ -8,26 +8,26 @@ import (
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulator"
)
type Model struct {
common.Model
type SimulationModel struct {
common.SimulationModel
}
func (m *Model) save() error {
func (m *SimulationModel) save() error {
db := common.GetDB()
err := db.Create(m).Error
return err
}
func (m *Model) ByID(id uint) error {
func (m *SimulationModel) ByID(id uint) error {
db := common.GetDB()
err := db.Find(m, id).Error
if err != nil {
return fmt.Errorf("Model with id=%v does not exist", id)
return fmt.Errorf("Simulation Model with id=%v does not exist", id)
}
return nil
}
func (m *Model) addToSimulation(simID int) error {
func (m *SimulationModel) addToSimulation(simID int) error {
db := common.GetDB()
var sim simulation.Simulation
err := sim.ByID(uint(simID))
@ -35,34 +35,34 @@ func (m *Model) addToSimulation(simID int) error {
return err
}
// save model to DB
// save simulation model to DB
err = m.save()
if err != nil {
return err
}
// associate simulator with model
// associate simulator with simulation model
var simltr simulator.Simulator
err = simltr.ByID(m.SimulatorID)
err = db.Model(m).Association("Simulator").Append(&simltr).Error
// associate model with simulation
err = db.Model(&sim).Association("Models").Append(m).Error
// associate simulation model with simulation
err = db.Model(&sim).Association("SimulationModels").Append(m).Error
return err
}
func (m *Model) update(modifiedModel Model) error {
func (m *SimulationModel) update(modifiedSimulationModel SimulationModel) error {
db := common.GetDB()
err := db.Model(m).Update(modifiedModel).Error
err := db.Model(m).Update(modifiedSimulationModel).Error
if err != nil {
return err
}
if m.SimulatorID != modifiedModel.SimulatorID {
if m.SimulatorID != modifiedSimulationModel.SimulatorID {
// update simulator
var s simulator.Simulator
err = s.ByID(modifiedModel.SimulatorID)
err = s.ByID(modifiedSimulationModel.SimulatorID)
err = db.Model(m).Association("Simulator").Replace(s).Error
@ -71,7 +71,7 @@ func (m *Model) update(modifiedModel Model) error {
return err
}
func (m *Model) updateSignals(signals []common.Signal, direction string) error {
func (m *SimulationModel) updateSignals(signals []common.Signal, direction string) error {
db := common.GetDB()
var err error
@ -85,7 +85,7 @@ func (m *Model) updateSignals(signals []common.Signal, direction string) error {
return err
}
func (m *Model) addSignal(signal common.Signal, direction string) error {
func (m *SimulationModel) addSignal(signal common.Signal, direction string) error {
db := common.GetDB()
var err error
@ -109,7 +109,7 @@ func (m *Model) addSignal(signal common.Signal, direction string) error {
return err
}
func (m *Model) deleteSignals(direction string) error {
func (m *SimulationModel) deleteSignals(direction string) error {
db := common.GetDB()
var err error

View file

@ -8,8 +8,8 @@ import (
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
_ "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/doc/autoapi" // apidocs folder is generated by Swag CLI, you have to import it
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/model"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulation"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulationmodel"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulator"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/visualization"
@ -51,7 +51,7 @@ func main() {
api.Use(user.Authentication(true))
simulation.RegisterSimulationEndpoints(api.Group("/simulations"))
model.RegisterModelEndpoints(api.Group("/models"))
simulationmodel.RegisterModelEndpoints(api.Group("/models"))
visualization.RegisterVisualizationEndpoints(api.Group("/visualizations"))
widget.RegisterWidgetEndpoints(api.Group("/widgets"))
file.RegisterFileEndpoints(api.Group("/files"))