public async Task <DeleteSiteDisclaimerCommandResponse> Handle(DeleteSiteDisclaimerCommand request, CancellationToken cancellationToken) { var response = new DeleteSiteDisclaimerCommandResponse() { IsSuccessful = false }; var siteDisclaimerIds = request.SiteDisclaimerIds.Distinct().ToList(); var siteDisclaimers = await _siteDisclaimerRepository.GetSiteDisclaimerByIds(siteDisclaimerIds); if (siteDisclaimers.Count != siteDisclaimerIds.Count) { throw new RulesException("Invalid", @"SiteDisclaimer not found"); } using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { foreach (var siteDisclaimer in siteDisclaimers) { foreach (var sitedisclaimerContent in siteDisclaimer.ArticleContents.ToList()) { siteDisclaimer.ArticleContents.Remove(sitedisclaimerContent); _siteDisclaimerRepository.Delete(sitedisclaimerContent); } foreach (var country in siteDisclaimer.ArticleRelatedCountries.ToList()) { siteDisclaimer.ArticleRelatedCountries.Remove(country); _siteDisclaimerRepository.Delete(country); } foreach (var countryGroup in siteDisclaimer.ArticleRelatedCountryGroups.ToList()) { siteDisclaimer.ArticleRelatedCountryGroups.Remove(countryGroup); _siteDisclaimerRepository.Delete(countryGroup); } foreach (var taxTag in siteDisclaimer.ArticleRelatedTaxTags.ToList()) { siteDisclaimer.ArticleRelatedTaxTags.Remove(taxTag); _siteDisclaimerRepository.Delete(taxTag); } foreach (var relatedArticle in siteDisclaimer.RelatedArticlesArticle.ToList()) { siteDisclaimer.RelatedArticlesArticle.Remove(relatedArticle); _siteDisclaimerRepository.Delete(relatedArticle); } //Remove reverse relation _siteDisclaimerRepository.DeleteRelatedArticles(siteDisclaimer.ArticleId); foreach (var relatedResource in siteDisclaimer.RelatedResourcesArticle.ToList()) { siteDisclaimer.RelatedResourcesArticle.Remove(relatedResource); _siteDisclaimerRepository.Delete(relatedResource); } //Remove reverse relation _siteDisclaimerRepository.DeleteRelatedResources(siteDisclaimer.ArticleId); foreach (var readArticle in siteDisclaimer.UserReadArticles.ToList()) { siteDisclaimer.UserReadArticles.Remove(readArticle); _siteDisclaimerRepository.Delete(readArticle); } foreach (var savedArticle in siteDisclaimer.UserSavedArticles.ToList()) { siteDisclaimer.UserSavedArticles.Remove(savedArticle); _siteDisclaimerRepository.Delete(savedArticle); } foreach (var contact in siteDisclaimer.ArticleRelatedContacts.ToList()) { siteDisclaimer.ArticleRelatedContacts.Remove(contact); _siteDisclaimerRepository.Delete(contact); } _siteDisclaimerRepository.Delete(siteDisclaimer); } await _siteDisclaimerRepository.UnitOfWork.SaveEntitiesAsync(); response.IsSuccessful = true; scope.Complete(); } using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var disclaimerDocs = _context.GetAll(Constants.ArticlesDiscriminator); foreach (var sitedisclaimer in siteDisclaimers) { foreach (var doc in disclaimerDocs.Where(d => d.GetPropertyValue <int>("ArticleId") == sitedisclaimer.ArticleId)) { var eventSource = new ArticleCommandEvent { id = doc.GetPropertyValue <Guid>("id"), EventType = ServiceBusEventType.Delete, Discriminator = Constants.ArticlesDiscriminator, PartitionKey = doc.GetPropertyValue <int>("LanguageId").ToString() }; await _eventcontext.PublishThroughEventBusAsync(eventSource); } } scope.Complete(); } return(response); }
public async Task <UpdateSiteDisclaimerCommandResponse> Handle(UpdateSiteDisclaimerCommand request, CancellationToken cancellationToken) { var response = new UpdateSiteDisclaimerCommandResponse() { IsSuccessful = false }; var siteDisclaimer = await _siteDisclaimerRepository.GetSiteDisclaimer(request.SiteDisclaimerId); if (siteDisclaimer == null) { throw new RulesException("siteDisclaimer", $"SiteDisclaimer with SiteDisclaimerId: {request.SiteDisclaimerId} not found"); } var contentToDelete = new List <int>(); using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { //Update existing disclaimer siteDisclaimer.Type = Convert.ToInt32(ArticleType.Page); siteDisclaimer.SubType = request.ArticleType; siteDisclaimer.Author = request.Author; siteDisclaimer.PublishedDate = DateTime.ParseExact(request.PublishedDate, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture).ToUniversalTime(); foreach (var item in request.LanguageContent) { var siteDisclaimerContent = siteDisclaimer.ArticleContents.FirstOrDefault(x => x.LanguageId.Equals(item.LanguageId)); if (siteDisclaimerContent == null) { siteDisclaimerContent = new ArticleContents { LanguageId = item.LanguageId, Content = item.Body, TeaserText = item.TeaserText, Title = item.Title }; siteDisclaimer.ArticleContents.Add(siteDisclaimerContent); } else { siteDisclaimerContent.Content = item.Body; siteDisclaimerContent.TeaserText = item.TeaserText; siteDisclaimerContent.Title = item.Title; siteDisclaimerContent.LanguageId = item.LanguageId; _siteDisclaimerRepository.Update(siteDisclaimerContent); } } foreach (var item in siteDisclaimer.ArticleContents.ToList()) { if (request.LanguageContent.Where(s => s.LanguageId == item.LanguageId).Count() == 0) { contentToDelete.Add((int)item.LanguageId); siteDisclaimer.ArticleContents.Remove(item); _siteDisclaimerRepository.Delete(item); } } siteDisclaimer.UpdatedBy = "CMS Admin"; siteDisclaimer.UpdatedDate = DateTime.UtcNow; await _siteDisclaimerRepository.UnitOfWork.SaveEntitiesAsync(); response.IsSuccessful = true; scope.Complete(); } using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var disclaimerdocs = _context.GetAll(Constants.ArticlesDiscriminator); foreach (var item in siteDisclaimer.ArticleContents) { var doc = disclaimerdocs.FirstOrDefault(d => d.GetPropertyValue <int>("ArticleId") == siteDisclaimer.ArticleId && d.GetPropertyValue <int?>("LanguageId") == item.LanguageId); var eventSource = new ArticleCommandEvent { id = doc != null?doc.GetPropertyValue <Guid>("id") : Guid.NewGuid(), EventType = doc != null ? ServiceBusEventType.Update : ServiceBusEventType.Create, Discriminator = Constants.ArticlesDiscriminator, Type = siteDisclaimer.Type, SubType = siteDisclaimer.SubType, Author = siteDisclaimer.Author ?? string.Empty, PublishedDate = siteDisclaimer.PublishedDate.ToString(), Title = item.Title, TeaserText = item.TeaserText, Content = item.Content, LanguageId = item.LanguageId, UpdatedBy = siteDisclaimer.UpdatedBy ?? string.Empty, UpdatedDate = siteDisclaimer.UpdatedDate.ToString(), CreatedBy = siteDisclaimer.CreatedBy ?? string.Empty, CreatedDate = siteDisclaimer.CreatedDate.ToString(), ArticleContentId = item.ArticleContentId, IsPublished = siteDisclaimer.IsPublished, ArticleId = siteDisclaimer.ArticleId, PartitionKey = "" }; await _eventcontext.PublishThroughEventBusAsync(eventSource); } foreach (int i in contentToDelete) { var deleteEvt = new ArticleCommandEvent() { id = disclaimerdocs.FirstOrDefault(d => d.GetPropertyValue <int>("ArticleId") == siteDisclaimer.ArticleId && d.GetPropertyValue <int?>("LanguageId") == i).GetPropertyValue <Guid>("id"), EventType = ServiceBusEventType.Delete, Discriminator = Constants.ArticlesDiscriminator, PartitionKey = "" }; await _eventcontext.PublishThroughEventBusAsync(deleteEvt); } scope.Complete(); } return(response); }