public async Task <IActionResult> PageUpdate([FromBody] CmsPostWebHookApiModel model)
        {
            if (model == null)
            {
                return(BadRequest("delete model empty or null."));
            }

            var postId = model.post_id;

            if (postId <= 0)
            {
                return(BadRequest($"post id null. model.post_id: {postId}"));
            }

            var webHookTypeName = HeaderType();

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

            var apiPath = model.post.post_type;

            _cmsApiProxy.RemovePageCache(postId);
            _cmsApiProxy.RemovePagesCache(apiPath);

            await _cmsApiProxy.GetPage(postId, apiPath);

            await _cmsApiProxy.GetPages(regionPageApiPath : apiPath);

            _logger.LogInformation("Cleared and renewed cache for page {PageId} for page api path {ApiPath}.", postId, apiPath);

            return(Ok());
        }
        public async Task <IActionResult> TranslationUpdate([FromBody] CmsPostWebHookApiModel model)
        {
            _cmsApiProxy.RemoveTranslationsCache();
            var translations = await _cmsApiProxy.GetTranslations();

            _logger.LogInformation("Cleared and renewed cache for {Count} translations.", translations.Count);

            return(Ok());
        }
        public async Task <IActionResult> BusinessUpdate([FromBody] CmsPostWebHookApiModel model)
        {
            var(isValid, modelErrorMessage, postId) = IsModelValid(model);
            if (!isValid)
            {
                return(BadRequest(modelErrorMessage));
            }

            var webHookTypeName = HeaderType();

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

            // if "trashed" remove from elastic and cache
            if (model.post.post_status == "trash")
            {
                var removeSuccessful = await _businessRepository.Delete(postId);

                if (!removeSuccessful)
                {
                    _logger.LogError("Failed to delete business with id: {PostId} in elasticsearch.", postId);
                    return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to delete business."));
                }
                return(NoContent());
            }

            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.Update(elasticBusiness);

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

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

            return(Ok());
        }
        private (bool valid, string errorMessage, int postId) IsModelValid(CmsPostWebHookApiModel apiModel)
        {
            if (apiModel == null)
            {
                _logger.LogWarning("Api model is null.");
                return(false, "", -1);
            }

            if (apiModel.post_id <= 0)
            {
                _logger.LogWarning("Post id {PostId} null when validating model.", apiModel.post_id);
                return(false, "post id null.", -1);
            }

            if (string.IsNullOrEmpty(apiModel.post?.post_type))
            {
                _logger.LogWarning("post type {PostType} empty or null.", apiModel.post?.post_type);
                return(false, "post type empty or null.", -1);
            }

            return(true, "", apiModel.post_id);
        }
        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));
        }
        public async Task <IActionResult> RegionUpdate([FromBody] CmsPostWebHookApiModel model)
        {
            if (model == null)
            {
                return(BadRequest("delete model empty or null."));
            }

            var postId = model.post_id;

            if (postId <= 0)
            {
                return(BadRequest($"post id null. model.post_id: {postId}"));
            }

            if (model.post.post_status == "trash")
            {
                var removeSuccessful = await _regionRepository.Delete(postId);

                if (!removeSuccessful)
                {
                    _logger.LogError("Failed to delete region with id: {PostId} in elasticsearch.", postId);
                    return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to delete region."));
                }
                return(NoContent());
            }

            var cmsRegion = await _cmsApiProxy.GetRegion(postId);

            var elasticRegion = await MapToElasticModel(cmsRegion);

            bool successful;
            var  region = await _regionRepository.Get(postId);

            if (region == null)
            {
                successful = await _regionRepository.Insert(new List <RegionElasticModel> {
                    elasticRegion
                });

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

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

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

            successful = await _regionRepository.Update(elasticRegion);

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

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

            // TODO: FIX THIS!!!!!!
            // Update business cache!
            await SyncBusiness();

            // Update pages cache!
            var apiPath = model.post.post_type;

            _cmsApiProxy.RemovePagesCache(apiPath);
            await _cmsApiProxy.GetPages(regionPageApiPath : apiPath);

            return(Ok());
        }
        public async Task <IActionResult> TagsUpdate([FromBody] CmsPostWebHookApiModel model)
        {
            if (model == null)
            {
                return(BadRequest("delete model empty or null."));
            }

            var postId = model.post_id;

            if (postId <= 0)
            {
                return(BadRequest($"post id null. model.post_id: {postId}"));
            }

            if (model.post.post_status == "trash")
            {
                var removeSuccessful = await _tagRepository.Delete(postId);

                if (!removeSuccessful)
                {
                    _logger.LogError("Failed to delete tag with id: {PostId} in elasticsearch.", postId);
                    return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to delete tag."));
                }
                return(NoContent());
            }

            var cmsTag = await _cmsApiProxy.GetTag(postId);

            var elasticTag = await MapToElasticModel(cmsTag);

            bool successful;
            var  tag = await _tagRepository.Get(postId);

            if (tag == null)
            {
                successful = await _tagRepository.Insert(new List <TagElasticModel> {
                    elasticTag
                });

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

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

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

            successful = await _tagRepository.Update(elasticTag);

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

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

            return(Ok());
        }