Пример #1
0
        public static IServiceCollection AddApplicationServices(this IServiceCollection services)
        {
            services.AddScoped <IProductRepo, ProductRepo>();
            services.AddScoped(typeof(IGenericsRepo <>), (typeof(GenericsRepo <>)));
            services.AddScoped <IToken, Token>();
            services.AddScoped <IOrderRepo, OrderRepo>();
            services.AddScoped <IBasketRepo, BasketRepo>();

            //Error Validation
            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = actionContext =>
                {
                    var errors = actionContext.ModelState
                                 .Where(e => e.Value.Errors.Count > 0)
                                 .SelectMany(x => x.Value.Errors)
                                 .Select(x => x.ErrorMessage).ToArray();

                    var errorResponse = new ErrorValidation
                    {
                        Errors = errors
                    };

                    return(new BadRequestObjectResult(errorResponse));
                };
            });
            return(services);
        }
Пример #2
0
        public JsonResult EditarDocumento(DocumentoEdicaoViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var documento = Mapper.Map <Documento>(model);

                    business.EditarDocumento(documento);

                    var retorno = new DocumentoCadastroRetornoViewModel();

                    retorno.IdDocumento = model.IdDocumento;
                    retorno.Mensagem    = "Documento alterado com sucesso.";

                    return(Json(retorno));
                }
                catch (Exception e)
                {
                    return(Json($"Ocorreu um erro {e.Message}"));
                }
            }
            else
            {
                return(Json(ErrorValidation.GetValidationErrors(ModelState)));
            }
        }
        public string GetError(string name)
        {
            string ret = string.Empty;

            if (ErrorValidation.ContainsKey(name))
            {
                ret = "<span class=\"field-validation-error\">" + ErrorValidation[name] + "</span>";
            }

            return(ret);
        }
 public void AddError(string name, string message)
 {
     ErrorValidation.Add(name, message);
 }
Пример #5
0
 // This method gets called by the runtime. Use this method to add services to the container.
 public void ConfigureServices(IServiceCollection services)
 {
     services
     .AddControllers()
     .AddNewtonsoftJson(options =>
     {
         options.SerializerSettings.ContractResolver = new DefaultContractResolver
         {
             NamingStrategy = new SnakeCaseNamingStrategy()
         };
     })
     .ConfigureApiBehaviorOptions(options =>
     {
         options.InvalidModelStateResponseFactory = actionContext =>
         {
             ApiResponse apiResponseError = new ApiResponseValidationError(HttpStatusCode.BadRequest, ErrorValidation.Error(actionContext), "Validation Error");
             return(new ObjectResult(apiResponseError));
         };
     });
 }