From 7b1400720267163d366922e52346688551fc4f44 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Mon, 6 Jun 2016 07:46:04 -0400 Subject: [PATCH] nitweb: move APIHandler to web_base Signed-off-by: Alexandre Terrasa --- src/web/model_api.nit | 36 ------------------------------------ src/web/web_base.nit | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/web/model_api.nit b/src/web/model_api.nit index 9bbe5ad..ba01837 100644 --- a/src/web/model_api.nit +++ b/src/web/model_api.nit @@ -18,42 +18,6 @@ import web_base import highlight import uml -# Specific handler for nitweb API. -abstract class APIHandler - super ModelHandler - - # The JSON API does not filter anything by default. - # - # So we can cache the model view. - var view: ModelView is lazy do - var view = new ModelView(model) - view.min_visibility = private_visibility - view.include_fictive = true - view.include_empty_doc = true - view.include_attribute = true - view.include_test_suite = true - return view - end - - # Try to load the mentity from uri with `/:id`. - # - # Send 400 if `:id` is null. - # Send 404 if no entity is found. - # Return null in both cases. - fun mentity_from_uri(req: HttpRequest, res: HttpResponse): nullable MEntity do - var id = req.param("id") - if id == null then - res.error 400 - return null - end - var mentity = find_mentity(view, id) - if mentity == null then - res.error 404 - end - return mentity - end -end - # Group all api handlers in one router. class APIRouter super Router diff --git a/src/web/web_base.nit b/src/web/web_base.nit index ec9bf75..fcaac53 100644 --- a/src/web/web_base.nit +++ b/src/web/web_base.nit @@ -51,6 +51,42 @@ class ModelHandler end end +# Specific handler for nitweb API. +abstract class APIHandler + super ModelHandler + + # The JSON API does not filter anything by default. + # + # So we can cache the model view. + var view: ModelView is lazy do + var view = new ModelView(model) + view.min_visibility = private_visibility + view.include_fictive = true + view.include_empty_doc = true + view.include_attribute = true + view.include_test_suite = true + return view + end + + # Try to load the mentity from uri with `/:id`. + # + # Send 400 if `:id` is null. + # Send 404 if no entity is found. + # Return null in both cases. + fun mentity_from_uri(req: HttpRequest, res: HttpResponse): nullable MEntity do + var id = req.param("id") + if id == null then + res.error 400 + return null + end + var mentity = find_mentity(view, id) + if mentity == null then + res.error 404 + end + return mentity + end +end + # A NitView is rendered by an action. interface NitView # Renders this view and returns something that can be written to a HTTP response. -- 1.7.9.5