private static CmsApiSummaryItemModel BuildValidPagesSummaryItemModel() { var model = new CmsApiSummaryItemModel() { Title = "an-article", Url = new Uri("/aaa/bbb", UriKind.Relative), Published = DateTime.Now, CreatedDate = DateTime.Now, }; return(model); }
public Task GetAndSaveItemAsync(CmsApiSummaryItemModel item, CancellationToken stoppingToken) { throw new NotImplementedException(); }
public async Task GetAndSaveItemAsync(CmsApiSummaryItemModel item, CancellationToken stoppingToken) { _ = item ?? throw new ArgumentNullException(nameof(item)); try { logger.LogInformation($"Get details for {item.Title} - {item.Url}"); var apiDataModel = await cmsApiService.GetItemAsync <CmsApiDataModel>(item.Url !).ConfigureAwait(false); if (apiDataModel == null) { logger.LogWarning($"No details returned from {item.Title} - {item.Url}"); return; } if (stoppingToken.IsCancellationRequested) { logger.LogWarning("Process item get and save cancelled"); return; } var contentPageModel = mapper.Map <ContentPageModel>(apiDataModel); if (!TryValidateModel(contentPageModel)) { logger.LogError($"Validation failure for {item.Title} - {item.Url}"); return; } logger.LogInformation($"Updating cache with {item.Title} - {item.Url}"); var result = await eventMessageService.UpdateAsync(contentPageModel).ConfigureAwait(false); if (result == HttpStatusCode.NotFound) { logger.LogInformation($"Does not exist, creating cache with {item.Title} - {item.Url}"); result = await eventMessageService.CreateAsync(contentPageModel).ConfigureAwait(false); if (result == HttpStatusCode.Created) { logger.LogInformation($"Created cache with {item.Title} - {item.Url}"); } else { logger.LogError($"Cache create error status {result} from {item.Title} - {item.Url}"); } } else { logger.LogInformation($"Updated cache with {item.Title} - {item.Url}"); } var contentItemIds = contentPageModel.AllContentItemIds; contentItemIds.AddRange(contentPageModel.AllPageLocationIds); contentCacheService.AddOrReplace(contentPageModel.Id, contentItemIds); } catch (Exception ex) { logger.LogError(ex, $"Error in get and save for {item.Title} - {item.Url}"); } }