From 1c696f5a38c1b58e5639a76f6b66be7f523d00fb Mon Sep 17 00:00:00 2001 From: Umputun Date: Sun, 7 Jan 2018 19:52:13 -0600 Subject: [PATCH] clean code duplication in find control --- app/rest/server.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/app/rest/server.go b/app/rest/server.go index b386c12f..ff5e723a 100644 --- a/app/rest/server.go +++ b/app/rest/server.go @@ -238,26 +238,19 @@ func (s *Server) findCommentsCtrl(w http.ResponseWriter, r *http.Request) { } comments = s.mod.maskBlockedUsers(comments) - if r.URL.Query().Get("format") == "tree" { - treeComments := format.MakeTree(comments, r.URL.Query().Get("sort")) - - b, err := encodeJSONWithHTML(treeComments) - if err != nil { - common.SendErrorJSON(w, r, http.StatusInternalServerError, err, "can't encode comments") - return - } - s.respCache.Set(cacheKey, b, time.Hour) - renderJSONFromBytes(w, r, b) - return + var bdata []byte + switch r.URL.Query().Get("format") { + case "tree": + bdata, err = encodeJSONWithHTML(format.MakeTree(comments, r.URL.Query().Get("sort"))) + default: + bdata, err = encodeJSONWithHTML(comments) } - - b, err := encodeJSONWithHTML(comments) if err != nil { common.SendErrorJSON(w, r, http.StatusInternalServerError, err, "can't encode comments") return } - s.respCache.Set(cacheKey, b, time.Hour) - renderJSONFromBytes(w, r, b) + s.respCache.Set(cacheKey, bdata, time.Hour) + renderJSONFromBytes(w, r, bdata) } // GET /last/{max}?site=siteID - last comments for the siteID, across all posts, sorted by time