VILLASweb-backend-go/routes/visualization/visualizationSerializer.go
Sonja Happ 2e7475a26b - add first draft of code for other routes (not complete!)
- some new endpoints
- some new DB queries
- some new serializers
2019-05-09 17:02:24 +02:00

59 lines
1.6 KiB
Go

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
}