nitweb: catalog api serves tags related data
[nit.git] / src / web / web_base.nit
index d85b3f8..ac63f53 100644 (file)
@@ -47,7 +47,7 @@ class NitwebConfig
                view.include_fictive = true
                view.include_empty_doc = true
                view.include_attribute = true
-               view.include_test_suite = true
+               view.include_test = true
                return view
        end
 end
@@ -82,6 +82,46 @@ abstract class APIHandler
                end
                return mentity
        end
+
+       # Paginate a json array
+       #
+       # Returns only a subset of `results` depending on the current `page` and the
+       # number of elements to return set by `limit`.
+       #
+       # Transforms the json array into an object:
+       # ~~~json
+       # {
+       #       "page": 2,
+       #       "limit": 10,
+       #       "results: [ ... ],
+       #       "max": 5,
+       #       "total": 49
+       # }
+       # ~~~
+       fun paginate(results: JsonArray, count: Int, page, limit: nullable Int): JsonObject do
+               if page == null or page <= 0 then page = 1
+               if limit == null or limit <= 0 then limit = 20
+
+               var max = count / limit
+               if max == 0 then
+                       page = 1
+                       max = 1
+               else if page > max then
+                       page = max
+               end
+
+               var lstart = (page - 1) * limit
+               var lend = limit
+               if lstart + lend > count then lend = count - lstart
+
+               var res = new JsonObject
+               res["page"] = page
+               res["limit"] = limit
+               res["results"] = new JsonArray.from(results.subarray(lstart, lend))
+               res["max"] = max
+               res["total"] = count
+               return res
+       end
 end
 
 # A Rooter dedicated to APIHandlers.
@@ -207,6 +247,7 @@ redef class MEntityRef
                v.serialize_attribute("mdoc", mentity.mdoc_or_fallback)
                v.serialize_attribute("visibility", mentity.visibility.to_s)
                v.serialize_attribute("modifiers", mentity.collect_modifiers)
+               v.serialize_attribute("class_name", mentity.class_name)
                var mentity = self.mentity
                if mentity isa MMethod then
                        v.serialize_attribute("msignature", mentity.intro.msignature)