nitweb: migrate /search to /api/search
authorAlexandre Terrasa <alexandre@moz-code.org>
Wed, 25 May 2016 03:21:34 +0000 (23:21 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Mon, 30 May 2016 15:03:32 +0000 (11:03 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/nitweb.nit
src/web/model_api.nit
src/web/web_actions.nit

index 9b6aef7..96443d4 100644 (file)
@@ -53,7 +53,6 @@ private class NitwebPhase
                app.use("/random", new RandomAction(model))
                app.use("/doc/:namespace", new DocAction(model, mainmodule, modelbuilder))
                app.use("/code/:namespace", new CodeAction(model, modelbuilder))
-               app.use("/search/:namespace", new SearchAction(model))
                app.use("/uml/:namespace", new UMLDiagramAction(model, mainmodule))
                app.use("/", new TreeAction(model, mainmodule))
 
index 3974cf7..37213d8 100644 (file)
@@ -68,8 +68,30 @@ class APIRouter
 
        init do
                use("/list", new APIList(model, mainmodule))
+               use("/search", new APISearch(model, mainmodule))
        end
 end
+
+# Search mentities from a query string.
+#
+# Example: `GET /search?q=Arr`
+class APISearch
+       super APIHandler
+
+       redef fun get(req, res) do
+               var q = req.string_arg("q")
+               if q == null then
+                       res.error 400
+                       return
+               end
+               var arr = new JsonArray
+               for mentity in view.mentities do
+                       if mentity.name.has_prefix(q) then arr.add mentity
+               end
+               res.json arr
+       end
+end
+
 # List all mentities.
 #
 # MEntities can be filtered on their kind using the `k` parameter.
index c27301d..f1dee04 100644 (file)
@@ -29,28 +29,6 @@ class TreeAction
        end
 end
 
-# Display the list of mentities matching `namespace`.
-class SearchAction
-       super ModelHandler
-
-       # TODO handle more than full namespaces.
-       redef fun get(req, res) do
-               var namespace = req.param("namespace")
-               var model = init_model_view(req)
-               var mentity = find_mentity(model, namespace)
-               if mentity == null then
-                       res.error(404)
-                       return
-               end
-               if req.is_json_asked then
-                       res.json(mentity.to_json)
-                       return
-               end
-               var view = new HtmlResultPage(namespace or else "null", [mentity])
-               res.send_view(view)
-       end
-end
-
 # Display a MEntity source code.
 class CodeAction
        super ModelHandler