Exemple #1
0
        public static object RunHandler <TIn, TOut>(this NancyModule module, Func <TIn, TOut> handler)
        {
            try
            {
                TIn model;
                try
                {
                    model = module.BindAndValidate <TIn>();
                    if (!module.ModelValidationResult.IsValid)
                    {
                        return(module.Negotiate.RespondWithValidationFailure(module.ModelValidationResult));
                    }
                }
                catch (ModelBindingException)
                {
                    return(module.Negotiate.RespondWithValidationFailure(ModelBindingErrorMessage));
                }

                return(handler(model));
            }
            catch (Exception Ex)
            {
                return(module.Negotiate.RespondWithFailure(Ex));
            }
        }
Exemple #2
0
        public static async Task <object> RunHandlerAsync <TIn>(this NancyModule module, Func <TIn, Task <object> > handler)
        {
            try
            {
                TIn model;
                try
                {
                    model = module.BindAndValidate <TIn>();
                    if (!module.ModelValidationResult.IsValid)
                    {
                        return(module.Negotiate.RespondWithValidationFailure(module.ModelValidationResult));
                    }
                }
                catch (ModelBindingException)
                {
                    return(module.Negotiate.RespondWithValidationFailure(ModelBindingErrorMessage));
                }

                return(await handler(model).ConfigureAwait(false));
            }
            catch (Exception Ex)
            {
                return(module.Negotiate.RespondWithFailure(Ex));
            }
        }
        public static Response Command <T, TU>(this NancyModule module, Func <T, TU> commandHandler)
            where TU : Entity
            where T : ApiAction
        {
            var command = module.BindAndValidate <T>();

            return(module.Response.AsJson(commandHandler(command))
                   .WithContentType("application/vnd.siren+json; charset=utf-8"));
        }
        public static Response Query <T, TU>(this NancyModule module, Func <T, TU> requestHandler, int cachingInSeconds = 0)
            where TU : Entity
            where T : ApiLink
        {
            var request = module.BindAndValidate <T>();

            return(module.Response.AsJson(requestHandler(request))
                   .WithContentType("application/vnd.siren+json; charset=utf-8")
                   .WithHeader("Cache-Control", string.Format("Max-Age={0}", cachingInSeconds)));
        }
        public static T BindAndValidateModel <T>(this NancyModule module)
        {
            var result = module.BindAndValidate <T>();

            if (!module.ModelValidationResult.IsValid)
            {
                throw new BadModelException(module.ModelValidationResult);
            }

            return(result);
        }
Exemple #6
0
        public static async Task <Negotiator> EvaluateAndBind <TRequest>(this NancyModule module, Func <TRequest, Task <object> > success, HttpStatusCode onSuccessHttpCode = HttpStatusCode.OK) where TRequest : class
        {
            var request = module.BindAndValidate <TRequest>();

            if (!module.ModelValidationResult.IsValid)
            {
                var errors = MakeError(module.ModelValidationResult);

                throw new GenericBadRequest(errors);
            }

            var result = await success(request);

            return(await module.Negotiate.WithStatusCode(onSuccessHttpCode)
                   .WithModel(result));
        }