Пример #1
0
        public async Task <ActionResult <RpGetCitizens> > GetCitizens([FromBody] RqGetCitizens rqGetCitizens)
        {
            try
            {
                if (!string.IsNullOrEmpty(rqGetCitizens.SearchRequest.Inn) && !ValidationsCollection.Validations.IsValidInnForIndividual(rqGetCitizens.SearchRequest.Inn))
                {
                    return(Json(ResponseHelper.ReturnBadRequest("Inn not valid")));
                }

                if (!string.IsNullOrEmpty(rqGetCitizens.SearchRequest.Snils) && !ValidationsCollection.Validations.IsValidSnils(StringConverter.GetNumbers(rqGetCitizens.SearchRequest.Snils)))
                {
                    return(Json(ResponseHelper.ReturnBadRequest("Snils not valid")));
                }

                var searchRequestParsed = new SearchRequestParsed();
                try
                {
                    searchRequestParsed = CitizenConverter.FromSearchRequestToSearchRequestParsed(rqGetCitizens.SearchRequest);
                }
                catch (Exception exception)
                {
                    LoggerStatic.Logger.Warn("Exception: " + exception);
                    return(Json(ResponseHelper.ReturnBadRequest(exception.Message)));
                }

                var rpGetCitizens = new RpGetCitizens
                {
                    Citizens = await _peopleService.GetCitizens(searchRequestParsed)
                };
                //return Json(rpGetCitizens);
                return(ControllersHelper.ReturnContentResult(SerializerJson.SerializeObjectToJsonString(rpGetCitizens)));
            }
            catch (Exception exception)
            {
                LoggerStatic.Logger.Error("Exception: " + exception);
                return(Json(ResponseHelper.ReturnInternalServerError(exception.Message)));
            }
        }
Пример #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var sqlConnectionString = Configuration.GetConnectionString("DefaultConnection");

            services.AddDbContext <PeopleDbContext>(
                options => options.UseSqlServer(sqlConnectionString), ServiceLifetime.Scoped
                );


            services.AddControllers()
            .AddJsonOptions(opt =>
            {
                opt.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
                opt.JsonSerializerOptions.WriteIndented          = true;
                opt.JsonSerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
                opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            });


            services.AddSingleton <IConfiguration>(Configuration);


            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version     = "v1",
                    Title       = "People API",
                    Description = "A simple example Web API",
                    Contact     = new OpenApiContact()
                    {
                        Name  = "Name",
                        Email = string.Empty,
                        //Url = "http://test.test"
                    }
                }
                                   );
            });

            services.ConfigureSwaggerGen(options =>
            {
                options.CustomSchemaIds(x => x.FullName);
            });

            services.RegisterServices(Configuration);

            services.Configure <ApiBehaviorOptions>(o =>
            {
                o.InvalidModelStateResponseFactory = actionContext =>
                {
                    var keyList = string.Join(",", actionContext.ModelState.Keys);

                    return(ControllersHelper.ReturnContentResult(SerializerJson.SerializeObjectToJsonString(ResponseHelper.ReturnBadRequest($"ModelState.Keys: {keyList}"))));
                };
            });

            services.AddLogging(builder =>
            {
                builder.AddConfiguration(Configuration.GetSection("Logging"))
                .AddConsole()
                .AddDebug();
            });

            services.Configure <WSettings>(Configuration);
        }