コード例 #1
0
        public async Task <IActionResult> BusinessInsert([FromBody] CmsPostWebHookApiModel model)
        {
            var(isValid, modelErrorMessage, postId) = IsModelValid(model);
            if (!isValid)
            {
                return(BadRequest(modelErrorMessage));
            }

            var webHookTypeName = HeaderType();

            if (string.IsNullOrEmpty(webHookTypeName) || webHookTypeName != "post_create")
            {
                return(BadRequest("No webhook name provided."));
            }

            var cityId = StringHelper.ReturnId(model.post_meta?.city?.FirstOrDefault());
            var region = await GetRegionFromId(cityId);

            var business_path = CmsVariable.SingleBusinessRegion ? "global_business" : region.Businesses_api_path;

            var cmsBusiness = await _cmsApiProxy.GetBusiness(postId, business_path);

            var elasticBusiness = await MapToElasticModel(cmsBusiness);

            var successful = await _businessRepository.Insert(new List <BusinessElasticModel> {
                elasticBusiness
            });

            if (!successful)
            {
                _logger.LogError("Failed to insert business with id:{PostId} in elasticsearch.", postId);
                return(BadRequest("Failed to insert business."));
            }

            _logger.LogInformation("Business with id {PostId} inserted to elasticsearch successfully.", postId);

            return(Created(elasticBusiness.Id.ToString(), elasticBusiness));
        }