From 4b7d4cf872b33fc6ec9b777f3d073129f4155eef Mon Sep 17 00:00:00 2001 From: Harvey Kandola Date: Thu, 28 Feb 2019 12:42:42 +0000 Subject: [PATCH] Ensure non-negative space summary counts Sometimes users direct manipulate database and so space level counts don't match. We ensure that counts don't go negative. --- domain/space/endpoint.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/domain/space/endpoint.go b/domain/space/endpoint.go index 7caa5719..63330e83 100644 --- a/domain/space/endpoint.go +++ b/domain/space/endpoint.go @@ -429,6 +429,14 @@ func (h *Handler) Get(w http.ResponseWriter, r *http.Request) { return } + // Cater for negative counts caused by user database manipulation. + if sp.CountCategory < 0 { + sp.CountCategory = 0 + } + if sp.CountContent < 0 { + sp.CountContent = 0 + } + ctx.Transaction, err = h.Runtime.Db.Beginx() if err != nil { response.WriteServerError(w, method, err) @@ -461,6 +469,16 @@ func (h *Handler) GetViewable(w http.ResponseWriter, r *http.Request) { h.Runtime.Log.Error(method, err) } + // Cater for negative counts caused by user database manipulation. + for i := range sp { + if sp[i].CountCategory < 0 { + sp[i].CountCategory = 0 + } + if sp[i].CountContent < 0 { + sp[i].CountContent = 0 + } + } + response.WriteJSON(w, sp) }