diff --git a/common/database.go b/common/database.go index a67e7a5..1883d62 100644 --- a/common/database.go +++ b/common/database.go @@ -53,8 +53,8 @@ func VerifyConnection(db *gorm.DB) error { // to the Dummy*() where it is called func DropTables(db *gorm.DB) { db.DropTableIfExists(&Simulator{}) - db.DropTableIfExists(&Signal{}) - db.DropTableIfExists(&SimulationModel{}) + db.DropTableIfExists(&Sample{}) + db.DropTableIfExists(&Model{}) db.DropTableIfExists(&File{}) db.DropTableIfExists(&Project{}) db.DropTableIfExists(&Simulation{}) @@ -66,8 +66,8 @@ func DropTables(db *gorm.DB) { // AutoMigrate the models func MigrateModels(db *gorm.DB) { db.AutoMigrate(&Simulator{}) - db.AutoMigrate(&Signal{}) - db.AutoMigrate(&SimulationModel{}) + db.AutoMigrate(&Sample{}) + db.AutoMigrate(&Model{}) db.AutoMigrate(&File{}) db.AutoMigrate(&Project{}) db.AutoMigrate(&Simulation{}) @@ -102,19 +102,19 @@ func DummyPopulateDB(test_db *gorm.DB) { checkErr(test_db.Create(&simr_A).Error) checkErr(test_db.Create(&simr_B).Error) - outSig_A := Signal{Name: "outSignal_A"} - outSig_B := Signal{Name: "outSignal_B"} - inSig_A := Signal{Name: "inSignal_A"} - inSig_B := Signal{Name: "inSignal_B"} - checkErr(test_db.Create(&outSig_A).Error) - checkErr(test_db.Create(&outSig_B).Error) - checkErr(test_db.Create(&inSig_A).Error) - checkErr(test_db.Create(&inSig_B).Error) + outSmp_A := Sample{Name: "outSample_A"} + outSmp_B := Sample{Name: "outSample_B"} + inSmp_A := Sample{Name: "inSample_A"} + inSmp_B := Sample{Name: "inSample_B"} + checkErr(test_db.Create(&outSmp_A).Error) + checkErr(test_db.Create(&outSmp_B).Error) + checkErr(test_db.Create(&inSmp_A).Error) + checkErr(test_db.Create(&inSmp_B).Error) - smo_A := SimulationModel{Name: "SimModel_A"} - smo_B := SimulationModel{Name: "SimModel_B"} - checkErr(test_db.Create(&smo_A).Error) - checkErr(test_db.Create(&smo_B).Error) + mo_A := Model{Name: "Model_A"} + mo_B := Model{Name: "Model_B"} + checkErr(test_db.Create(&mo_A).Error) + checkErr(test_db.Create(&mo_B).Error) file_A := File{Name: "File_A"} file_B := File{Name: "File_B"} @@ -163,8 +163,8 @@ func DummyPopulateDB(test_db *gorm.DB) { checkErr(test_db.Model(&proj_B).Association("Simulation").Append(&simn_A).Error) // Simulation HM SimModel, SimModel BT Simulation - checkErr(test_db.Model(&smo_A).Association("BelongsToSimulation").Append(&simn_A).Error) - checkErr(test_db.Model(&smo_B).Association("BelongsToSimulation").Append(&simn_A).Error) + checkErr(test_db.Model(&mo_A).Association("BelongsToSimulation").Append(&simn_A).Error) + checkErr(test_db.Model(&mo_B).Association("BelongsToSimulation").Append(&simn_A).Error) // User HM Simulation, Simulation BT User checkErr(test_db.Model(&simn_A).Association("User").Append(&usr_A).Error) @@ -174,21 +174,21 @@ 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) - // SimModel HM Signal - checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_A).Error) - checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_B).Error) - checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_A).Error) - checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_B).Error) + // SimModel HM Samples + checkErr(test_db.Model(&mo_A).Association("InputMapping").Append(&inSmp_A).Error) + checkErr(test_db.Model(&mo_A).Association("InputMapping").Append(&inSmp_B).Error) + checkErr(test_db.Model(&mo_A).Association("OutputMapping").Append(&outSmp_A).Error) + checkErr(test_db.Model(&mo_A).Association("OutputMapping").Append(&outSmp_B).Error) - //SimulationModel HM Files - checkErr(test_db.Model(&smo_A).Association("Files").Append(&file_A).Error) - checkErr(test_db.Model(&smo_A).Association("Files").Append(&file_B).Error) + // Model 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) // Visualization BT User checkErr(test_db.Model(&vis_A).Association("User").Append(&usr_A).Error) // Simulator BT SimModel - checkErr(test_db.Model(&smo_A).Association("BelongsToSimulator").Append(&simr_A).Error) + checkErr(test_db.Model(&mo_A).Association("BelongsToSimulator").Append(&simr_A).Error) // Widget HM Files checkErr(test_db.Model(&widg_A).Association("Files").Append(&file_A).Error) diff --git a/common/database_test.go b/common/database_test.go index 4a1c284..949086e 100644 --- a/common/database_test.go +++ b/common/database_test.go @@ -28,7 +28,7 @@ func TestDummyDBAssociations(t *testing.T) { // Variables for tests var simr Simulator - var smo SimulationModel + var mo Model var file File var proj Project var simn Simulation @@ -36,8 +36,8 @@ func TestDummyDBAssociations(t *testing.T) { var vis Visualization var widg Widget - var sigs []Signal - var smos []SimulationModel + var smps []Sample + var mos []Model var files []File var files_sm []File var projs []Project @@ -45,28 +45,28 @@ func TestDummyDBAssociations(t *testing.T) { var viss []Visualization var widgs []Widget - // Simulation Model + // Model - a.NoError(db.Find(&smo, 1).Error, fM("SimulationModel")) - a.EqualValues("SimModel_A", smo.Name) + a.NoError(db.Find(&mo, 1).Error, fM("Model")) + a.EqualValues("SimModel_A", mo.Name) - // Simulation Model Associations + // Model Associations - a.NoError(db.Model(&smo).Association("BelongsToSimulation").Find(&simn).Error) + a.NoError(db.Model(&mo).Association("BelongsToSimulation").Find(&simn).Error) a.EqualValues("Simulation_A", simn.Name, "Expected Simulation_A") - a.NoError(db.Model(&smo).Association("BelongsToSimulator").Find(&simr).Error) + a.NoError(db.Model(&mo).Association("BelongsToSimulator").Find(&simr).Error) a.EqualValues("Host_A", simr.Host, "Expected Host_A") - a.NoError(db.Model(&smo).Related(&sigs, "OutputMapping").Error) - if len(sigs) != 4 { - a.Fail("Simulation Model Associations", - "Expected to have %v Output AND Input Signals. Has %v.", 4, len(sigs)) + a.NoError(db.Model(&mo).Related(&smps, "OutputMapping").Error) + if len(smps) != 4 { + a.Fail("Model Associations", + "Expected to have %v Output AND Input Samples. Has %v.", 4, len(smps)) } - a.NoError(db.Model(&smo).Related(&files_sm, "Files").Error) + a.NoError(db.Model(&mo).Related(&files_sm, "Files").Error) if len(files_sm) != 2 { - a.Fail("Simulation Model Associations", + a.Fail("Model Associations", "Expected to have %v Files. Has %v.", 2, len(files_sm)) } @@ -80,10 +80,10 @@ func TestDummyDBAssociations(t *testing.T) { a.NoError(db.Model(&simn).Association("User").Find(&usr).Error) a.EqualValues("User_A", usr.Username) - a.NoError(db.Model(&simn).Related(&smos, "Models").Error) - if len(smos) != 2 { + a.NoError(db.Model(&simn).Related(&mos, "Models").Error) + if len(mos) != 2 { a.Fail("Simulation Associations", - "Expected to have %v Simulation Models. Has %v.", 2, len(smos)) + "Expected to have %v Models. Has %v.", 2, len(mos)) } a.NoError(db.Model(&simn).Related(&projs, "Projects").Error) diff --git a/common/datastructs.go b/common/datastructs.go index cc029ab..ac4d8c6 100644 --- a/common/datastructs.go +++ b/common/datastructs.go @@ -1,9 +1,10 @@ package common import ( - //"github.com/jinzhu/gorm" - "github.com/jinzhu/gorm/dialects/postgres" "time" + + // "github.com/jinzhu/gorm" + "github.com/jinzhu/gorm/dialects/postgres" ) type Simulator struct { @@ -35,7 +36,7 @@ type File struct { //UserID uint `gorm:"not null"` //new in villasweb 2.0 - SimulationModelID uint `gorm:""` + ModelID uint `gorm:""` WidgetID uint `gorm:""` } @@ -63,11 +64,11 @@ type Simulation struct { User User `gorm:"not null;association_autoupdate:false"` UserID uint `gorm:"not null"` - Models []SimulationModel `gorm:"foreignkey:BelongsToSimulationID;association_autoupdate:false"` + Models []Model `gorm:"foreignkey:BelongsToSimulationID;association_autoupdate:false"` Projects []Project `gorm:"association_autoupdate:false"` } -type SimulationModel struct { +type Model struct { //gorm.Model ID uint `gorm:"primary_key;auto_increment"` Name string `gorm:"not null"` @@ -80,9 +81,9 @@ type SimulationModel struct { BelongsToSimulator Simulator `gorm:"not null;association_autoupdate:false"` BelongsToSimulatorID uint `gorm:"not null"` - // NOTE: order of signals is important - OutputMapping []Signal `gorm:""` - InputMapping []Signal `gorm:""` + // NOTE: order of samples is important + OutputMapping []Sample `gorm:""` + InputMapping []Sample `gorm:""` //new in villasweb 2.0 (for CIM file of simulation model and other model file formats) Files []File `gorm:""` @@ -119,12 +120,14 @@ type Visualization struct { Widgets []Widget `gorm:""` } -type Signal struct { +type Sample struct { //gorm.Model ID uint `gorm:"primary_key;auto_increment"` Name string `gorm:"not null"` Unit string `gorm:"not null"` - SimulationModelID uint + Index uint `gorm:"not null"` + Direction string `gorm:"not null"` + ModelID uint //IsRecorded bool `gorm:"default:false"` } diff --git a/doc/api/api.yaml b/doc/api/api.yaml index e0cd988..4cac0e6 100644 --- a/doc/api/api.yaml +++ b/doc/api/api.yaml @@ -20,8 +20,8 @@ tags: description: Manage Projects - name: simulations description: Manage Simulations -- name: simulationmodels - description: Manage SimulationModels +- name: models + description: Manage Models - name: visualizations description: Manage Visualizations - name: uploads @@ -502,9 +502,9 @@ paths: /models: get: tags: - - simulationmodels + - models summary: Get simulation models of user - operationId: getSimulationModels + operationId: getModels responses: 200: description: OK. @@ -513,7 +513,7 @@ paths: schema: type: array items: - $ref: '#/components/schemas/SimulationModel' + $ref: '#/components/schemas/Model' 401: description: Unauthorized access. 403: @@ -524,16 +524,16 @@ paths: description: Internal server error. post: tags: - - simulationmodels - summary: Add a new SimulationModel to the database - operationId: addSimulationModel + - models + summary: Add a new Model to the database + operationId: addModel requestBody: - description: "SimulationModel object to add to DB" + description: "Model object to add to DB" required: true content: application/json: schema: - $ref: '#/components/schemas/SimulationModel' + $ref: '#/components/schemas/Model' responses: 200: description: OK. @@ -545,26 +545,26 @@ paths: description: Access forbidden. 500: description: Internal server error. Unable to save simulation or simulation model. - /models/{SimulationModelID}: + /models/{ModelID}: put: tags: - - simulationmodels - summary: Update properties of SimulationModel with ID SimulationModelID - operationId: updateSimulationModelProperties + - models + summary: Update properties of Model with ID ModelID + operationId: updateModelProperties parameters: - in: path - name: SimulationModelID - description: ID of a SimulationModel + name: ModelID + description: ID of a Model required: true schema: type: integer requestBody: - description: "SimulationModel object with new properties" + description: "Model object with new properties" required: true content: application/json: schema: - $ref: '#/components/schemas/SimulationModel' + $ref: '#/components/schemas/Model' responses: 200: description: OK. @@ -580,13 +580,13 @@ paths: description: Internal server error. Unable to save simulation model. get: tags: - - simulationmodels - summary: Get properties of SimulationModel with ID SimulationModelID - operationId: getSimulationModelProperties + - models + summary: Get properties of Model with ID ModelID + operationId: getModelProperties parameters: - in: path - name: SimulationModelID - description: ID of a SimulationModel + name: ModelID + description: ID of a Model required: true schema: type: integer @@ -596,7 +596,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/SimulationModel' + $ref: '#/components/schemas/Model' 401: description: Unauthorized access. 403: @@ -607,13 +607,13 @@ paths: description: Internal server error. delete: tags: - - simulationmodels - summary: Delete SimulationModel with ID SimulationModelID - operationId: deleteSimulationModel + - models + summary: Delete Model with ID ModelID + operationId: deleteModel parameters: - in: path - name: SimulationModelID - description: ID of a SimulationModel + name: ModelID + description: ID of a Model required: true schema: type: integer @@ -628,16 +628,16 @@ paths: description: Not found. Simulation or simulation model not found. 500: description: Internal server error. Unable to save changed simulation or to remove simulation model. - /models/{SimulationModelID}/file: + /models/{ModelID}/file: get: tags: - - simulationmodels - summary: Get file from SimulationModel with ID SimulationModelID (NEW) - operationId: getFileFromSimulationModel + - models + summary: Get file from Model with ID ModelID (NEW) + operationId: getFileFromModel parameters: - in: path - name: SimulationModelID - description: ID of a SimulationModel + name: ModelID + description: ID of a Model required: true schema: type: integer @@ -680,13 +680,13 @@ paths: description: Internal server error. put: tags: - - simulationmodels - summary: Update (Overwrite) file of SimulationModel with ID SimulationModelID, File object has to be in database (NEW) - operationId: updateFileForSimulationModel + - models + summary: Update (Overwrite) file of Model with ID ModelID, File object has to be in database (NEW) + operationId: updateFileForModel parameters: - in: path - name: SimulationModelID - description: ID of a SimulationModel + name: ModelID + description: ID of a Model required: true schema: type: integer @@ -1269,7 +1269,7 @@ components: type: integer UserID: type: integer - SimulationModelID: + ModelID: type: integer Date: type: string @@ -1298,7 +1298,7 @@ components: RawProperties: type: object properties: {} - SimulationModel: + Model: required: - Name - OutputLength @@ -1323,23 +1323,23 @@ components: OutputMapping: type: array items: - $ref: '#/components/schemas/Signal' + $ref: '#/components/schemas/Sample' InputMapping: type: array items: - $ref: '#/components/schemas/Signal' - Signal: + $ref: '#/components/schemas/Sample' + Sample: required: - Name - Unit - - SimulationModelID + - ModelID type: object properties: Name: type: string Unit: type: string - SimulationModelID: + ModelID: type: integer Widget: required: diff --git a/routes/file/fileQueries.go b/routes/file/fileQueries.go index 92219b0..72fe117 100644 --- a/routes/file/fileQueries.go +++ b/routes/file/fileQueries.go @@ -52,7 +52,7 @@ func FindFileByPath(path string) (common.File, error) { return file, err } -func RegisterFile(c *gin.Context, widgetID int, simulationmodelID int, simulationID int){ +func RegisterFile(c *gin.Context, widgetID int, modelID int, simulationID int){ // Extract file from PUT request form file_header, err := c.FormFile("file") @@ -67,7 +67,7 @@ func RegisterFile(c *gin.Context, widgetID int, simulationmodelID int, simulatio // Obtain properties of file filetype := file_header.Header.Get("Content-Type") // TODO make sure this is properly set in file header filename := filepath.Base(file_header.Filename) - foldername := getFolderName(simulationID, simulationmodelID, widgetID) + foldername := getFolderName(simulationID, modelID, widgetID) size := file_header.Size // Save file to local disc (NOT DB!) @@ -81,11 +81,11 @@ func RegisterFile(c *gin.Context, widgetID int, simulationmodelID int, simulatio } // Add File object with parameters to DB - saveFileInDB(c, filename, foldername, filetype, uint(size), widgetID, simulationmodelID, true) + saveFileInDB(c, filename, foldername, filetype, uint(size), widgetID, modelID, true) } -func UpdateFile(c *gin.Context, widgetID int, simulationmodelID int, simulationID int){ +func UpdateFile(c *gin.Context, widgetID int, modelID int, simulationID int){ // Extract file from PUT request form file_header, err := c.FormFile("file") @@ -100,7 +100,7 @@ func UpdateFile(c *gin.Context, widgetID int, simulationmodelID int, simulationI filename := filepath.Base(file_header.Filename) filetype := file_header.Header.Get("Content-Type") // TODO make sure this is properly set in file header size := file_header.Size - foldername := getFolderName(simulationID, simulationmodelID, widgetID) + foldername := getFolderName(simulationID, modelID, widgetID) err = modifyFileOnDisc(file_header, filename, foldername, uint(size), false) if err != nil { @@ -111,10 +111,10 @@ func UpdateFile(c *gin.Context, widgetID int, simulationmodelID int, simulationI return } - saveFileInDB(c, filename, foldername, filetype, uint(size), widgetID, simulationmodelID, false) + saveFileInDB(c, filename, foldername, filetype, uint(size), widgetID, modelID, false) } -func ReadFile(c *gin.Context, widgetID int, simulationmodelID int, simulationID int){ +func ReadFile(c *gin.Context, widgetID int, modelID int, simulationID int){ contentType := c.GetHeader("Content-Type") @@ -123,7 +123,7 @@ func ReadFile(c *gin.Context, widgetID int, simulationmodelID int, simulationID if widgetID != -1 { // get associated Widget var wdgt common.Widget - err := db.First(&wdgt, simulationmodelID).Error + err := db.First(&wdgt, modelID).Error if common.ProvideErrorResponse(c, err) { return } @@ -132,11 +132,11 @@ func ReadFile(c *gin.Context, widgetID int, simulationmodelID int, simulationID return } - } else if simulationmodelID != -1 { + } else if modelID != -1 { // get associated Simulation Model - var model common.SimulationModel - err := db.First(&model, simulationmodelID).Error + var model common.Model + err := db.First(&model, modelID).Error if common.ProvideErrorResponse(c, err) { return } @@ -158,12 +158,12 @@ func ReadFile(c *gin.Context, widgetID int, simulationmodelID int, simulationID }) } -func DeleteFile(c *gin.Context, widgetID int, simulationmodelID int, simulationID int){ +func DeleteFile(c *gin.Context, widgetID int, nmodelID int, simulationID int){ // TODO } -func saveFileInDB(c *gin.Context, filename string, foldername string, filetype string, size uint, widgetID int, simulationmodelID int, createObj bool) { +func saveFileInDB(c *gin.Context, filename string, foldername string, filetype string, size uint, widgetID int, modelID int, createObj bool) { filesavepath := filepath.Join(foldername, filename) @@ -220,13 +220,13 @@ func saveFileInDB(c *gin.Context, filename string, foldername string, filetype s } } - if simulationmodelID != -1 { + if modelID != -1 { if createObj { // associate to Simulation Model db := common.GetDB() - var model common.SimulationModel - err := db.First(&model, simulationmodelID).Error + var model common.Model + err := db.First(&model, modelID).Error if common.ProvideErrorResponse(c, err) { return } @@ -310,16 +310,16 @@ func modifyFileOnDisc(file_header *multipart.FileHeader, filename string, folder } -func getFolderName(simulationID int, simulationmodelID int, widgetID int) string { +func getFolderName(simulationID int, modelID int, widgetID int) string { base_foldername := "files/" elementname := "" elementid := 0 - if simulationmodelID == -1{ + if modelID == -1{ elementname = "/widget_" elementid = widgetID } else { - elementname = "/simulationmodel_" - elementid = simulationmodelID + elementname = "/model_" + elementid = modelID } diff --git a/routes/model/modelEnpoints.go b/routes/model/modelEnpoints.go new file mode 100644 index 0000000..fee4298 --- /dev/null +++ b/routes/model/modelEnpoints.go @@ -0,0 +1,148 @@ +package model + +import ( + "fmt" + "net/http" + "strconv" + + "github.com/gin-gonic/gin" + + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/file" + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulation" +) + +func ModelsRegister(r *gin.RouterGroup) { + r.GET("/:SimulationID/models/", modelsReadEp) + r.POST("/:SimulationID/models/", modelRegistrationEp) + + r.PUT("/:SimulationID/models/:ModelID", modelUpdateEp) + r.GET("/:SimulationID/models/:ModelID", modelReadEp) + r.DELETE("/:SimulationID/models/:ModelID", modelDeleteEp) + + // Files + r.POST ("/:SimulationID/models/:ModelID/file", modelRegisterFileEp) // NEW in API + r.GET("/:SimulationID/models/:ModelID/file", modelReadFileEp) // NEW in API + r.PUT("/:SimulationID/models/:ModelID/file", modelUpdateFileEp) // NEW in API + r.DELETE("/:SimulationID/models/:ModelID/file", modelDeleteFileEp) // NEW in API + + // Simulator + r.PUT("/:SimulationID/models/:ModelID/simulator", modelUpdateSimulatorEp) // NEW in API + r.GET("/:SimulationID/models/:ModelID/simulator", modelReadSimulatorEp) // NEW in API + + // Input and Output Samples + r.POST("/:SimulationID/models/:ModelID/Samples/:Direction", modelRegisterSamplesEp) // NEW in API + r.GET("/:SimulationID/models/:ModelID/Samples/:Direction", modelReadSamplesEp) // NEW in API + r.PUT("/:SimulationID/models/:ModelID/Samples/:Direction", modelUpdateSamplesEp) // NEW in API + r.DELETE("/:SimulationID/models/:ModelID/Samples/:Direction", modelDeleteSamplesEp) // NEW in API +} + +func modelsReadEp(c *gin.Context) { + allModels, _, _ := FindAllModels() + serializer := ModelsSerializerNoAssoc{c, allModels} + c.JSON(http.StatusOK, gin.H{ + "models": serializer.Response(), + }) +} + +func modelRegistrationEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func modelUpdateEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func modelReadEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func modelDeleteEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + + +func modelRegisterFileEp(c *gin.Context) { + + simulationID, modelID, err := getRequestParams(c) + if err != nil{ + return + } + + // Save file locally and register file in DB, HTTP response is set by this method + file.RegisterFile(c,-1, modelID, simulationID) + +} + +func modelReadFileEp(c *gin.Context) { + + simulationID, modelID, err := getRequestParams(c) + if err != nil{ + return + } + + // Read file from disk and return in HTTP response, no change to DB + file.ReadFile(c, -1, modelID, simulationID) +} + +func modelUpdateFileEp(c *gin.Context) { + + simulationID, modelID, err := getRequestParams(c) + if err != nil{ + return + } + + // Update file locally and update file entry in DB, HTTP response is set by this method + file.UpdateFile(c,-1, modelID, simulationID) +} + +func modelDeleteFileEp(c *gin.Context) { + + simulationID, modelID, err := getRequestParams(c) + if err != nil{ + return + } + + // Delete file from disk and remove entry from DB, HTTP response is set by this method + file.DeleteFile(c, -1, modelID, simulationID) + + +} + + +func GetModelID(c *gin.Context) (int, error) { + + modelID, err := strconv.Atoi(c.Param("ModelID")) + + if err != nil { + errormsg := fmt.Sprintf("Bad request. No or incorrect format of simulation model ID") + c.JSON(http.StatusBadRequest, gin.H{ + "error": errormsg, + }) + return -1, err + } else { + return modelID, err + + } +} + +func getRequestParams(c *gin.Context) (int, int, error){ + simulationID, err := simulation.GetSimulationID(c) + if err != nil{ + return -1, -1, err + } + + modelID, err := GetModelID(c) + if err != nil{ + return -1, -1, err + } + + return simulationID, modelID, err +} \ No newline at end of file diff --git a/routes/model/modelQueries.go b/routes/model/modelQueries.go new file mode 100644 index 0000000..6e3a10c --- /dev/null +++ b/routes/model/modelQueries.go @@ -0,0 +1,12 @@ +package model + +import ( + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +func FindAllModels() ([]common.Model, int, error) { + db := common.GetDB() + var models []common.Model + err := db.Find(&models).Error + return models, len(models), err +} diff --git a/routes/simulationmodel/simulationmodelSerializer.go b/routes/model/modelSerializer.go similarity index 57% rename from routes/simulationmodel/simulationmodelSerializer.go rename to routes/model/modelSerializer.go index fdee9f7..6096a87 100644 --- a/routes/simulationmodel/simulationmodelSerializer.go +++ b/routes/model/modelSerializer.go @@ -1,4 +1,4 @@ -package simulationmodel +package model import ( "github.com/gin-gonic/gin" @@ -6,26 +6,26 @@ import ( "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" ) -type SimulationModelsSerializerNoAssoc struct { +type ModelsSerializerNoAssoc struct { Ctx *gin.Context - SimulationModels []common.SimulationModel + Models []common.Model } -func (self *SimulationModelsSerializerNoAssoc) Response() []SimulationModelResponseNoAssoc { - response := []SimulationModelResponseNoAssoc{} - for _, simulationmodel := range self.SimulationModels { - serializer := SimulationModelSerializerNoAssoc{self.Ctx, simulationmodel} +func (self *ModelsSerializerNoAssoc) Response() []ModelResponseNoAssoc { + response := []ModelResponseNoAssoc{} + for _, model := range self.Models { + serializer := ModelSerializerNoAssoc{self.Ctx, model} response = append(response, serializer.Response()) } return response } -type SimulationModelSerializerNoAssoc struct { +type ModelSerializerNoAssoc struct { Ctx *gin.Context - common.SimulationModel + common.Model } -type SimulationModelResponseNoAssoc struct { +type ModelResponseNoAssoc struct { Name string `json:"Name"` OutputLength int `json:"OutputLength"` InputLength int `json:"InputLength"` @@ -36,8 +36,8 @@ type SimulationModelResponseNoAssoc struct { //Input Mapping } -func (self *SimulationModelSerializerNoAssoc) Response() SimulationModelResponseNoAssoc { - response := SimulationModelResponseNoAssoc{ +func (self *ModelSerializerNoAssoc) Response() ModelResponseNoAssoc { + response := ModelResponseNoAssoc{ Name: self.Name, OutputLength: self.OutputLength, InputLength: self.InputLength, diff --git a/routes/sample/sampleEndpoints.go b/routes/sample/sampleEndpoints.go new file mode 100644 index 0000000..ca6c759 --- /dev/null +++ b/routes/sample/sampleEndpoints.go @@ -0,0 +1,4 @@ +package sample + + +//TODO extend API with sample endpoints \ No newline at end of file diff --git a/routes/sample/sampleQueries.go b/routes/sample/sampleQueries.go new file mode 100644 index 0000000..3f86c11 --- /dev/null +++ b/routes/sample/sampleQueries.go @@ -0,0 +1,3 @@ +package sample + +//TODO extend API with sample endpoints \ No newline at end of file diff --git a/routes/sample/sampleSerializer.go b/routes/sample/sampleSerializer.go new file mode 100644 index 0000000..3f86c11 --- /dev/null +++ b/routes/sample/sampleSerializer.go @@ -0,0 +1,3 @@ +package sample + +//TODO extend API with sample endpoints \ No newline at end of file diff --git a/routes/signal/signalEndpoints.go b/routes/signal/signalEndpoints.go deleted file mode 100644 index 3fde498..0000000 --- a/routes/signal/signalEndpoints.go +++ /dev/null @@ -1,4 +0,0 @@ -package signal - - -//TODO extend API with signal endpoints \ No newline at end of file diff --git a/routes/signal/signalQueries.go b/routes/signal/signalQueries.go deleted file mode 100644 index 65499f1..0000000 --- a/routes/signal/signalQueries.go +++ /dev/null @@ -1,3 +0,0 @@ -package signal - -//TODO extend API with signal endpoints \ No newline at end of file diff --git a/routes/signal/signalSerializer.go b/routes/signal/signalSerializer.go deleted file mode 100644 index 65499f1..0000000 --- a/routes/signal/signalSerializer.go +++ /dev/null @@ -1,3 +0,0 @@ -package signal - -//TODO extend API with signal endpoints \ No newline at end of file diff --git a/routes/simulationmodel/simulationmodelEnpoints.go b/routes/simulationmodel/simulationmodelEnpoints.go deleted file mode 100644 index 868de1e..0000000 --- a/routes/simulationmodel/simulationmodelEnpoints.go +++ /dev/null @@ -1,140 +0,0 @@ -package simulationmodel - -import ( - "fmt" - "net/http" - "strconv" - - "github.com/gin-gonic/gin" - - "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/file" - "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulation" -) - -func SimulationModelsRegister(r *gin.RouterGroup) { - r.GET("/:SimulationID/models/", simulationmodelsReadEp) - r.POST("/:SimulationID/models/", simulationmodelRegistrationEp) - - r.PUT("/:SimulationID/models/:SimulationModelID", simulationmodelUpdateEp) - r.GET("/:SimulationID/models/:SimulationModelID", simulationmodelReadEp) - r.DELETE("/:SimulationID/models/:SimulationModelID", simulationmodelDeleteEp) - - // Files - r.POST ("/:SimulationID/models/:SimulationModelID/file", simulationmodelRegisterFileEp) // NEW in API - r.GET("/:SimulationID/models/:SimulationModelID/file", simulationmodelReadFileEp) // NEW in API - r.PUT("/:SimulationID/models/:SimulationModelID/file", simulationmodelUpdateFileEp) // NEW in API - r.DELETE("/:SimulationID/models/:SimulationModelID/file", simulationmodelDeleteFileEp) // NEW in API - - -} - -func simulationmodelsReadEp(c *gin.Context) { - allSimulationModels, _, _ := FindAllSimulationModels() - serializer := SimulationModelsSerializerNoAssoc{c, allSimulationModels} - c.JSON(http.StatusOK, gin.H{ - "simulationmodels": serializer.Response(), - }) -} - -func simulationmodelRegistrationEp(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ - "message": "NOT implemented", - }) -} - -func simulationmodelUpdateEp(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ - "message": "NOT implemented", - }) -} - -func simulationmodelReadEp(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ - "message": "NOT implemented", - }) -} - -func simulationmodelDeleteEp(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ - "message": "NOT implemented", - }) -} - - -func simulationmodelRegisterFileEp(c *gin.Context) { - - simulationID, simulationmodelID, err := getRequestParams(c) - if err != nil{ - return - } - - // Save file locally and register file in DB, HTTP response is set by this method - file.RegisterFile(c,-1, simulationmodelID, simulationID) - -} - -func simulationmodelReadFileEp(c *gin.Context) { - - simulationID, simulationmodelID, err := getRequestParams(c) - if err != nil{ - return - } - - // Read file from disk and return in HTTP response, no change to DB - file.ReadFile(c, -1, simulationmodelID, simulationID) -} - -func simulationmodelUpdateFileEp(c *gin.Context) { - - simulationID, simulationmodelID, err := getRequestParams(c) - if err != nil{ - return - } - - // Update file locally and update file entry in DB, HTTP response is set by this method - file.UpdateFile(c,-1, simulationmodelID, simulationID) -} - -func simulationmodelDeleteFileEp(c *gin.Context) { - - simulationID, simulationmodelID, err := getRequestParams(c) - if err != nil{ - return - } - - // Delete file from disk and remove entry from DB, HTTP response is set by this method - file.DeleteFile(c, -1, simulationmodelID, simulationID) - - -} - - -func GetSimulationmodelID(c *gin.Context) (int, error) { - - simulationmodelID, err := strconv.Atoi(c.Param("SimulationModelID")) - - if err != nil { - errormsg := fmt.Sprintf("Bad request. No or incorrect format of simulation model ID") - c.JSON(http.StatusBadRequest, gin.H{ - "error": errormsg, - }) - return -1, err - } else { - return simulationmodelID, err - - } -} - -func getRequestParams(c *gin.Context) (int, int, error){ - simulationID, err := simulation.GetSimulationID(c) - if err != nil{ - return -1, -1, err - } - - simulationmodelID, err := GetSimulationmodelID(c) - if err != nil{ - return -1, -1, err - } - - return simulationID, simulationmodelID, err -} \ No newline at end of file diff --git a/routes/simulationmodel/simulationmodelQueries.go b/routes/simulationmodel/simulationmodelQueries.go deleted file mode 100644 index 0eaf308..0000000 --- a/routes/simulationmodel/simulationmodelQueries.go +++ /dev/null @@ -1,12 +0,0 @@ -package simulationmodel - -import ( - "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" -) - -func FindAllSimulationModels() ([]common.SimulationModel, int, error) { - db := common.GetDB() - var simulationmodels []common.SimulationModel - err := db.Find(&simulationmodels).Error - return simulationmodels, len(simulationmodels), err -} diff --git a/start.go b/start.go index b1e14d8..33eb69a 100644 --- a/start.go +++ b/start.go @@ -1,16 +1,12 @@ package main 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/file" - "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/project" "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" - - "github.com/gin-gonic/gin" ) func main() { @@ -23,12 +19,12 @@ func main() { api := r.Group("/api") user.UsersRegister(api.Group("/users")) - file.FilesRegister(api.Group("/files")) - project.ProjectsRegister(api.Group("/projects")) + //file.FilesRegister(api.Group("/files")) + //project.ProjectsRegister(api.Group("/projects")) simulation.SimulationsRegister(api.Group("/simulations")) - simulationmodel.SimulationModelsRegister(api.Group("/models")) + //model.ModelsRegister(api.Group("/models")) simulator.SimulatorsRegister(api.Group("/simulators")) - visualization.VisualizationsRegister(api.Group("/visualizations")) + //visualization.VisualizationsRegister(api.Group("/visualizations"))