Skip to content

Commit

Permalink
chore(http): cleans up the dashboardCellResponse marshaler
Browse files Browse the repository at this point in the history
  • Loading branch information
dearyhud committed Dec 2, 2019
1 parent 937bc9e commit 1a9fe28
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions http/dashboard_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,30 +207,24 @@ func newDashboardResponse(d *platform.Dashboard, labels []*platform.Label) dashb

type dashboardCellResponse struct {
platform.Cell
Links map[string]string `json:"links"`
Properties platform.ViewProperties `json:"-"`
Name string `json:"name,omitempty"`
Links map[string]string `json:"links"`
}

func (d *dashboardCellResponse) MarshalJSON() ([]byte, error) {
r := struct {
ID platform.ID `json:"id,omitempty"`
X int32 `json:"x"`
Y int32 `json:"y"`
W int32 `json:"w"`
H int32 `json:"h"`
Name string `json:"name,omitempty"`
ViewProperties platform.ViewProperties `json:"properties,omitempty"`
Links map[string]string `json:"links"`
platform.Cell
Properties platform.ViewProperties `json:"properties,omitempty"`
Name string `json:"name,omitempty"`
Links map[string]string `json:"links"`
}{
ID: d.Cell.ID,
X: d.Cell.X,
Y: d.Cell.Y,
W: d.Cell.W,
H: d.Cell.H,
Cell: d.Cell,
Links: d.Links,
}

if d.Cell.View != nil {
r.ViewProperties = d.Cell.View.Properties
r.Properties = d.Cell.View.Properties
r.Name = d.Cell.View.Name
}
return json.Marshal(r)
Expand All @@ -241,13 +235,19 @@ func (c dashboardCellResponse) toPlatform() *platform.Cell {
}

func newDashboardCellResponse(dashboardID platform.ID, c *platform.Cell) dashboardCellResponse {
return dashboardCellResponse{
resp := dashboardCellResponse{
Cell: *c,
Links: map[string]string{
"self": fmt.Sprintf("/api/v2/dashboards/%s/cells/%s", dashboardID, c.ID),
"view": fmt.Sprintf("/api/v2/dashboards/%s/cells/%s/view", dashboardID, c.ID),
},
}

if c.View != nil {
resp.Properties = c.View.Properties
resp.Name = c.View.Name
}
return resp
}

type dashboardCellsResponse struct {
Expand Down Expand Up @@ -498,15 +498,13 @@ func (h *DashboardHandler) handleGetDashboard(w http.ResponseWriter, r *http.Req
return
}

showViewProperties := r.URL.Query().Get("include") == "properties"

dashboard, err := h.DashboardService.FindDashboardByID(ctx, req.DashboardID)
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
}

if showViewProperties {
if r.URL.Query().Get("include") == "properties" {
for _, c := range dashboard.Cells {
view, err := h.DashboardService.GetDashboardCellView(ctx, dashboard.ID, c.ID)
if err != nil {
Expand Down

0 comments on commit 1a9fe28

Please sign in to comment.