Ejemplo n.º 1
0
        public ApplicationResponse GetResponseAPI()
        {
            var result = new ApplicationResponse();

            result.AddErro(Mensagens);
            return(result);
        }
Ejemplo n.º 2
0
        public void ConfigureServices(IServiceCollection services)
        {
            var assembly = AppDomain.CurrentDomain.Load("Enquete.Domain");

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1).AddJsonOptions(op =>
            {
                op.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            });

            var connectionString = Configuration.GetConnectionString("DefaultConnection");

            services.AddDbContext <EnqueteContext>(op =>
                                                   op.UseSqlServer(connectionString)
                                                   );

            var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";

            services.AddSwagger(Configuration, xmlFile);

            //services.AddScoped<IdentityService>();
            services.AddMediatR(assembly);
            AddRepositories(services);

            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = (context) =>
                {
                    var result = new ApplicationResponse();

                    foreach (var propriedade in context.ModelState.Keys)
                    {
                        var errosPropriedade = context.ModelState[propriedade];

                        foreach (var erroPropriedadeItem in errosPropriedade.Errors)
                        {
                            result.AddErro(erroPropriedadeItem.ErrorMessage);
                        }
                    }

                    return(new UnprocessableEntityObjectResult(result));
                };
            });
        }