From 2e7475a26bd08c3371b0f5c28d71a459f0656547 Mon Sep 17 00:00:00 2001 From: Sonja Happ Date: Thu, 9 May 2019 17:02:24 +0200 Subject: [PATCH] - add first draft of code for other routes (not complete!) - some new endpoints - some new DB queries - some new serializers --- routes/file/fileEndpoints.go | 10 +- routes/file/fileQueries.go | 7 + routes/file/fileSerializer.go | 42 +++--- routes/project/projectEndpoints.go | 45 ++++++ routes/project/projectQueries.go | 27 ++++ routes/project/projectSerializer.go | 40 ++++++ routes/signal/signalEndpoints.go | 3 + routes/signal/signalQueries.go | 2 + routes/signal/signalSerializer.go | 2 + routes/simulation/simulationEndpoints.go | 46 ++++++ routes/simulation/simulationQueries.go | 18 +++ routes/simulation/simulationSerializer.go | 42 ++++++ .../simulationmodelEnpoints.go | 45 ++++++ .../simulationmodel/simulationmodelQueries.go | 11 ++ .../simulationmodelSerializer.go | 51 +++++++ routes/simulator/simulatorEndpoints.go | 52 +++++++ routes/simulator/simulatorQueries.go | 12 ++ routes/simulator/simulatorSerializer.go | 51 +++++++ routes/user/userEndpoints.go | 6 +- routes/user/userQueries.go | 21 --- routes/user/userSerializer.go | 135 ++---------------- .../visualization/visualizationEndpoints.go | 45 ++++++ routes/visualization/visualizationQueries.go | 11 ++ .../visualization/visualizationSerializer.go | 58 ++++++++ routes/widget/widgetEndpoints.go | 2 + routes/widget/widgetQueries.go | 13 ++ routes/widget/widgetSerializer.go | 59 ++++++++ start.go | 15 ++ 28 files changed, 698 insertions(+), 173 deletions(-) diff --git a/routes/file/fileEndpoints.go b/routes/file/fileEndpoints.go index 2628e76..51f45a2 100644 --- a/routes/file/fileEndpoints.go +++ b/routes/file/fileEndpoints.go @@ -8,16 +8,16 @@ import ( func FilesRegister(r *gin.RouterGroup) { r.GET("/", filesReadEp) //r.POST("/", fileRegistrationEp) // TODO to be added to API - //r.PUT("/:fileID", fileUpdateEp) // TODO to be added to API - r.GET("/:fileID", fileReadEp) - r.DELETE("/:fileID", fileDeleteEp) + //r.PUT("/:FileID", fileUpdateEp) // TODO to be added to API + r.GET("/:FileID", fileReadEp) + r.DELETE("/:FileID", fileDeleteEp) } func filesReadEp(c *gin.Context) { allFiles, _, _ := FindAllFiles() - serializer := FilesSerializer{c, allFiles} + serializer := FilesSerializerNoAssoc{c, allFiles} c.JSON(http.StatusOK, gin.H{ - "users": serializer.Response(), + "files": serializer.Response(), }) } diff --git a/routes/file/fileQueries.go b/routes/file/fileQueries.go index 44e7f17..6c977ed 100644 --- a/routes/file/fileQueries.go +++ b/routes/file/fileQueries.go @@ -10,3 +10,10 @@ func FindAllFiles() ([]common.File, int, error) { err := db.Find(&files).Error return files, len(files), err } + +func FindUserFiles(user *common.User) ([]common.File, int, error) { + db := common.GetDB() + var files []common.File + err := db.Model(user).Related(&files, "Files").Error + return files, len(files), err +} \ No newline at end of file diff --git a/routes/file/fileSerializer.go b/routes/file/fileSerializer.go index 754d8e5..16d85ae 100644 --- a/routes/file/fileSerializer.go +++ b/routes/file/fileSerializer.go @@ -1,53 +1,53 @@ package file import ( - "time" - "github.com/gin-gonic/gin" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" ) -type FilesSerializer struct { +// File/s Serializers + +type FilesSerializerNoAssoc struct { Ctx *gin.Context Files []common.File } -func (self *FilesSerializer) Response() []FileResponse { - response := []FileResponse{} - for _, File := range self.Files { - serializer := FileSerializer{self.Ctx, File} +func (self *FilesSerializerNoAssoc) Response() []FileResponseNoAssoc { + response := []FileResponseNoAssoc{} + for _, files := range self.Files { + serializer := FileSerializerNoAssoc{self.Ctx, files} response = append(response, serializer.Response()) } return response } -type FileSerializer struct { +type FileSerializerNoAssoc struct { Ctx *gin.Context common.File } -type FileResponse struct { +type FileResponseNoAssoc struct { Name string `json:"Name"` ID uint `json:"FileID"` Path string `json:"Path"` - Type string `json:"Type"` //MIME type? + Type string `json:"Type"` Size uint `json:"Size"` H uint `json:"ImageHeight"` W uint `json:"ImageWidth"` - Date time.Time `json:"Date"` + // Date } -func (self *FileSerializer) Response() FileResponse { - - response := FileResponse{ - Name: self.Name, - Path: self.Path, - Type: self.Type, - Size: self.Size, - Date: self.Date, - H: self.ImageHeight, +func (self *FileSerializerNoAssoc) Response() FileResponseNoAssoc { + response := FileResponseNoAssoc{ + Name: self.Name, + ID: self.ID, + Path: self.Path, + Type: self.Type, + Size: self.Size, + H: self.ImageHeight, W: self.ImageWidth, + // Date } return response -} +} \ No newline at end of file diff --git a/routes/project/projectEndpoints.go b/routes/project/projectEndpoints.go index 7dac6f1..a5fac47 100644 --- a/routes/project/projectEndpoints.go +++ b/routes/project/projectEndpoints.go @@ -1 +1,46 @@ package project + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +func ProjectsRegister(r *gin.RouterGroup) { + r.GET("/", projectsReadEp) + r.POST("/", projectRegistrationEp) + r.PUT("/:ProjectID", projectUpdateEp) + r.GET("/:ProjectID", projectReadEp) + r.DELETE("/:ProjectID", projectDeleteEp) +} + +func projectsReadEp(c *gin.Context) { + allProjects, _, _ := FindAllProjects() + serializer := ProjectsSerializerNoAssoc{c, allProjects} + c.JSON(http.StatusOK, gin.H{ + "projects": serializer.Response(), + }) +} + +func projectRegistrationEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func projectUpdateEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func projectReadEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func projectDeleteEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} diff --git a/routes/project/projectQueries.go b/routes/project/projectQueries.go index 7dac6f1..c42cb5b 100644 --- a/routes/project/projectQueries.go +++ b/routes/project/projectQueries.go @@ -1 +1,28 @@ package project + +import ( + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +func FindAllProjects() ([]common.Project, int, error) { + db := common.GetDB() + var projects []common.Project + err := db.Find(&projects).Error + return projects, len(projects), err +} + +func FindUserProjects(user *common.User) ([]common.Project, int, error) { + db := common.GetDB() + var projects []common.Project + err := db.Model(user).Related(&projects, "Projects").Error + return projects, len(projects), err +} + +func FindVisualizationProject(visualization *common.Visualization) (common.Project, int, error) { + db := common.GetDB() + var project common.Project + err := db.Model(visualization).Related(&project, "Projects").Error + return project, 1, err +} + + diff --git a/routes/project/projectSerializer.go b/routes/project/projectSerializer.go index 7dac6f1..753a79a 100644 --- a/routes/project/projectSerializer.go +++ b/routes/project/projectSerializer.go @@ -1 +1,41 @@ package project + +import ( + "github.com/gin-gonic/gin" + + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +type ProjectsSerializerNoAssoc struct { + Ctx *gin.Context + Projects []common.Project +} + +func (self *ProjectsSerializerNoAssoc) Response() []ProjectResponseNoAssoc { + response := []ProjectResponseNoAssoc{} + for _, project := range self.Projects { + serializer := ProjectSerializerNoAssoc{self.Ctx, project} + response = append(response, serializer.Response()) + } + return response +} + +type ProjectSerializerNoAssoc struct { + Ctx *gin.Context + common.Project +} + +type ProjectResponseNoAssoc struct { + Name string `json:"Name"` + ID uint `json:"ProjectID"` +} + +func (self *ProjectSerializerNoAssoc) Response() ProjectResponseNoAssoc { + response := ProjectResponseNoAssoc{ + Name: self.Name, + ID: self.ID, + } + return response +} + + diff --git a/routes/signal/signalEndpoints.go b/routes/signal/signalEndpoints.go index 5f45566..3fde498 100644 --- a/routes/signal/signalEndpoints.go +++ b/routes/signal/signalEndpoints.go @@ -1 +1,4 @@ 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 index 5f45566..65499f1 100644 --- a/routes/signal/signalQueries.go +++ b/routes/signal/signalQueries.go @@ -1 +1,3 @@ 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 index 5f45566..65499f1 100644 --- a/routes/signal/signalSerializer.go +++ b/routes/signal/signalSerializer.go @@ -1 +1,3 @@ package signal + +//TODO extend API with signal endpoints \ No newline at end of file diff --git a/routes/simulation/simulationEndpoints.go b/routes/simulation/simulationEndpoints.go index 02eddda..00a6c73 100644 --- a/routes/simulation/simulationEndpoints.go +++ b/routes/simulation/simulationEndpoints.go @@ -1 +1,47 @@ package simulation + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +func SimulationsRegister(r *gin.RouterGroup) { + r.GET("/", simulationsReadEp) + r.POST("/", simulationRegistrationEp) + r.PUT("/:SimulationID", simulationUpdateEp) + r.GET("/:SimulationID", simulationReadEp) + r.DELETE("/:SimulationID", simulationDeleteEp) +} + +func simulationsReadEp(c *gin.Context) { + allSimulations, _, _ := FindAllSimulations() + serializer := SimulationsSerializerNoAssoc{c, allSimulations} + c.JSON(http.StatusOK, gin.H{ + "simulations": serializer.Response(), + }) +} + +func simulationRegistrationEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func simulationUpdateEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func simulationReadEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func simulationDeleteEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + diff --git a/routes/simulation/simulationQueries.go b/routes/simulation/simulationQueries.go index 02eddda..58b88ce 100644 --- a/routes/simulation/simulationQueries.go +++ b/routes/simulation/simulationQueries.go @@ -1 +1,19 @@ package simulation + +import ( + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +func FindAllSimulations() ([]common.Simulation, int, error) { + db := common.GetDB() + var simulations []common.Simulation + err := db.Find(&simulations).Error + return simulations, len(simulations), err +} + +func FindUserSimulations(user *common.User) ([]common.Simulation, int, error) { + db := common.GetDB() + var simulations []common.Simulation + err := db.Model(user).Related(&simulations, "Simulations").Error + return simulations, len(simulations), err +} diff --git a/routes/simulation/simulationSerializer.go b/routes/simulation/simulationSerializer.go index 02eddda..b967b12 100644 --- a/routes/simulation/simulationSerializer.go +++ b/routes/simulation/simulationSerializer.go @@ -1 +1,43 @@ package simulation + +import ( + "github.com/gin-gonic/gin" + + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +type SimulationsSerializerNoAssoc struct { + Ctx *gin.Context + Simulations []common.Simulation +} + +func (self *SimulationsSerializerNoAssoc) Response() []SimulationResponseNoAssoc { + response := []SimulationResponseNoAssoc{} + for _, simulation := range self.Simulations { + serializer := SimulationSerializerNoAssoc{self.Ctx, simulation} + response = append(response, serializer.Response()) + } + return response +} + +type SimulationSerializerNoAssoc struct { + Ctx *gin.Context + common.Simulation +} + +type SimulationResponseNoAssoc struct { + Name string `json:"Name"` + ID uint `json:"SimulationID"` + Running bool `json:"Running"` + //StartParams postgres.Jsonb `json:"Starting Parameters"` +} + +func (self *SimulationSerializerNoAssoc) Response() SimulationResponseNoAssoc { + response := SimulationResponseNoAssoc{ + Name: self.Name, + ID: self.ID, + Running: self.Running, + //StartParams: self.StartParameters, + } + return response +} diff --git a/routes/simulationmodel/simulationmodelEnpoints.go b/routes/simulationmodel/simulationmodelEnpoints.go index 2820f09..1212ffb 100644 --- a/routes/simulationmodel/simulationmodelEnpoints.go +++ b/routes/simulationmodel/simulationmodelEnpoints.go @@ -1 +1,46 @@ package simulationmodel + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +func SimulationModelsRegister(r *gin.RouterGroup) { + r.GET("/", simulationmodelsReadEp) + r.POST("/", simulationmodelRegistrationEp) + r.PUT("/:SimulationModelID", simulationmodelUpdateEp) + r.GET("/:SimulationModelID", simulationmodelReadEp) + r.DELETE("/:SimulationModelID", simulationmodelDeleteEp) +} + +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", + }) +} diff --git a/routes/simulationmodel/simulationmodelQueries.go b/routes/simulationmodel/simulationmodelQueries.go index 2820f09..0eaf308 100644 --- a/routes/simulationmodel/simulationmodelQueries.go +++ b/routes/simulationmodel/simulationmodelQueries.go @@ -1 +1,12 @@ 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/routes/simulationmodel/simulationmodelSerializer.go b/routes/simulationmodel/simulationmodelSerializer.go index 2820f09..fdee9f7 100644 --- a/routes/simulationmodel/simulationmodelSerializer.go +++ b/routes/simulationmodel/simulationmodelSerializer.go @@ -1 +1,52 @@ package simulationmodel + +import ( + "github.com/gin-gonic/gin" + + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +type SimulationModelsSerializerNoAssoc struct { + Ctx *gin.Context + SimulationModels []common.SimulationModel +} + +func (self *SimulationModelsSerializerNoAssoc) Response() []SimulationModelResponseNoAssoc { + response := []SimulationModelResponseNoAssoc{} + for _, simulationmodel := range self.SimulationModels { + serializer := SimulationModelSerializerNoAssoc{self.Ctx, simulationmodel} + response = append(response, serializer.Response()) + } + return response +} + +type SimulationModelSerializerNoAssoc struct { + Ctx *gin.Context + common.SimulationModel +} + +type SimulationModelResponseNoAssoc struct { + Name string `json:"Name"` + OutputLength int `json:"OutputLength"` + InputLength int `json:"InputLength"` + BelongsToSimulationID uint `json:"BelongsToSimulationID"` + BelongsToSimulatorID uint `json:"BelongsToSimulatiorID"` + //StartParams postgres.Jsonb `json:"Starting Parameters"` + //Output Mapping + //Input Mapping +} + +func (self *SimulationModelSerializerNoAssoc) Response() SimulationModelResponseNoAssoc { + response := SimulationModelResponseNoAssoc{ + Name: self.Name, + OutputLength: self.OutputLength, + InputLength: self.InputLength, + BelongsToSimulationID: self.BelongsToSimulationID, + BelongsToSimulatorID: self.BelongsToSimulatorID, + //StartParams: self.StartParameters, + //InputMapping + //OutputMapping + } + return response +} + diff --git a/routes/simulator/simulatorEndpoints.go b/routes/simulator/simulatorEndpoints.go index 71a1702..d8ce7ca 100644 --- a/routes/simulator/simulatorEndpoints.go +++ b/routes/simulator/simulatorEndpoints.go @@ -1 +1,53 @@ package simulator + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +func SimulatorsRegister(r *gin.RouterGroup) { + r.GET("/", simulatorsReadEp) + r.POST("/", simulatorRegistrationEp) + r.PUT("/:SimulatorID", simulatorUpdateEp) + r.GET("/:SimulatorID", simulatorReadEp) + r.DELETE("/:SimulatorID", simulatorDeleteEp) + r.POST("/:SimulatorID", simulatorSendActionEp) +} + +func simulatorsReadEp(c *gin.Context) { + allSimulators, _, _ := FindAllSimulators() + serializer := SimulatorsSerializer{c, allSimulators} + c.JSON(http.StatusOK, gin.H{ + "simulators": serializer.Response(), + }) +} + +func simulatorRegistrationEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func simulatorUpdateEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func simulatorReadEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func simulatorDeleteEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func simulatorSendActionEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} \ No newline at end of file diff --git a/routes/simulator/simulatorQueries.go b/routes/simulator/simulatorQueries.go index 71a1702..92220d9 100644 --- a/routes/simulator/simulatorQueries.go +++ b/routes/simulator/simulatorQueries.go @@ -1 +1,13 @@ package simulator + +import ( + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +func FindAllSimulators() ([]common.Simulator, int, error) { + db := common.GetDB() + var simulators []common.Simulator + err := db.Find(&simulators).Error + return simulators, len(simulators), err +} + diff --git a/routes/simulator/simulatorSerializer.go b/routes/simulator/simulatorSerializer.go index 71a1702..7307238 100644 --- a/routes/simulator/simulatorSerializer.go +++ b/routes/simulator/simulatorSerializer.go @@ -1 +1,52 @@ package simulator + +import ( + "time" + + "github.com/gin-gonic/gin" + + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +type SimulatorsSerializer struct { + Ctx *gin.Context + Simulators []common.Simulator +} + +func (self *SimulatorsSerializer) Response() []SimulatorResponse { + response := []SimulatorResponse{} + for _, simulator := range self.Simulators { + serializer := SimulatorSerializer{self.Ctx, simulator} + response = append(response, serializer.Response()) + } + return response +} + +type SimulatorSerializer struct { + Ctx *gin.Context + common.Simulator +} + +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 + // Raw Properties +} + +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, + } + return response +} diff --git a/routes/user/userEndpoints.go b/routes/user/userEndpoints.go index 13fea2c..ab0a7b6 100644 --- a/routes/user/userEndpoints.go +++ b/routes/user/userEndpoints.go @@ -8,9 +8,9 @@ import ( func UsersRegister(r *gin.RouterGroup) { r.GET("/", usersReadEp) r.POST("/", userRegistrationEp) - r.PUT("/:userID", userUpdateEp) - r.GET("/:userID", userReadEp) - r.DELETE("/:userID", userDeleteEp) + r.PUT("/:UserID", userUpdateEp) + r.GET("/:UserID", userReadEp) + r.DELETE("/:UserID", userDeleteEp) //r.GET("/me", userSelfEp) // TODO: this conflicts with GET /:userID } diff --git a/routes/user/userQueries.go b/routes/user/userQueries.go index 031331d..4c45c94 100644 --- a/routes/user/userQueries.go +++ b/routes/user/userQueries.go @@ -10,24 +10,3 @@ func FindAllUsers() ([]common.User, int, error) { err := db.Find(&users).Error return users, len(users), err } - -func FindUserProjects(user *common.User) ([]common.Project, int, error) { - db := common.GetDB() - var projects []common.Project - err := db.Model(user).Related(&projects, "Projects").Error - return projects, len(projects), err -} - -func FindUserSimulations(user *common.User) ([]common.Simulation, int, error) { - db := common.GetDB() - var simulations []common.Simulation - err := db.Model(user).Related(&simulations, "Simulations").Error - return simulations, len(simulations), err -} - -func FindUserFiles(user *common.User) ([]common.File, int, error) { - db := common.GetDB() - var files []common.File - err := db.Model(user).Related(&files, "Files").Error - return files, len(files), err -} diff --git a/routes/user/userSerializer.go b/routes/user/userSerializer.go index 3dd2de8..31fac71 100644 --- a/routes/user/userSerializer.go +++ b/routes/user/userSerializer.go @@ -4,6 +4,9 @@ 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/project" + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulation" + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/file" ) type UsersSerializer struct { @@ -30,21 +33,21 @@ type UserResponse struct { Password string `json:"Password"` // XXX: ??? Role string `json:"Role"` Mail string `json:"Mail"` - Projects []ProjectResponseNoAssoc - Simulations []SimulationResponseNoAssoc - Files []FileResponseNoAssoc + Projects []project.ProjectResponseNoAssoc + Simulations []simulation.SimulationResponseNoAssoc + Files []file.FileResponseNoAssoc } func (self *UserSerializer) Response() UserResponse { // TODO: maybe all those should be made in one transaction - projects, _, _ := FindUserProjects(&self.User) - projectsSerializer := ProjectsSerializerNoAssoc{self.Ctx, projects} + projects, _, _ := project.FindUserProjects(&self.User) + projectsSerializer := project.ProjectsSerializerNoAssoc{self.Ctx, projects} - simulations, _, _ := FindUserSimulations(&self.User) - simulationsSerializer := SimulationsSerializerNoAssoc{self.Ctx, simulations} + simulations, _, _ := simulation.FindUserSimulations(&self.User) + simulationsSerializer := simulation.SimulationsSerializerNoAssoc{self.Ctx, simulations} - files, _, _ := FindUserFiles(&self.User) - filesSerializer := FilesSerializerNoAssoc{self.Ctx, files} + files, _, _ := file.FindUserFiles(&self.User) + filesSerializer := file.FilesSerializerNoAssoc{self.Ctx, files} response := UserResponse{ Username: self.Username, @@ -58,120 +61,6 @@ func (self *UserSerializer) Response() UserResponse { return response } -// Project/s Serializers -type ProjectsSerializerNoAssoc struct { - Ctx *gin.Context - Projects []common.Project -} -func (self *ProjectsSerializerNoAssoc) Response() []ProjectResponseNoAssoc { - response := []ProjectResponseNoAssoc{} - for _, project := range self.Projects { - serializer := ProjectSerializerNoAssoc{self.Ctx, project} - response = append(response, serializer.Response()) - } - return response -} -type ProjectSerializerNoAssoc struct { - Ctx *gin.Context - common.Project -} - -type ProjectResponseNoAssoc struct { - Name string `json:"Name"` - ID uint `json:"ProjectID"` -} - -func (self *ProjectSerializerNoAssoc) Response() ProjectResponseNoAssoc { - response := ProjectResponseNoAssoc{ - Name: self.Name, - ID: self.ID, - } - return response -} - -// Simulation/s Serializers - -type SimulationsSerializerNoAssoc struct { - Ctx *gin.Context - Simulations []common.Simulation -} - -func (self *SimulationsSerializerNoAssoc) Response() []SimulationResponseNoAssoc { - response := []SimulationResponseNoAssoc{} - for _, simulation := range self.Simulations { - serializer := SimulationSerializerNoAssoc{self.Ctx, simulation} - response = append(response, serializer.Response()) - } - return response -} - -type SimulationSerializerNoAssoc struct { - Ctx *gin.Context - common.Simulation -} - -type SimulationResponseNoAssoc struct { - Name string `json:"Name"` - ID uint `json:"SimulationID"` - Running bool `json:"Running"` - //StartParams postgres.Jsonb `json:"Starting Parameters"` -} - -func (self *SimulationSerializerNoAssoc) Response() SimulationResponseNoAssoc { - response := SimulationResponseNoAssoc{ - Name: self.Name, - ID: self.ID, - Running: self.Running, - //StartParams: self.StartParameters, - } - return response -} - -// File/s Serializers - -type FilesSerializerNoAssoc struct { - Ctx *gin.Context - Files []common.File -} - -func (self *FilesSerializerNoAssoc) Response() []FileResponseNoAssoc { - response := []FileResponseNoAssoc{} - for _, files := range self.Files { - serializer := FileSerializerNoAssoc{self.Ctx, files} - response = append(response, serializer.Response()) - } - return response -} - -type FileSerializerNoAssoc struct { - Ctx *gin.Context - common.File -} - -type FileResponseNoAssoc 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"` - // Date -} - -func (self *FileSerializerNoAssoc) Response() FileResponseNoAssoc { - response := FileResponseNoAssoc{ - Name: self.Name, - ID: self.ID, - Path: self.Path, - Type: self.Type, - Size: self.Size, - H: self.ImageHeight, - W: self.ImageWidth, - // Date - } - return response -} \ No newline at end of file diff --git a/routes/visualization/visualizationEndpoints.go b/routes/visualization/visualizationEndpoints.go index 27ba1fd..bfd5a52 100644 --- a/routes/visualization/visualizationEndpoints.go +++ b/routes/visualization/visualizationEndpoints.go @@ -1 +1,46 @@ package visualization + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +func VisualizationsRegister(r *gin.RouterGroup) { + r.GET("/", visualizationsReadEp) + r.POST("/", visualizationRegistrationEp) + r.PUT("/:VisualizationID", visualizationUpdateEp) + r.GET("/:VisualizationID", visualizationReadEp) + r.DELETE("/:VisualizationID", visualizationDeleteEp) +} + +func visualizationsReadEp(c *gin.Context) { + allVisualizations, _, _ := FindAllVisualizations() + serializer := VisualizationsSerializer{c, allVisualizations} + c.JSON(http.StatusOK, gin.H{ + "visualizations": serializer.Response(), + }) +} + +func visualizationRegistrationEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func visualizationUpdateEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func visualizationReadEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func visualizationDeleteEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} diff --git a/routes/visualization/visualizationQueries.go b/routes/visualization/visualizationQueries.go index 27ba1fd..a1f97db 100644 --- a/routes/visualization/visualizationQueries.go +++ b/routes/visualization/visualizationQueries.go @@ -1 +1,12 @@ package visualization + +import ( + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +func FindAllVisualizations() ([]common.Visualization, int, error) { + db := common.GetDB() + var visualization []common.Visualization + err := db.Find(&visualization).Error + return visualization, len(visualization), err +} diff --git a/routes/visualization/visualizationSerializer.go b/routes/visualization/visualizationSerializer.go index 27ba1fd..968b21e 100644 --- a/routes/visualization/visualizationSerializer.go +++ b/routes/visualization/visualizationSerializer.go @@ -1 +1,59 @@ package visualization + +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/project" + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/widget" +) + +type VisualizationsSerializer struct { + Ctx *gin.Context + Visualizations []common.Visualization +} + +func (self *VisualizationsSerializer) Response() []VisualizationResponse { + response := []VisualizationResponse{} + for _, visualization := range self.Visualizations { + serializer := VisualizationSerializer{self.Ctx, visualization} + response = append(response, serializer.Response()) + } + return response +} + +type VisualizationSerializer struct { + Ctx *gin.Context + common.Visualization +} + +type VisualizationResponse struct { + Name string `json:"Name"` + UserID uint `json:"UserID"` + Grid int `json:"Grid"` + ProjectID uint `json:"ProjectID"` + Project project.ProjectResponseNoAssoc + Widgets []widget.WidgetResponse +} + +func (self *VisualizationSerializer) Response() VisualizationResponse { + + // TODO: maybe all those should be made in one transaction + p, _, _ := project.FindVisualizationProject(&self.Visualization) + projectSerializer := project.ProjectSerializerNoAssoc{self.Ctx, p} + + w, _, _:= widget.FindVisualizationWidgets(&self.Visualization) + widgetsSerializer := widget.WidgetsSerializer{self.Ctx, w} + + + response := VisualizationResponse{ + Name: self.Name, + UserID: self.UserID, + Grid: self.Grid, + ProjectID: self.ProjectID, + Project: projectSerializer.Response(), + Widgets: widgetsSerializer.Response(), + } + return response +} + diff --git a/routes/widget/widgetEndpoints.go b/routes/widget/widgetEndpoints.go index 3c5635c..950ec4d 100644 --- a/routes/widget/widgetEndpoints.go +++ b/routes/widget/widgetEndpoints.go @@ -1 +1,3 @@ package widget + +// TODO add new API endpoints for widgets diff --git a/routes/widget/widgetQueries.go b/routes/widget/widgetQueries.go index 3c5635c..3d64294 100644 --- a/routes/widget/widgetQueries.go +++ b/routes/widget/widgetQueries.go @@ -1 +1,14 @@ package widget + +import ( + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +func FindVisualizationWidgets(visualization *common.Visualization) ([]common.Widget, int, error) { + db := common.GetDB() + var widgets []common.Widget + err := db.Model(visualization).Related(&widgets, "Widgets").Error + return widgets, len(widgets), err +} + + diff --git a/routes/widget/widgetSerializer.go b/routes/widget/widgetSerializer.go index 3c5635c..cb9a629 100644 --- a/routes/widget/widgetSerializer.go +++ b/routes/widget/widgetSerializer.go @@ -1 +1,60 @@ package widget + +import ( + "github.com/gin-gonic/gin" + + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +type WidgetsSerializer struct { + Ctx *gin.Context + Widgets []common.Widget +} + +func (self *WidgetsSerializer) Response() []WidgetResponse { + response := []WidgetResponse{} + for _, widget := range self.Widgets { + serializer := WidgetSerializer{self.Ctx, widget} + response = append(response, serializer.Response()) + } + return response +} + +type WidgetSerializer struct { + Ctx *gin.Context + common.Widget +} + +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"` + //CustomProperties +} + +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, + //CustomProperties + } + return response +} diff --git a/start.go b/start.go index b139e2b..b1e14d8 100644 --- a/start.go +++ b/start.go @@ -2,7 +2,13 @@ package main import ( "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" ) @@ -17,6 +23,15 @@ func main() { api := r.Group("/api") user.UsersRegister(api.Group("/users")) + file.FilesRegister(api.Group("/files")) + project.ProjectsRegister(api.Group("/projects")) + simulation.SimulationsRegister(api.Group("/simulations")) + simulationmodel.SimulationModelsRegister(api.Group("/models")) + simulator.SimulatorsRegister(api.Group("/simulators")) + visualization.VisualizationsRegister(api.Group("/visualizations")) + + + r.Run() }