///// <summary>首次连接数据库时初始化数据,仅用于实体类重载,用户不应该调用该方法</summary> //[EditorBrowsable(EditorBrowsableState.Never)] //protected override void InitData() //{ // base.InitData(); // // InitData一般用于当数据表没有数据时添加一些默认数据,该实体类的任何第一次数据库操作都会触发该方法,默认异步调用 // // Meta.Count是快速取得表记录数 // if (Meta.Count > 0) return; // // 需要注意的是,如果该方法调用了其它实体类的首次数据库操作,目标实体类的数据初始化将会在同一个线程完成 // if (XTrace.Debug) XTrace.WriteLine("开始初始化{0}[{1}]数据……", typeof(ArticleCategory).Name, Meta.Table.DataTable.DisplayName); // var entity = new ArticleCategory(); // entity.Serialnum = "abc"; // entity.CreateTime = DateTime.Now; // entity.CreateSysUserSerialnum = "abc"; // entity.CreateSysUserUserName = "******"; // entity.UpdateTime = DateTime.Now; // entity.UpdateSysUserSerialnum = "abc"; // entity.UpdateSysUserUserName = "******"; // entity.Name = "abc"; // entity.ParentSerialnum = "abc"; // entity.Sort = 0; // entity.Num = 0; // entity.Remark = "abc"; // entity.Insert(); // if (XTrace.Debug) XTrace.WriteLine("完成初始化{0}[{1}]数据!", typeof(ArticleCategory).Name, Meta.Table.DataTable.DisplayName); //} /// <summary>已重载。删除关联数据</summary> /// <returns></returns> protected override int OnDelete() { if (ArticleContents != null) { ArticleContents.Delete(); } return(base.OnDelete()); }
public void UpdateContent(IArticle data) { var newVersion = ArticleContents.Count + 1; var content = new ArticleContent() { Article = this, ArticleContentUid = Guid.NewGuid(), ArticleId = ArticleId, Content = data.Content.ToString(), Version = newVersion }; ArticleContents.Add(content); }
public async Task SendMessage(string art_Id, string userName, string message) { var user = new ApplicationUser(); user = await user.getUserAsync(userName); var artC = new ArticleContents(); var cList = await artC.GetListAsync(long.Parse(art_Id)); var imageContents = ""; imageContents = string.Join <string>(",", cList?.Where(c => c.getContentType() == "image").Select(c => c.FileName)); var videoContents = ""; videoContents = string.Join <string>(",", cList?.Where(c => c.getContentType() == "video").Select(c => c.FileName)); await Clients.Group(userName).SendAsync("ReceiveMessage", art_Id, userName, user.ApplicationUserName, message, imageContents, videoContents); }
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); }
public async Task <CreateSiteDisclaimerCommandResponse> Handle(CreateSiteDisclaimerCommand request, CancellationToken cancellationToken) { var response = new CreateSiteDisclaimerCommandResponse() { IsSuccessful = false }; Disclaimers DisclaimersDetails = new Disclaimers(); var siteDisclaimer = new Articles { PublishedDate = DateTime.ParseExact(request.PublishedDate, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture).ToUniversalTime(), Author = request.Author, Type = Convert.ToInt32(ArticleType.Page), SubType = request.ArticleType, IsPublished = true, CreatedBy = "CMS Admin", CreatedDate = DateTime.UtcNow, UpdatedDate = DateTime.UtcNow }; using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { foreach (var item in request.LanguageContent) { var siteDisclaimerContent = new ArticleContents { LanguageId = item.LanguageId, Title = item.Title, TeaserText = item.TeaserText, Content = item.Body }; siteDisclaimer.ArticleContents.Add(siteDisclaimerContent); } await _siteDisclaimerRepository.AddAsync(siteDisclaimer); await _siteDisclaimerRepository.UnitOfWork .SaveEntitiesAsync(); DisclaimersDetails = siteDisclaimer.DisclaimerId == null ? null : _siteDisclaimerRepository.getDisclaimerById(int.Parse(siteDisclaimer.DisclaimerId.ToString())); response.IsSuccessful = true; scope.Complete(); } using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { foreach (var content in siteDisclaimer.ArticleContents) { var eventSourcing = new ArticleCommandEvent() { EventType = ServiceBusEventType.Create, Discriminator = Constants.ArticlesDiscriminator, ArticleId = siteDisclaimer.ArticleId, CreatedBy = siteDisclaimer.CreatedBy ?? string.Empty, CreatedDate = siteDisclaimer.CreatedDate.ToString(), UpdatedBy = siteDisclaimer.UpdatedBy ?? string.Empty, UpdatedDate = siteDisclaimer.UpdatedDate.ToString(), PublishedDate = siteDisclaimer.PublishedDate.ToString(), IsPublished = siteDisclaimer.IsPublished, Author = siteDisclaimer.Author ?? string.Empty, Type = siteDisclaimer.Type, SubType = siteDisclaimer.SubType, LanguageId = content.LanguageId, Title = content.Title, TeaserText = content.TeaserText, Content = content.Content, ArticleContentId = content.ArticleContentId, Disclaimer = new DisclamersSchema { DisclaimerId = int.Parse(siteDisclaimer.DisclaimerId.ToString()), ProviderName = DisclaimersDetails.DisclaimerContents.Where(d => d.LanguageId == content.LanguageId).Select(ds => ds.ProviderName).FirstOrDefault(), ProviderTerms = DisclaimersDetails.DisclaimerContents.Where(d => d.LanguageId == content.LanguageId).Select(ds => ds.ProviderTerms).FirstOrDefault() }, PartitionKey = "" }; await _eventcontext.PublishThroughEventBusAsync(eventSourcing); } scope.Complete(); } return(response); }
public async Task <UpdateArticleCommandResponse> Handle(UpdateArticleCommand request, CancellationToken cancellationToken) { UpdateArticleCommandResponse response = new UpdateArticleCommandResponse() { IsSuccessful = false }; try { Disclaimers DisclaimersDetails = new Disclaimers(); ResourceGroups ResourceGroupsDetails = new ResourceGroups(); Provinces ProvincesDetails = new Provinces(); List <TaxTags> taxTagsDetails = new List <TaxTags>(); List <Articles> articlesDetails = new List <Articles>(); Articles _article = _ArticleRepository.getArticleCompleteDataById(new List <int> { request.ArticleID })[0]; var contentToDelete = new List <int>(); using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { _article.UpdatedBy = request.UpdatedBy; _article.UpdatedDate = DateTime.UtcNow; _article.Author = request.Author; _article.DisclaimerId = request.DisclaimerId; _article.ResourceGroupId = request.ResourceGroupId > 0 ? request.ResourceGroupId : (int?)null; _article.ResourcePosition = request.ResourcePosition > 0 ? request.ResourcePosition : (int?)null; if (request.PublishedDate != null) { _article.PublishedDate = request.PublishedDate.Value; } _article.SubType = request.SubType; _article.State = request.State; _article.ProvinceId = request.ProvinceId; _article.IsPublished = request.IsPublished;//= Action == "Publish"; //update article content //delete removed languages, update existing, add new languages foreach (var content in request.ArticleContent) { var artContent = _article.ArticleContents.FirstOrDefault(v => v.LanguageId == content.LanguageId); if (artContent == null) { var newContent = new ArticleContents { Content = content.Content, LanguageId = content.LanguageId, TeaserText = content.TeaserText, Title = content.Title }; //content.ArticleId = articleobject.ArticleId; _article.ArticleContents.Add(newContent); } else { artContent.Content = content.Content; artContent.LanguageId = content.LanguageId; artContent.TeaserText = content.TeaserText; artContent.Title = content.Title; _ArticleRepository.Update <ArticleContents>(artContent); } } var articleContentIds = _article.ArticleContents.Select(c => c.LanguageId).ToList(); var articleDtoCids = request.ArticleContent.Select(c => c.LanguageId).ToList(); var removedContents = articleContentIds.Except(articleDtoCids); _article.ArticleContents.Where(a => removedContents.Contains(a.LanguageId)).ToList().ForEach(removed => { _ArticleRepository.Delete <ArticleContents>(removed); _article.ArticleContents.Remove(removed); contentToDelete.Add((int)removed.LanguageId); }); // Update Related Countries foreach (var country in request.RelatedCountries) { var articleCountry = _article.ArticleRelatedCountries .FirstOrDefault(c => c.CountryId == country); if (articleCountry != null) { continue; } var newCountry = _ArticleRepository.getCountryById(country); _article.ArticleRelatedCountries.Add(new ArticleRelatedCountries { Country = newCountry, CountryId = newCountry.CountryId }); } var articleCountries = _article.ArticleRelatedCountries.Select(c => c.CountryId).ToList(); var dtoCountries = request.RelatedCountries; var removedCountries = articleCountries.Except(dtoCountries); _article.ArticleRelatedCountries.Where(a => removedCountries.Contains(a.CountryId)) .ToList() .ForEach(removed => { _article.ArticleRelatedCountries.Remove(removed); }); // Update Related Country Groups foreach (var countryGroup in request.RelatedCountryGroups) { var articleCountryGroup = _article.ArticleRelatedCountryGroups .FirstOrDefault(c => c.CountryGroupId == countryGroup); if (articleCountryGroup != null) { continue; } var newCountryGroup = _ArticleRepository.getCountryGroupById(countryGroup); _article.ArticleRelatedCountryGroups.Add(new ArticleRelatedCountryGroups { CountryGroup = newCountryGroup, CountryGroupId = newCountryGroup.CountryGroupId }); } var articleCountryGroups = _article.ArticleRelatedCountryGroups.Select(c => c.CountryGroupId).ToList(); var dtoCountryGroups = request.RelatedCountryGroups; var removedCountryGroups = articleCountryGroups.Except(dtoCountryGroups); _article.ArticleRelatedCountryGroups.Where(a => removedCountryGroups.Contains(a.CountryGroupId)) .ToList() .ForEach(removed => { _article.ArticleRelatedCountryGroups.Remove(removed); }); //update related articles if (request.RelatedArticles != null) { var relatedArticles = _article.RelatedArticlesArticle.Select(c => c.ArticleId).ToList(); var dtoRelatedArticles = request.RelatedArticles; var addRelatedArticles = dtoRelatedArticles.Except(relatedArticles); var newRelatedArticles = _ArticleRepository.getArticlesListById(addRelatedArticles.ToList());// context.Articles.Where(a => addRelatedArticles.Contains(a.ArticleId)); newRelatedArticles.ForEach(a => { _article.RelatedArticlesArticle.Add(new RelatedArticles { Article = a, RelatedArticleId = a.ArticleId }); //Reverse relation //if (!a.RelatedArticlesArticle.Any(r => r.ArticleId == _article.ArticleId)) //{ // a.RelatedArticlesArticle.Add(new RelatedArticles { Article = _article, RelatedArticleId = _article.ArticleId }); //} }); var removedRelatedArticles = relatedArticles.Except(dtoRelatedArticles); _article.RelatedArticlesArticle.Where(a => removedRelatedArticles.Contains(a.ArticleId)) .ToList() .ForEach(removed => { _article.RelatedArticlesArticle.Remove(removed); }); } // update related resources if (request.RelatedResources != null) { foreach (var rResource in request.RelatedResources) { var relatedRes = _article.RelatedResourcesArticle.FirstOrDefault(c => c.RelatedArticleId == rResource); if (relatedRes != null) { continue; } var newRelatedResource = _ArticleRepository.getArticleDataById(rResource); _article.RelatedResourcesArticle.Add(new RelatedResources { Article = newRelatedResource, RelatedArticleId = newRelatedResource.ArticleId }); } var relatedResources = _article.RelatedResourcesArticle.Select(c => c.RelatedArticleId).ToList(); var dtoRelatedResources = request.RelatedResources; var removedRelatedResources = relatedResources.Except(dtoRelatedResources); _article.RelatedResourcesArticle.Where(a => removedRelatedResources.Contains(a.RelatedArticleId)) .ToList() .ForEach(removed => { _article.RelatedResourcesArticle.Remove(removed); }); } //update related tags if (request.RelatedTaxTags != null) { foreach (var rTag in request.RelatedTaxTags) { var relatedTag = _article.ArticleRelatedTaxTags.FirstOrDefault(c => c.TaxTagId == rTag); if (relatedTag == null) { var newTag = _ArticleRepository.getTaxTagsById(rTag); _article.ArticleRelatedTaxTags.Add(new ArticleRelatedTaxTags { TaxTag = newTag, TaxTagId = newTag.TaxTagId }); } } var relatedTags = _article.ArticleRelatedTaxTags.Select(c => c.TaxTagId).ToList(); var dtoTags = request.RelatedTaxTags; var removedTags = relatedTags.Except(dtoTags); _article.ArticleRelatedTaxTags.Where(a => removedTags.Contains(a.TaxTagId)) .ToList() .ForEach(removed => { _article.ArticleRelatedTaxTags.Remove(removed); }); } //update related contacts if (request.RelatedContacts != null) { foreach (var dtoContact in request.RelatedContacts) { var relatedContact = _article.ArticleRelatedContacts.FirstOrDefault(c => c.ContactId == dtoContact); if (relatedContact == null) { var newContact = _ArticleRepository.getContactsById(dtoContact);// context.Contacts.FirstOrDefault(c => c.ContactId == dtoContact.ContactId); _article.ArticleRelatedContacts.Add(new ArticleRelatedContacts { Contact = newContact, ContactId = newContact.ContactId }); } } var relatedContacts = _article.ArticleRelatedContacts.Select(c => c.ContactId).ToList(); var dtoContacts = request.RelatedContacts; var removeContacts = relatedContacts.Except(dtoContacts); _article.ArticleRelatedContacts.Where(a => removeContacts.Contains(a.ContactId)) .ToList() .ForEach(removeContact => _article.ArticleRelatedContacts.Remove(removeContact)); } //update image _article.ImageId = request.ImageId; //Push logic needs to be implemented int userCount = _ArticleRepository.SendNotificationsForArticle <UpdateArticleCommand>(request); if (userCount > 0) { _article.NotificationSentDate = DateTime.Now; } await _ArticleRepository.UnitOfWork .SaveEntitiesAsync(); taxTagsDetails = _ArticleRepository.getTaxTagsDetailsByIds(_article.ArticleRelatedTaxTags.Select(s => s.TaxTagId).ToList()); articlesDetails = _ArticleRepository.getArticleCompleteDataById(_article.RelatedArticlesArticle.Select(s => s.RelatedArticleId).ToList()); ResourceGroupsDetails = _article.ResourceGroupId == null ? null : _ArticleRepository.getResourceGroupById(int.Parse(_article.ResourceGroupId.ToString())); ProvincesDetails = _article.ProvinceId == null ? null : _ArticleRepository.getProvisionsById(int.Parse(_article.ProvinceId.ToString())); DisclaimersDetails = _article.DisclaimerId == null ? null : _ArticleRepository.getDisclaimerById(int.Parse(_article.DisclaimerId.ToString())); response.IsSuccessful = true; scope.Complete(); } using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var articleDocs = _context.GetAll(Constants.ArticlesDiscriminator); foreach (var content in _article.ArticleContents) { foreach (var article in articleDocs.Where(ad => ad.GetPropertyValue <int>("LanguageId") == content.LanguageId)) { foreach (var relatedArticles in article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedArticles")) { if (relatedArticles.ArticleId == _article.ArticleId) { List <RelatedArticlesSchema> relatedArticleSchema = new List <RelatedArticlesSchema>(); relatedArticleSchema = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedArticles"); var index = relatedArticleSchema.IndexOf(relatedArticleSchema.Where(i => i.ArticleId == _article.ArticleId).First()); if (index != -1) { relatedArticleSchema[index] = new RelatedArticlesSchema { ArticleId = _article.ArticleId, Title = content.Title == null ? "" : content.Title, PublishedDate = _article.PublishedDate == null ? "" : _article.PublishedDate.ToString(), CountryId = _article.ArticleRelatedCountries.Select(s => new RelatedEntityId { IdVal = s.CountryId }).ToList() } } ; var eventSourcingRelated = new ArticleCommandEvent() { id = article != null?article.GetPropertyValue <Guid>("id") : Guid.NewGuid(), EventType = ServiceBusEventType.Update, ArticleId = article.GetPropertyValue <int>("ArticleId"), PublishedDate = article.GetPropertyValue <string>("PublishedDate"), Author = article.GetPropertyValue <string>("author"), ImageId = article.GetPropertyValue <int>("ImageId"), State = article.GetPropertyValue <string>("State"), Type = article.GetPropertyValue <int>("Type"), SubType = article.GetPropertyValue <int>("SubType"), ResourcePosition = article.GetPropertyValue <int>("ResourcePosition"), Disclaimer = article.GetPropertyValue <DisclamersSchema>("Disclaimer"), ResourceGroup = article.GetPropertyValue <ResourceGroupsSchema>("ResourceGroup"), IsPublished = article.GetPropertyValue <bool>("IsPublished"), CreatedDate = article.GetPropertyValue <string>("CreatedDate"), CreatedBy = article.GetPropertyValue <string>("CreatedBy"), UpdatedDate = article.GetPropertyValue <string>("UpdatedDate"), UpdatedBy = article.GetPropertyValue <string>("UpdatedBy"), NotificationSentDate = article.GetPropertyValue <string>("NotificationSentDate"), Provinces = article.GetPropertyValue <ProvinceSchema>("Provisions"), ArticleContentId = article.GetPropertyValue <int>("ArticleContentId"), LanguageId = article.GetPropertyValue <int>("LanguageId"), Title = article.GetPropertyValue <string>("Title"), TitleInEnglishDefault = article.GetPropertyValue <string>("TitleInEnglishDefault"), TeaserText = article.GetPropertyValue <string>("TeaserText"), Content = article.GetPropertyValue <string>("Content"), RelatedContacts = article.GetPropertyValue <List <RelatedEntityId> >("RelatedContacts"), RelatedCountries = article.GetPropertyValue <List <RelatedEntityId> >("RelatedCountries"), RelatedCountryGroups = article.GetPropertyValue <List <RelatedEntityId> >("RelatedCountryGroups"), RelatedTaxTags = article.GetPropertyValue <List <RelatedTaxTagsSchema> >("RelatedTaxTags"), RelatedArticles = relatedArticleSchema, RelatedResources = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedResources"), Discriminator = article.GetPropertyValue <string>("Discriminator"), PartitionKey = "" }; await _Eventcontext.PublishThroughEventBusAsync(eventSourcingRelated); } } } var DisclaimerLanguageId = DisclaimersDetails.DisclaimerContents.Where(d => d.LanguageId == content.LanguageId).Count() > 0 ? content.LanguageId : 37; var ResourceGroupLanguageId = ResourceGroupsDetails.ResourceGroupContents.Where(d => d.LanguageId == content.LanguageId).Count() > 0 ? content.LanguageId : 37; var ProvisionsLanguageId = ProvincesDetails.ProvinceContents.Where(d => d.LanguageId == content.LanguageId).Count() > 0 ? content.LanguageId : 37; var doc = articleDocs.FirstOrDefault(d => d.GetPropertyValue <int>("ArticleId") == _article.ArticleId && d.GetPropertyValue <int?>("LanguageId") == content.LanguageId); var eventSourcing = new ArticleCommandEvent() { id = doc != null?doc.GetPropertyValue <Guid>("id") : Guid.NewGuid(), EventType = doc != null ? ServiceBusEventType.Update : ServiceBusEventType.Create, ArticleId = _article.ArticleId, PublishedDate = _article.PublishedDate == null ? "" : _article.PublishedDate.ToString(), Author = _article.Author == null ? "" : _article.Author, ImageId = _article.ImageId == null ? -1 : _article.ImageId, State = _article.State == null ? "" : _article.State, Type = _article.Type == null ? -1 : _article.Type, SubType = _article.SubType == null ? -1 : _article.SubType, ResourcePosition = _article.ResourcePosition == null ? -1 : _article.ResourcePosition, Disclaimer = new DisclamersSchema { DisclaimerId = int.Parse(_article.DisclaimerId == null?"-1": _article.DisclaimerId.ToString()), ProviderName = DisclaimersDetails.DisclaimerContents.Where(d => d.LanguageId == DisclaimerLanguageId).Select(ds => ds.ProviderName == null ? "" : ds.ProviderName).FirstOrDefault(), ProviderTerms = DisclaimersDetails.DisclaimerContents.Where(d => d.LanguageId == DisclaimerLanguageId).Select(ds => ds.ProviderTerms == null ? "" : ds.ProviderTerms).FirstOrDefault() }, ResourceGroup = new ResourceGroupsSchema { ResourceGroupId = int.Parse(_article.ResourceGroupId == null ? "-1" : _article.ResourceGroupId.ToString()), GroupName = ResourceGroupsDetails.ResourceGroupContents.Where(d => d.LanguageId == ResourceGroupLanguageId).Select(ds => ds.GroupName == null ? "" : ds.GroupName).FirstOrDefault(), Position = ResourceGroupsDetails.Position == null ? -1 : ResourceGroupsDetails.Position }, IsPublished = _article.IsPublished == null ? false : _article.IsPublished, CreatedDate = _article.CreatedDate == null ? "" : _article.CreatedDate.ToString(), CreatedBy = _article.CreatedBy == null ? "" : _article.CreatedBy, UpdatedDate = _article.UpdatedDate == null ? "" : _article.UpdatedDate.ToString(), UpdatedBy = _article.UpdatedBy == null ? "" : _article.UpdatedBy, NotificationSentDate = _article.NotificationSentDate == null ? "" : _article.NotificationSentDate.ToString(), Provinces = new ProvinceSchema { ProvinceId = int.Parse(_article.ProvinceId.ToString()), DisplayName = ProvincesDetails.ProvinceContents.Where(d => d.LanguageId == ProvisionsLanguageId).Select(ds => ds.DisplayName == null ? "" : ds.DisplayName).FirstOrDefault() }, ArticleContentId = content.ArticleContentId == null ? -1 : content.ArticleContentId, LanguageId = content.LanguageId == null ? -1 : content.LanguageId, Title = content.Title == null ? "" : content.Title, TitleInEnglishDefault = _article.ArticleContents.Where(l => l.LanguageId == 37 && l.ArticleId == content.ArticleId).Select(s => s.Title == null ? "" : s.Title).FirstOrDefault(), TeaserText = content.TeaserText == null ? "" : content.TeaserText, Content = content.Content == null ? "" : content.Content, RelatedContacts = _article.ArticleRelatedContacts.Select(s => new RelatedEntityId { IdVal = s.ContactId }).ToList(), RelatedCountries = _article.ArticleRelatedCountries.Select(s => new RelatedEntityId { IdVal = s.CountryId }).ToList(), RelatedCountryGroups = _article.ArticleRelatedCountryGroups.Select(s => new RelatedEntityId { IdVal = s.CountryGroupId }).ToList(), RelatedTaxTags = _article.ArticleRelatedTaxTags.Select(s => { var RelatedtaxTagLanguageId = taxTagsDetails.Where(td => td.TaxTagId == s.TaxTagId).FirstOrDefault().TaxTagContents.Where(ttc => ttc.LanguageId == content.LanguageId).Count() > 0 ? content.LanguageId : 37; return(new RelatedTaxTagsSchema { TaxTagId = s.TaxTagId, DisplayName = taxTagsDetails.Where(td => td.TaxTagId == s.TaxTagId).FirstOrDefault().TaxTagContents.Where(ttc => ttc.LanguageId == RelatedtaxTagLanguageId).Select(ttcs => ttcs.DisplayName == null ? "" : ttcs.DisplayName).FirstOrDefault() }); }).ToList(), RelatedArticles = _article.RelatedArticlesArticle.Select(s => { var RelatedArticleLanguageId = articlesDetails.Where(ra => ra.ArticleId.Equals(s.RelatedArticleId)).FirstOrDefault().ArticleContents.Where(ttc => ttc.LanguageId == content.LanguageId).Count() > 0 ? content.LanguageId : 37; return(new RelatedArticlesSchema { ArticleId = s.RelatedArticleId, PublishedDate = articlesDetails.Where(ra => ra.ArticleId.Equals(s.RelatedArticleId)).Select(v => v.PublishedDate == null ? "" : v.PublishedDate.ToString()).FirstOrDefault().ToString(), Title = articlesDetails.Where(ra => ra.ArticleId.Equals(s.RelatedArticleId)).FirstOrDefault().ArticleContents.Where(ttc => ttc.LanguageId == RelatedArticleLanguageId).Select(v => v.Title == null ? "" : v.Title).FirstOrDefault().ToString(), CountryId = articlesDetails.Where(ad => ad.ArticleId.Equals(s.RelatedArticleId)).FirstOrDefault().ArticleRelatedCountries.Select(arc => new RelatedEntityId { IdVal = arc.CountryId }).ToList() }); }).ToList(), RelatedResources = _article.RelatedResourcesArticle.Select(s => { var RelatedResourceLanguageId = articlesDetails.Where(ra => ra.ArticleId.Equals(s.RelatedArticleId)).FirstOrDefault().ArticleContents.Where(ttc => ttc.LanguageId == content.LanguageId).Count() > 0 ? content.LanguageId : 37; return(new RelatedArticlesSchema { ArticleId = s.RelatedArticleId, PublishedDate = articlesDetails.Where(ra => ra.ArticleId.Equals(s.RelatedArticleId)).Select(v => v.PublishedDate == null ? "" : v.PublishedDate.ToString()).FirstOrDefault().ToString(), Title = articlesDetails.Where(ra => ra.ArticleId.Equals(s.RelatedArticleId)).FirstOrDefault().ArticleContents.Where(ttc => ttc.LanguageId == RelatedResourceLanguageId).Select(v => v.Title == null ? "" : v.Title).FirstOrDefault().ToString(), CountryId = articlesDetails.Where(ad => ad.ArticleId.Equals(s.RelatedArticleId)).FirstOrDefault().ArticleRelatedCountries.Select(arc => new RelatedEntityId { IdVal = arc.CountryId }).ToList() }); }).ToList(), Discriminator = Constants.ArticlesDiscriminator, PartitionKey = "" }; await _Eventcontext.PublishThroughEventBusAsync(eventSourcing); } foreach (int i in contentToDelete) { foreach (var article in articleDocs.Where(ad => ad.GetPropertyValue <int>("LanguageId") == i)) { foreach (var relatedArticles in article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedArticles")) { if (relatedArticles.ArticleId == _article.ArticleId) { var titleInEnglish = articleDocs.Where(ad => ad.GetPropertyValue <int>("ArticleId") == _article.ArticleId && ad.GetPropertyValue <int>("LanguageId") == 37).Select(ads => ads.GetPropertyValue <string>("Title")).FirstOrDefault(); List <RelatedArticlesSchema> relatedArticleSchema = new List <RelatedArticlesSchema>(); relatedArticleSchema = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedArticles"); var index = relatedArticleSchema.IndexOf(relatedArticleSchema.Where(ras => ras.ArticleId == _article.ArticleId).First()); if (index != -1) { if (titleInEnglish == "") { relatedArticleSchema.Remove(relatedArticleSchema.Where(ras => ras.ArticleId == _article.ArticleId).First()); } else { relatedArticleSchema[index] = new RelatedArticlesSchema { ArticleId = _article.ArticleId, Title = (titleInEnglish == null ? "" : titleInEnglish), PublishedDate = _article.PublishedDate == null ? "" : _article.PublishedDate.ToString(), CountryId = _article.ArticleRelatedCountries.Select(s => new RelatedEntityId { IdVal = s.CountryId }).ToList() } } } ; var eventSourcingRelated = new ArticleCommandEvent() { id = article != null?article.GetPropertyValue <Guid>("id") : Guid.NewGuid(), EventType = ServiceBusEventType.Update, ArticleId = article.GetPropertyValue <int>("ArticleId"), PublishedDate = article.GetPropertyValue <string>("PublishedDate"), Author = article.GetPropertyValue <string>("author"), ImageId = article.GetPropertyValue <int>("ImageId"), State = article.GetPropertyValue <string>("State"), Type = article.GetPropertyValue <int>("Type"), SubType = article.GetPropertyValue <int>("SubType"), ResourcePosition = article.GetPropertyValue <int>("ResourcePosition"), Disclaimer = article.GetPropertyValue <DisclamersSchema>("Disclaimer"), ResourceGroup = article.GetPropertyValue <ResourceGroupsSchema>("ResourceGroup"), IsPublished = article.GetPropertyValue <bool>("IsPublished"), CreatedDate = article.GetPropertyValue <string>("CreatedDate"), CreatedBy = article.GetPropertyValue <string>("CreatedBy"), UpdatedDate = article.GetPropertyValue <string>("UpdatedDate"), UpdatedBy = article.GetPropertyValue <string>("UpdatedBy"), NotificationSentDate = article.GetPropertyValue <string>("NotificationSentDate"), Provinces = article.GetPropertyValue <ProvinceSchema>("Provisions"), ArticleContentId = article.GetPropertyValue <int>("ArticleContentId"), LanguageId = article.GetPropertyValue <int>("LanguageId"), Title = article.GetPropertyValue <string>("Title"), TitleInEnglishDefault = article.GetPropertyValue <string>("TitleInEnglishDefault"), TeaserText = article.GetPropertyValue <string>("TeaserText"), Content = article.GetPropertyValue <string>("Content"), RelatedContacts = article.GetPropertyValue <List <RelatedEntityId> >("RelatedContacts"), RelatedCountries = article.GetPropertyValue <List <RelatedEntityId> >("RelatedCountries"), RelatedCountryGroups = article.GetPropertyValue <List <RelatedEntityId> >("RelatedCountryGroups"), RelatedTaxTags = article.GetPropertyValue <List <RelatedTaxTagsSchema> >("RelatedTaxTags"), RelatedArticles = relatedArticleSchema, RelatedResources = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedResources"), Discriminator = article.GetPropertyValue <string>("Discriminator"), PartitionKey = "" }; await _Eventcontext.PublishThroughEventBusAsync(eventSourcingRelated); } } } var deleteEvt = new ArticleCommandEvent() { id = articleDocs.FirstOrDefault(d => d.GetPropertyValue <int>("ArticleId") == _article.ArticleId && d.GetPropertyValue <int>("LanguageId") == i).GetPropertyValue <Guid>("id"), EventType = ServiceBusEventType.Delete, Discriminator = Constants.ArticlesDiscriminator, PartitionKey = i.ToString() }; await _Eventcontext.PublishThroughEventBusAsync(deleteEvt); } scope.Complete(); } return(response); } catch (Exception ex) { response.IsSuccessful = false; response.FailureReason = "Technical Error"; // _logger.LogError(ex, "Error while handling command"); } return(response); }
public async Task <CreateArticleCommandResponse> Handle(CreateArticleCommand request, CancellationToken cancellationToken) { CreateArticleCommandResponse response = new CreateArticleCommandResponse() { IsSuccessful = false }; try { List <Articles> articlesDetails = new List <Articles>(); List <TaxTags> taxTagsDetails = new List <TaxTags>(); ResourceGroups ResourceGroupsDetails = new ResourceGroups(); Provinces ProvincesDetails = new Provinces(); Disclaimers DisclaimersDetails = new Disclaimers(); List <Articles> rarticles = _ArticleRepository.getArticlesListById(request.RelatedArticles); List <Articles> rresources = _ArticleRepository.getArticlesListById(request.RelatedResources); List <TaxTags> rtaxTags = _ArticleRepository.getTaxTagsDetailsByIds(request.RelatedTaxTags); List <Countries> rcountries = _ArticleRepository.getCountriesByIds(request.RelatedCountries); List <CountryGroups> rcountryGroups = _ArticleRepository.getCountryGroupsByIds(request.RelatedCountryGroups); List <Contacts> rcontacts = _ArticleRepository.getContactsByIds(request.RelatedContacts); Articles _article = new Articles(); using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { _article.Type = request.Type; _article.Author = request.Author; _article.PublishedDate = request.PublishedDate.Value; _article.SubType = request.SubType; _article.ResourceGroupId = request.ResourceGroupId > 0 ? request.ResourceGroupId : (int?)null; _article.ResourcePosition = request.ResourcePosition > 0 ? request.ResourcePosition : (int?)null; _article.IsPublished = request.IsPublished; _article.CreatedDate = DateTime.UtcNow; _article.UpdatedDate = DateTime.UtcNow; _article.ArticleContents = new List <ArticleContents>(); _article.CreatedBy = request.UpdatedBy; _article.UpdatedBy = request.UpdatedBy; _article.DisclaimerId = request.DisclaimerId; _article.ImageId = request.ImageId; _article.State = request.State; _article.ProvinceId = request.ProvinceId; foreach (var acontent in request.ArticleContent) { ArticleContents ac = new ArticleContents() { LanguageId = acontent.LanguageId, Content = acontent.Content, TeaserText = acontent.TeaserText, Title = acontent.Title }; _article.ArticleContents.Add(ac); } if (request.RelatedArticles != null) { // var rArticleList = request.RelatedArticles; // var relatedArticles = rarticles.Where(a => rArticleList.Any(ra => ra == a.ArticleId)).ToList(); foreach (Articles article in rarticles) { _article.RelatedArticlesArticle.Add(new RelatedArticles() { RelatedArticleId = article.ArticleId, Article = article }); } ; //Add reverse relation //relatedArticles.ForEach(a => a.RelatedArticlesArticle.Add(new RelatedArticles {Article = Article })); } if (request.RelatedResources != null) { // var rResourceList = request.RelatedResources.ToList(); // var relatedResources = articles.Where(a => rResourceList.Contains(a.ArticleId)).ToList(); foreach (Articles article in rresources) { _article.RelatedResourcesArticle.Add(new RelatedResources() { RelatedArticleId = article.ArticleId, Article = article }); } ; } if (request.RelatedTaxTags != null) { // var rTagList = request.RelatedTaxTags.ToList(); //var relatedTags = taxTags.Where(t => rTagList.Contains(t.TaxTagId)).ToList(); foreach (TaxTags tags in rtaxTags) { _article.ArticleRelatedTaxTags.Add(new ArticleRelatedTaxTags() { TaxTagId = tags.TaxTagId, TaxTag = tags }); } ; } if (request.RelatedCountries != null) { // var rCountries = request.RelatedCountries.ToList(); // var relatedCountries = countries.Where(c => rCountries.Contains(c.CountryId)).ToList(); foreach (Countries country in rcountries) { _article.ArticleRelatedCountries.Add(new ArticleRelatedCountries() { CountryId = country.CountryId, Country = country }); } ; } if (request.RelatedCountryGroups != null) { // var rCountryGroups = request.RelatedCountryGroups.ToList(); // var relatedCountryGroups = countryGroups.Where(c => rCountryGroups.Contains(c.CountryGroupId)).ToList(); foreach (CountryGroups cGroup in rcountryGroups) { _article.ArticleRelatedCountryGroups.Add(new ArticleRelatedCountryGroups() { CountryGroupId = cGroup.CountryGroupId, CountryGroup = cGroup }); } ; } if (request.RelatedContacts != null) { foreach (Contacts rcontact in rcontacts) { _article.ArticleRelatedContacts.Add(new ArticleRelatedContacts() { ContactId = rcontact.ContactId, Contact = rcontact }); } ; } //Push logic needs to be implemented int userCount = _ArticleRepository.SendNotificationsForArticle <CreateArticleCommand>(request); if (userCount > 0) { _article.NotificationSentDate = DateTime.Now; } _ArticleRepository.Add(_article); await _ArticleRepository.UnitOfWork .SaveEntitiesAsync(); articlesDetails = _ArticleRepository.getArticleCompleteDataById(_article.RelatedArticlesArticle.Select(s => s.RelatedArticleId).ToList()); taxTagsDetails = _ArticleRepository.getTaxTagsDetailsByIds(_article.ArticleRelatedTaxTags.Select(s => s.TaxTagId).ToList()); ResourceGroupsDetails = _article.ResourceGroupId == null ? null : _ArticleRepository.getResourceGroupById(int.Parse(_article.ResourceGroupId.ToString())); ProvincesDetails = _article.ProvinceId == null ? null : _ArticleRepository.getProvisionsById(int.Parse(_article.ProvinceId.ToString())); DisclaimersDetails = _article.DisclaimerId == null ? null : _ArticleRepository.getDisclaimerById(int.Parse(_article.DisclaimerId.ToString())); response.IsSuccessful = true; scope.Complete(); } using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { foreach (var content in _article.ArticleContents) { var DisclaimerLanguageId = DisclaimersDetails.DisclaimerContents.Where(d => d.LanguageId == content.LanguageId).Count() > 0 ? content.LanguageId : 37; var ResourceGroupLanguageId = ResourceGroupsDetails.ResourceGroupContents.Where(d => d.LanguageId == content.LanguageId).Count() > 0 ? content.LanguageId : 37; var ProvisionsLanguageId = ProvincesDetails.ProvinceContents.Where(d => d.LanguageId == content.LanguageId).Count() > 0 ? content.LanguageId : 37; var eventSourcing = new ArticleCommandEvent() { EventType = ServiceBusEventType.Create, ArticleId = _article.ArticleId, PublishedDate = _article.PublishedDate == null?"": _article.PublishedDate.ToString(), Author = _article.Author == null ? "" : _article.Author, ImageId = _article.ImageId == null ? -1 : _article.ImageId, State = _article.State == null ? "" : _article.State, Type = _article.Type == null ? -1 : _article.Type, SubType = _article.SubType == null ? -1 : _article.SubType, ResourcePosition = _article.ResourcePosition == null ? -1 : _article.ResourcePosition, Disclaimer = new DisclamersSchema { DisclaimerId = int.Parse(_article.DisclaimerId.ToString()), ProviderName = DisclaimersDetails.DisclaimerContents.Where(d => d.LanguageId == DisclaimerLanguageId).Select(ds => ds.ProviderName == null ? "" : ds.ProviderName).FirstOrDefault(), ProviderTerms = DisclaimersDetails.DisclaimerContents.Where(d => d.LanguageId == DisclaimerLanguageId).Select(ds => ds.ProviderTerms == null ? "" : ds.ProviderTerms).FirstOrDefault() }, ResourceGroup = new ResourceGroupsSchema { ResourceGroupId = int.Parse(_article.ResourceGroupId.ToString()), GroupName = ResourceGroupsDetails.ResourceGroupContents.Where(d => d.LanguageId == ResourceGroupLanguageId).Select(ds => ds.GroupName == null ? "" : ds.GroupName).FirstOrDefault(), Position = ResourceGroupsDetails.Position == null ? -1 : ResourceGroupsDetails.Position }, IsPublished = _article.IsPublished == null ? false : _article.IsPublished, CreatedDate = _article.CreatedDate == null ? "" : _article.CreatedDate.ToString(), CreatedBy = _article.CreatedBy == null ? "" : _article.CreatedBy, UpdatedDate = _article.UpdatedDate == null ? "" : _article.UpdatedDate.ToString(), UpdatedBy = _article.UpdatedBy == null ? "" : _article.UpdatedBy, NotificationSentDate = _article.NotificationSentDate == null ? "" : _article.NotificationSentDate.ToString(), Provinces = new ProvinceSchema { ProvinceId = int.Parse(_article.ProvinceId.ToString()), DisplayName = ProvincesDetails.ProvinceContents.Where(d => d.LanguageId == ProvisionsLanguageId).Select(ds => ds.DisplayName == null ? "" : ds.DisplayName).FirstOrDefault() }, ArticleContentId = content.ArticleContentId == null ? -1 : content.ArticleContentId, LanguageId = content.LanguageId == null ? -1 : content.LanguageId, Title = content.Title == null ? "" : content.Title, TitleInEnglishDefault = _article.ArticleContents.Where(l => l.LanguageId == 37 && l.ArticleId == content.ArticleId).Select(s => { if (s.Title == null) { return(""); } else { return(s.Title); } }).FirstOrDefault(), TeaserText = content.TeaserText == null ? "" : content.TeaserText, Content = content.Content == null ? "" : content.Content, RelatedContacts = _article.ArticleRelatedContacts.Select(s => new RelatedEntityId { IdVal = s.ContactId }).ToList(), RelatedCountries = _article.ArticleRelatedCountries.Select(s => new RelatedEntityId { IdVal = s.CountryId }).ToList(), RelatedCountryGroups = _article.ArticleRelatedCountryGroups.Select(s => new RelatedEntityId { IdVal = s.CountryGroupId }).ToList(), RelatedTaxTags = _article.ArticleRelatedTaxTags.Select(s => { var RelatedtaxTagLanguageId = taxTagsDetails.Where(td => td.TaxTagId == s.TaxTagId).FirstOrDefault().TaxTagContents.Where(ttc => ttc.LanguageId == content.LanguageId).Count() > 0 ? content.LanguageId : 37; return(new RelatedTaxTagsSchema { TaxTagId = s.TaxTagId, DisplayName = taxTagsDetails.Where(td => td.TaxTagId == s.TaxTagId).FirstOrDefault().TaxTagContents.Where(ttc => ttc.LanguageId == RelatedtaxTagLanguageId).Select(ttcs => ttcs.DisplayName == null ? "" : ttcs.DisplayName).FirstOrDefault() }); }).ToList(), RelatedArticles = _article.RelatedArticlesArticle.Select(s => { var RelatedArticleLanguageId = articlesDetails.Where(ra => ra.ArticleId.Equals(s.RelatedArticleId)).FirstOrDefault().ArticleContents.Where(ttc => ttc.LanguageId == content.LanguageId).Count() > 0 ? content.LanguageId : 37; return(new RelatedArticlesSchema { ArticleId = s.RelatedArticleId, PublishedDate = articlesDetails.Where(ra => ra.ArticleId.Equals(s.RelatedArticleId)).Select(v => v.PublishedDate == null ? "" : v.PublishedDate.ToString()).FirstOrDefault(), Title = articlesDetails.Where(ra => ra.ArticleId.Equals(s.RelatedArticleId)).FirstOrDefault().ArticleContents.Where(ttc => ttc.LanguageId == RelatedArticleLanguageId).Select(v => v.Title == null ? "" : v.Title).FirstOrDefault().ToString(), CountryId = articlesDetails.Where(ad => ad.ArticleId.Equals(s.RelatedArticleId)).FirstOrDefault().ArticleRelatedCountries.Select(arc => new RelatedEntityId { IdVal = arc.CountryId }).ToList() }); }).ToList(), RelatedResources = _article.RelatedResourcesArticle.Select(s => { var RelatedResourceLanguageId = articlesDetails.Where(ra => ra.ArticleId.Equals(s.RelatedArticleId)).FirstOrDefault().ArticleContents.Where(ttc => ttc.LanguageId == content.LanguageId).Count() > 0 ? content.LanguageId : 37; return(new RelatedArticlesSchema { ArticleId = s.RelatedArticleId, PublishedDate = articlesDetails.Where(ra => ra.ArticleId.Equals(s.RelatedArticleId)).Select(v => v.PublishedDate == null ? "" : v.PublishedDate.ToString()).FirstOrDefault(), Title = articlesDetails.Where(ra => ra.ArticleId.Equals(s.RelatedArticleId)).FirstOrDefault().ArticleContents.Where(ttc => ttc.LanguageId == RelatedResourceLanguageId).Select(v => v.Title == null ? "" : v.Title).FirstOrDefault().ToString(), CountryId = articlesDetails.Where(ad => ad.ArticleId.Equals(s.RelatedArticleId)).FirstOrDefault().ArticleRelatedCountries.Select(arc => new RelatedEntityId { IdVal = arc.CountryId }).ToList() }); }).ToList(), Discriminator = Constants.ArticlesDiscriminator, PartitionKey = "" }; await _Eventcontext.PublishThroughEventBusAsync(eventSourcing); } scope.Complete(); } return(response); } catch (Exception ex) { response.IsSuccessful = false; response.FailureReason = "Technical Error"; // _logger.LogError(ex, "Error while handling command"); } return(response); }