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))
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.
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