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 <ManipulateResourceGroupCommandResponse> Handle(ManipulateResourceGroupCommand request, CancellationToken cancellationToken)
        {
            ManipulateResourceGroupCommandResponse response = new ManipulateResourceGroupCommandResponse()
            {
                IsSuccessful = false
            };
            var articleDocs = _context.GetAll(Constants.ArticlesDiscriminator);
            List <ResourceGroups> resourceGroups = _ResourceGroupRepository.getResourceGroups(request.ResourceGroupIds);

            if (request.ResourceGroupIds.Count != resourceGroups.Count)
            {
                throw new RulesException("Invalid", @"ResourceGroup not found");
            }

            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                if (request.Operation == "Publish")
                {
                    foreach (var resourcegroup in resourceGroups)
                    {
                        resourcegroup.IsPublished = true;
                        _ResourceGroupRepository.Update <ResourceGroups>(resourcegroup);
                    }
                }
                else if (request.Operation == "UnPublish")
                {
                    foreach (var resourcegroup in resourceGroups)
                    {
                        resourcegroup.IsPublished = false;
                        _ResourceGroupRepository.Update <ResourceGroups>(resourcegroup);
                    }
                }
                else if (request.Operation == "Delete")
                {
                    foreach (ResourceGroups resourcegroup in resourceGroups)
                    {
                        foreach (var resourceGroupContents in resourcegroup.ResourceGroupContents.ToList())
                        {
                            resourcegroup.ResourceGroupContents.Remove(resourceGroupContents);
                            _ResourceGroupRepository.Delete <ResourceGroupContents>(resourceGroupContents);
                        }
                        _ResourceGroupRepository.DeleteResourceGroup(resourcegroup);
                    }
                }
                else
                {
                    throw new RulesException("Operation", @"The Operation " + request.Operation + " is not valied");
                }
                await _ResourceGroupRepository.UnitOfWork
                .SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }

            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var resourcegrpDocs = _context.GetAll(Constants.ResourceGroupsDiscriminator);

                if (request.Operation == "Publish" || request.Operation == "UnPublish")
                {
                    foreach (var resourcegrp in resourceGroups)
                    {
                        foreach (var doc in resourcegrpDocs.Where(d => d.GetPropertyValue <int>("ResourceGroupId") == resourcegrp.ResourceGroupId))
                        {
                            var eventsource = new ResourceGroupCommandEvent()
                            {
                                id                     = doc.GetPropertyValue <Guid>("id"),
                                EventType              = ServiceBusEventType.Update,
                                Discriminator          = Constants.ResourceGroupsDiscriminator,
                                ResourceGroupId        = resourcegrp.ResourceGroupId,
                                IsPublished            = resourcegrp.IsPublished,
                                LanguageId             = doc.GetPropertyValue <int?>("LanguageId"),
                                GroupName              = doc.GetPropertyValue <string>("GroupName"),
                                Position               = doc.GetPropertyValue <int>("Position"),
                                ResourceGroupContentId = doc.GetPropertyValue <int>("ResourceGroupContentId"),
                                CreatedBy              = doc.GetPropertyValue <string>("CreatedBy"),
                                CreatedDate            = doc.GetPropertyValue <DateTime>("CreatedDate"),
                                UpdatedBy              = doc.GetPropertyValue <string>("UpdatedBy"),
                                UpdatedDate            = doc.GetPropertyValue <DateTime>("UpdatedDate"),
                                PartitionKey           = ""
                            };
                            await _Eventcontext.PublishThroughEventBusAsync(eventsource);
                        }
                    }
                }
                else if (request.Operation == "Delete")
                {
                    foreach (var resourcegrp in resourceGroups)
                    {
                        foreach (var content in resourcegrp.ResourceGroupContents)
                        {
                            foreach (var article in articleDocs.Where(ad => ad.GetPropertyValue <int>("LanguageId") == content.LanguageId))
                            {
                                var relatedResourceGroups = article.GetPropertyValue <List <ResourceGroupsSchema> >("ResourceGroup").FirstOrDefault();
                                if (relatedResourceGroups.ResourceGroupId == resourcegrp.ResourceGroupId)
                                {
                                    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    = new ResourceGroupsSchema
                                        {
                                            ResourceGroupId = -1, GroupName = "", Position = -1
                                        },
                                        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      = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedArticles"),
                                        RelatedResources     = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedResources"),
                                        Discriminator        = article.GetPropertyValue <string>("Discriminator"),
                                        PartitionKey         = ""
                                    };
                                    await _Eventcontext.PublishThroughEventBusAsync(eventSourcingRelated);
                                }
                            }
                        }
                        foreach (var doc in resourcegrpDocs.Where(d => d.GetPropertyValue <int>("ResourceGroupId") == resourcegrp.ResourceGroupId))
                        {
                            var resourceEvent = new ResourceGroupCommandEvent()
                            {
                                id            = doc.GetPropertyValue <Guid>("id"),
                                EventType     = ServiceBusEventType.Delete,
                                Discriminator = Constants.ResourceGroupsDiscriminator,
                                PartitionKey  = doc.GetPropertyValue <int>("LanguageId").ToString()
                            };
                            await _Eventcontext.PublishThroughEventBusAsync(resourceEvent);
                        }
                    }
                }
                scope.Complete();
            }
            return(response);
        }
Exemple #3
0
        public async Task <ManipulateTaxGroupCommandResponse> Handle(ManipulateTaxGroupCommand request, CancellationToken cancellationToken)
        {
            ManipulateTaxGroupCommandResponse response = new ManipulateTaxGroupCommandResponse()
            {
                IsSuccessful = false
            };

            List <TaxTags> tagGroups = _tagGroupRepository.GetTagGroups(request.TaxGroupIds);

            if (request.TaxGroupIds.Count != tagGroups.Count)
            {
                throw new RulesException("Invalid", @"TagGroup not found");
            }

            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                if (request.Operation == "Publish")
                {
                    foreach (var taxgroup in tagGroups)
                    {
                        if (taxgroup.ParentTagId == null && request.TagType == "Tag")
                        {
                            throw new RulesException("Invalid", @"Tag not Valid");
                        }
                        taxgroup.IsPublished = true;
                        _tagGroupRepository.Update <TaxTags>(taxgroup);
                    }
                }
                else if (request.Operation == "UnPublish")
                {
                    foreach (var taxgroup in tagGroups)
                    {
                        if (taxgroup.ParentTagId == null && request.TagType == "Tag")
                        {
                            throw new RulesException("Invalid", @"Tag not Valid");
                        }
                        taxgroup.IsPublished = false;
                        _tagGroupRepository.Update <TaxTags>(taxgroup);
                    }
                }
                else if (request.Operation == "Delete")
                {
                    foreach (TaxTags taxgroup in tagGroups)
                    {
                        foreach (var tagGroupContents in taxgroup.TaxTagContents.ToList())
                        {
                            taxgroup.TaxTagContents.Remove(tagGroupContents);
                            _tagGroupRepository.Delete <TaxTagContents>(tagGroupContents);
                        }
                        if (request.TagType == "Tag")
                        {
                            if (taxgroup.ParentTagId == null)
                            {
                                throw new RulesException("Invalid", @"Tag not Valid");
                            }
                            foreach (var country in taxgroup.TaxTagRelatedCountries.ToList())
                            {
                                taxgroup.TaxTagRelatedCountries.Remove(country);
                                _tagGroupRepository.Delete <TaxTagRelatedCountries>(country);
                            }
                        }
                        _tagGroupRepository.DeletetagGroup(taxgroup);
                    }
                }
                else
                {
                    throw new RulesException("Operation", @"The Operation " + request.Operation + " is not valied");
                }
                await _tagGroupRepository.UnitOfWork
                .SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }

            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var taxtagDocs = _context.GetAll(Constants.TaxTagsDiscriminator);

                if (request.Operation == "Publish" || request.Operation == "UnPublish")
                {
                    foreach (var tagGrp in tagGroups)
                    {
                        foreach (var doc in taxtagDocs.Where(d => d.GetPropertyValue <int>("TaxTagId") == tagGrp.TaxTagId))
                        {
                            var eventSource = new TagGroupCommandEvent
                            {
                                id                = doc.GetPropertyValue <Guid>("id"),
                                EventType         = ServiceBusEventType.Update,
                                Discriminator     = Constants.TaxTagsDiscriminator,
                                IsPublished       = tagGrp.IsPublished,
                                TagId             = tagGrp.TaxTagId,
                                UpdatedDate       = doc.GetPropertyValue <DateTime>("UpdatedDate"),
                                UpdatedBy         = doc.GetPropertyValue <string>("UpdatedBy"),
                                CreatedDate       = doc.GetPropertyValue <DateTime>("CreatedDate"),
                                CreatedBy         = doc.GetPropertyValue <string>("CreatedBy"),
                                DisplayName       = doc.GetPropertyValue <string>("DisplayName"),
                                LanguageId        = doc.GetPropertyValue <int?>("LanguageId"),
                                ParentTagId       = doc.GetPropertyValue <int?>("ParentTagId"),
                                RelatedCountryIds = doc.GetPropertyValue <List <int> >("RelatedCountryIds"),
                                TagContentId      = doc.GetPropertyValue <int>("TaxTagContentId"),
                                PartitionKey      = doc.GetPropertyValue <int>("LanguageId").ToString()
                            };
                            await _Eventcontext.PublishThroughEventBusAsync(eventSource);
                        }
                    }
                }
                else if (request.Operation == "Delete")
                {
                    foreach (var tagGrp in tagGroups)
                    {
                        foreach (var content in tagGrp.TaxTagContents)
                        {
                            var articleDocs = _context.GetAll(Constants.ArticlesDiscriminator);
                            foreach (var article in articleDocs.Where(ad => ad.GetPropertyValue <int>("LanguageId") == content.LanguageId))
                            {
                                foreach (var relatedTaxTags in article.GetPropertyValue <List <RelatedTaxTagsSchema> >("RelatedTaxTags"))
                                {
                                    if (relatedTaxTags.TaxTagId == content.TaxTagId)
                                    {
                                        List <RelatedTaxTagsSchema> relatedTaxTagsSchema = new List <RelatedTaxTagsSchema>();
                                        relatedTaxTagsSchema = article.GetPropertyValue <List <RelatedTaxTagsSchema> >("RelatedTaxTags");

                                        var index = relatedTaxTagsSchema.IndexOf(relatedTaxTagsSchema.Where(i => i.TaxTagId == content.TaxTagId).First());
                                        if (index != -1)
                                        {
                                            relatedTaxTagsSchema.Remove(relatedTaxTagsSchema.Where(i => i.TaxTagId == tagGrp.TaxTagId).First());
                                        }
                                        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        = relatedTaxTagsSchema,
                                                     RelatedArticles       = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedArticles"),
                                                     RelatedResources      = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedResources"),
                                                     Discriminator         = article.GetPropertyValue <string>("Discriminator"),
                                                     PartitionKey          = ""
                                        };
                                        await _Eventcontext.PublishThroughEventBusAsync(eventSourcingRelated);
                                    }
                                }
                            }
                        }
                        foreach (var doc in taxtagDocs.Where(d => d.GetPropertyValue <int>("TaxTagId") == tagGrp.TaxTagId))
                        {
                            var eventSrc = new TagGroupCommandEvent
                            {
                                id            = doc.GetPropertyValue <Guid>("id"),
                                EventType     = ServiceBusEventType.Delete,
                                Discriminator = Constants.TaxTagsDiscriminator,
                                PartitionKey  = ""
                            };
                            await _Eventcontext.PublishThroughEventBusAsync(eventSrc);
                        }
                    }
                }
                scope.Complete();
            }
            return(response);
        }
Exemple #4
0
        public async Task <UpdateResourceGroupCommandResponse> Handle(UpdateResourceGroupCommand request, CancellationToken cancellationToken)
        {
            var response = new UpdateResourceGroupCommandResponse()
            {
                IsSuccessful = false
            };
            List <int> objresourceGroupId = new List <int>();

            objresourceGroupId.Add(request.ResourceGroupId);
            var resourceGroup      = _ResourceGroupRepository.getResourceGroups(objresourceGroupId)[0];
            var contentToDelete    = new List <int>();
            var articleDocs        = _context.GetAll(Constants.ArticlesDiscriminator);
            var resourceGroupsDocs = _context.GetAll(Constants.ResourceGroupsDiscriminator);

            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                resourceGroup.Position = request.Position;
                //List<Languages> languages = _ResourceGroupRepository.GetAllLanguages();

                foreach (var content in request.LanguageName)
                {
                    var resourceGroupContents = resourceGroup.ResourceGroupContents.Where(s => s.LanguageId == content.LanguageId).FirstOrDefault();
                    if (resourceGroupContents == null)
                    {
                        ResourceGroupContents objresourceGroupContents = new ResourceGroupContents();
                        objresourceGroupContents.GroupName  = content.Name;
                        objresourceGroupContents.LanguageId = content.LanguageId;
                        resourceGroup.ResourceGroupContents.Add(objresourceGroupContents);
                    }
                    else
                    {
                        resourceGroupContents.GroupName = content.Name;
                        _ResourceGroupRepository.Update(resourceGroupContents);
                    }
                }
                //    List<ResourceGroupContents> ResourceGroupContents = resourceGroup.ResourceGroupContents.Where(s => s.ResourceGroupId == request.ResourceGroupId).ToList();
                foreach (var resourceContent in resourceGroup.ResourceGroupContents.ToList())
                {
                    if (request.LanguageName.Where(s => s.LanguageId == resourceContent.LanguageId).Count() == 0)
                    {
                        contentToDelete.Add((int)resourceContent.LanguageId);
                        resourceGroup.ResourceGroupContents.Remove(resourceContent);
                        _ResourceGroupRepository.Delete(resourceContent);
                    }
                }
                resourceGroup.UpdatedBy   = "";
                resourceGroup.UpdatedDate = DateTime.Now;
                await _ResourceGroupRepository.UnitOfWork
                .SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var resourcegroupDocs = _context.GetAll(Constants.ResourceGroupsDiscriminator);
                foreach (var content in resourceGroup.ResourceGroupContents)
                {
                    foreach (var article in articleDocs.Where(ad => ad.GetPropertyValue <int>("LanguageId") == content.LanguageId))
                    {
                        // foreach (var relatedResourceGroups in article.GetPropertyValue<List<ResourceGroupsSchema>>("ResourceGroup"))
                        //{
                        var relatedResourceGroups = article.GetPropertyValue <List <ResourceGroupsSchema> >("ResourceGroup").FirstOrDefault();
                        if (relatedResourceGroups.ResourceGroupId == resourceGroup.ResourceGroupId)
                        {
                            ResourceGroupsSchema resourceGroupsSchema = new ResourceGroupsSchema();
                            //relatedArticleSchema = article.GetPropertyValue<ResourceGroupsSchema>("RelatedArticles");

                            // var index = relatedArticleSchema.IndexOf(relatedArticleSchema.Where(i => i.ArticleId == _article.ArticleId).First());
                            //if (index != -1)
                            resourceGroupsSchema = new ResourceGroupsSchema {
                                ResourceGroupId = resourceGroup.ResourceGroupId, GroupName = content.GroupName == null ? "" : content.GroupName, Position = resourceGroup.Position == null ? -1 : resourceGroup.Position
                            };
                            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         = resourceGroupsSchema,
                                         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       = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedArticles"),
                                         RelatedResources      = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedResources"),
                                         Discriminator         = article.GetPropertyValue <string>("Discriminator"),
                                         PartitionKey          = ""
                            };
                            await _eventcontext.PublishThroughEventBusAsync(eventSourcingRelated);
                        }
                    }
                    var doc = resourcegroupDocs.FirstOrDefault(d => d.GetPropertyValue <int>("ResourceGroupId") == resourceGroup.ResourceGroupId &&
                                                               d.GetPropertyValue <int?>("LanguageId") == content.LanguageId);
                    var eventSourcing = new ResourceGroupCommandEvent()
                    {
                        id = doc != null?doc.GetPropertyValue <Guid>("id") : Guid.NewGuid(),
                                 EventType              = doc != null ? ServiceBusEventType.Update : ServiceBusEventType.Create,
                                 Discriminator          = Constants.ResourceGroupsDiscriminator,
                                 ResourceGroupId        = resourceGroup.ResourceGroupId,
                                 IsPublished            = resourceGroup.IsPublished,
                                 CreatedBy              = resourceGroup.CreatedBy,
                                 CreatedDate            = resourceGroup.CreatedDate,
                                 UpdatedBy              = resourceGroup.UpdatedBy,
                                 UpdatedDate            = resourceGroup.UpdatedDate,
                                 Position               = resourceGroup.Position,
                                 ResourceGroupContentId = content.ResourceGroupContentId,
                                 LanguageId             = content.LanguageId,
                                 GroupName              = content.GroupName,
                                 PartitionKey           = ""
                    };
                    await _eventcontext.PublishThroughEventBusAsync(eventSourcing);
                }
                foreach (int i in contentToDelete)
                {
                    foreach (var article in articleDocs.Where(ad => ad.GetPropertyValue <int>("LanguageId") == i))
                    {
                        var relatedResourceGroups = article.GetPropertyValue <List <ResourceGroupsSchema> >("ResourceGroup").FirstOrDefault();
                        if (relatedResourceGroups.ResourceGroupId == resourceGroup.ResourceGroupId)
                        {
                            var GroupNameInEnglish = resourceGroupsDocs.Where(rg => rg.GetPropertyValue <int>("ResourceGroupContentId") == resourceGroup.ResourceGroupId && rg.GetPropertyValue <int>("LanguageId") == 37).Select(rgs => rgs.GetPropertyValue <string>("GroupName")).FirstOrDefault();
                            var ResourceGroup      = (GroupNameInEnglish == "") ? new ResourceGroupsSchema {
                                ResourceGroupId = -1, GroupName = "", Position = -1
                            } : new ResourceGroupsSchema {
                                ResourceGroupId = resourceGroup.ResourceGroupId, GroupName = GroupNameInEnglish, Position = relatedResourceGroups.Position
                            };
                            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    = new ResourceGroupsSchema
                                {
                                    ResourceGroupId = -1, GroupName = "", Position = -1
                                },
                                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      = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedArticles"),
                                RelatedResources     = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedResources"),
                                Discriminator        = article.GetPropertyValue <string>("Discriminator"),
                                PartitionKey         = ""
                            };
                            await _eventcontext.PublishThroughEventBusAsync(eventSourcingRelated);
                        }
                    }
                    var deleteEvt = new ResourceGroupCommandEvent()
                    {
                        id = resourcegroupDocs.FirstOrDefault(d => d.GetPropertyValue <int>("ResourceGroupId") == resourceGroup.ResourceGroupId &&
                                                              d.GetPropertyValue <int?>("LanguageId") == i).GetPropertyValue <Guid>("id"),
                        EventType     = ServiceBusEventType.Delete,
                        Discriminator = Constants.ResourceGroupsDiscriminator,
                        PartitionKey  = ""
                    };
                    await _eventcontext.PublishThroughEventBusAsync(deleteEvt);
                }
                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);
        }
Exemple #6
0
        public async Task <ManipulateArticlesCommandResponse> Handle(ManipulateArticlesCommand request, CancellationToken cancellationToken)
        {
            ManipulateArticlesCommandResponse response = new ManipulateArticlesCommandResponse()
            {
                IsSuccessful = false
            };

            Disclaimers     DisclaimersDetails    = new Disclaimers();
            ResourceGroups  ResourceGroupsDetails = new ResourceGroups();
            Provinces       ProvincesDetails      = new Provinces();
            List <TaxTags>  taxTagsDetails        = new List <TaxTags>();
            List <Articles> articlesDetails       = new List <Articles>();
            List <Articles> articles = _ArticleRepository.getArticleCompleteDataById(request.ArticlesIds);

            if (request.ArticlesIds.Count != articles.Count)
            {
                throw new RulesException("Invalid", @"Country not found");
            }
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                if (request.Operation == "Publish")
                {
                    foreach (var article in articles)
                    {
                        article.IsPublished = true;
                        _ArticleRepository.Update <Articles>(article);
                    }
                }
                else if (request.Operation == "UnPublish")
                {
                    foreach (var article in articles)
                    {
                        article.IsPublished = false;
                        _ArticleRepository.Update <Articles>(article);
                    }
                }
                else if (request.Operation == "Delete")
                {
                    foreach (Articles article in articles)
                    {
                        foreach (var content in article.ArticleContents.ToList())
                        {
                            article.ArticleContents.Remove(content);
                            _ArticleRepository.Delete <ArticleContents>(content);
                        }
                        foreach (var country in article.ArticleRelatedCountries.ToList())
                        {
                            article.ArticleRelatedCountries.Remove(country);
                        }
                        foreach (var countryGroup in article.ArticleRelatedCountryGroups.ToList())
                        {
                            article.ArticleRelatedCountryGroups.Remove(countryGroup);
                        }
                        foreach (var taxTag in article.ArticleRelatedTaxTags.ToList())
                        {
                            article.ArticleRelatedTaxTags.Remove(taxTag);
                        }
                        foreach (var relatedArticle in article.RelatedArticlesArticle.ToList())
                        {
                            article.RelatedArticlesArticle.Remove(relatedArticle);
                            //Remove reverse relation
                            // relatedArticle.RelatedArticles.Remove(article);
                        }
                        foreach (var relatedResource in article.RelatedResourcesArticle.ToList())
                        {
                            article.RelatedResourcesArticle.Remove(relatedResource);
                        }
                        foreach (var readArticle in article.UserReadArticles.ToList())
                        {
                            article.UserReadArticles.Remove(readArticle);
                            _ArticleRepository.Delete <UserReadArticles>(readArticle);
                        }
                        foreach (var savedArticle in article.UserSavedArticles.ToList())
                        {
                            article.UserSavedArticles.Remove(savedArticle);
                            _ArticleRepository.Delete <UserSavedArticles>(savedArticle);
                        }
                        foreach (var contact in article.ArticleRelatedContacts.ToList())
                        {
                            article.ArticleRelatedContacts.Remove(contact);
                        }
                        _ArticleRepository.DeleteArticle(article);
                    }
                }
                else
                {
                    throw new RulesException("Operation", @"The Operation " + request.Operation + " is not valied");
                }
                await _ArticleRepository.UnitOfWork
                .SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }
            var articleDocs = _context.GetAll(Constants.ArticlesDiscriminator);

            if (request.Operation == "Publish" || request.Operation == "UnPublish")
            {
                foreach (var article in articles)
                {
                    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()));
                    using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                    {
                        foreach (var doc in articleDocs.Where(d => d.GetPropertyValue <int>("ArticleId") == article.ArticleId))
                        {
                            var DisclaimerLanguageId    = DisclaimersDetails.DisclaimerContents.Where(d => d.LanguageId == doc.GetPropertyValue <int>("LanguageId")).Count() > 0 ? doc.GetPropertyValue <int>("LanguageId") : 37;
                            var ResourceGroupLanguageId = ResourceGroupsDetails.ResourceGroupContents.Where(d => d.LanguageId == doc.GetPropertyValue <int>("LanguageId")).Count() > 0 ? doc.GetPropertyValue <int>("LanguageId") : 37;
                            var ProvisionsLanguageId    = ProvincesDetails.ProvinceContents.Where(d => d.LanguageId == doc.GetPropertyValue <int>("LanguageId")).Count() > 0 ? doc.GetPropertyValue <int>("LanguageId") : 37;


                            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.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          = request.Operation == "Publish" ? true : false,
                                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      = doc.GetPropertyValue <int>("ArticleContentId") == null ? -1 : doc.GetPropertyValue <int>("ArticleContentId"),
                                LanguageId            = doc.GetPropertyValue <int>("LanguageId") == null ? -1 : doc.GetPropertyValue <int>("LanguageId"),
                                Title                 = doc.GetPropertyValue <string>("Title") == null ? "" : doc.GetPropertyValue <string>("Title"),
                                TitleInEnglishDefault = doc.GetPropertyValue <string>("TitleInEnglishDefault") == null ? "" : doc.GetPropertyValue <string>("TitleInEnglishDefault"),
                                TeaserText            = doc.GetPropertyValue <string>("TeaserText") == null ? "" : doc.GetPropertyValue <string>("TeaserText"),
                                Content               = doc.GetPropertyValue <string>("Content") == null ? "" : doc.GetPropertyValue <string>("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 == doc.GetPropertyValue <int>("LanguageId")).Count() > 0 ? doc.GetPropertyValue <int>("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 == doc.GetPropertyValue <int>("LanguageId")).Count() > 0 ? doc.GetPropertyValue <int>("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 == doc.GetPropertyValue <int>("LanguageId")).Count() > 0 ? doc.GetPropertyValue <int>("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);
                        }
                        scope.Complete();
                    }
                }
            }
            else if (request.Operation == "Delete")
            {
                using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    foreach (var item in articles)
                    {
                        foreach (var content in item.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 == item.ArticleId)
                                    {
                                        List <RelatedArticlesSchema> relatedArticleSchema = new List <RelatedArticlesSchema>();
                                        relatedArticleSchema = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedArticles");

                                        var index = relatedArticleSchema.IndexOf(relatedArticleSchema.Where(i => i.ArticleId == item.ArticleId).First());
                                        if (index != -1)
                                        {
                                            relatedArticleSchema.Remove(relatedArticleSchema.Where(i => i.ArticleId == item.ArticleId).First());
                                        }
                                        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);
                                    }
                                }
                            }
                        }
                        foreach (var doc in articleDocs.Where(d => d.GetPropertyValue <int>("ArticleId") == item.ArticleId))
                        {
                            var articleevent = new ArticleCommandEvent()
                            {
                                id            = doc.GetPropertyValue <Guid>("id"),
                                EventType     = ServiceBusEventType.Delete,
                                Discriminator = Constants.ArticlesDiscriminator,
                                PartitionKey  = doc.GetPropertyValue <int>("LanguageId").ToString()
                            };
                            await _Eventcontext.PublishThroughEventBusAsync(articleevent);
                        }
                    }
                    scope.Complete();
                }
            }

            return(response);
        }
Exemple #7
0
        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);
        }
Exemple #9
0
        public async Task <UpdateTagGroupsCommandResponse> Handle(UpdateTagsCommand request, CancellationToken cancellationToken)
        {
            UpdateTagGroupsCommandResponse response = new UpdateTagGroupsCommandResponse()
            {
                IsSuccessful = false
            };
            List <int> objTagGroups = new List <int>();

            objTagGroups.Add(request.TagGroupsId);
            var taxGroup        = _taxTagsRepository.GetTagGroups(objTagGroups)[0];
            var contentToDelete = new List <int>();
            var articleDocs     = _context.GetAll(Constants.ArticlesDiscriminator);
            var taxTagDocs      = _context.GetAll(Constants.TaxTagsDiscriminator);

            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                //List<Languages> languages = _taxTagsRepository.GetAllLanguages();
                if (request.TagType == "Tag")
                {
                    if (taxGroup.ParentTagId == null)
                    {
                        throw new RulesException("Invalid", @"Tag not Valid");
                    }
                    taxGroup.ParentTagId = request.TagGroup;
                    foreach (var country in request.RelatedCountyIds)
                    {
                        var taxCountries = taxGroup.TaxTagRelatedCountries.Where(s => s.CountryId == country).FirstOrDefault();
                        if (taxCountries == null)
                        {
                            TaxTagRelatedCountries objRelatedCountries = new TaxTagRelatedCountries();
                            objRelatedCountries.CountryId = country;
                            taxGroup.TaxTagRelatedCountries.Add(objRelatedCountries);
                        }
                        else
                        {
                            taxCountries.CountryId = country;
                            _taxTagsRepository.Update(taxCountries);
                        }
                    }
                }
                foreach (var content in request.LanguageName)
                {
                    var taxGroupContents = taxGroup.TaxTagContents.Where(s => s.LanguageId == content.LanguageId).FirstOrDefault();
                    if (taxGroupContents == null)
                    {
                        TaxTagContents objtaxGroupContents = new TaxTagContents();
                        objtaxGroupContents.DisplayName = content.Name;
                        objtaxGroupContents.LanguageId  = content.LanguageId;
                        taxGroup.TaxTagContents.Add(objtaxGroupContents);
                    }
                    else
                    {
                        taxGroupContents.DisplayName = content.Name;
                        _taxTagsRepository.Update(taxGroupContents);
                    }
                }
                //  List<TaxTagContents> ResourceGroupContents = taxGroup.TaxTagContents.Where(s => s.TaxTagId == request.TagGroupsId).ToList();
                foreach (var resourceContent in taxGroup.TaxTagContents.ToList())
                {
                    if (request.LanguageName.Where(s => s.LanguageId == resourceContent.LanguageId).Count() == 0)
                    {
                        contentToDelete.Add((int)resourceContent.LanguageId);
                        taxGroup.TaxTagContents.Remove(resourceContent);
                        _taxTagsRepository.Delete(resourceContent);
                    }
                }
                foreach (var resourceCountries in taxGroup.TaxTagRelatedCountries.ToList())
                {
                    if (request.RelatedCountyIds.Where(s => s == resourceCountries.CountryId).Count() == 0)
                    {
                        taxGroup.TaxTagRelatedCountries.Remove(resourceCountries);
                        _taxTagsRepository.Delete(resourceCountries);
                    }
                }
                taxGroup.UpdatedBy   = "";
                taxGroup.UpdatedDate = DateTime.Now;
                await _taxTagsRepository.UnitOfWork
                .SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var taggroupDocs = _context.GetAll(Constants.TaxTagsDiscriminator);
                foreach (var content in taxGroup.TaxTagContents)
                {
                    foreach (var article in articleDocs.Where(ad => ad.GetPropertyValue <int>("LanguageId") == content.LanguageId))
                    {
                        foreach (var relatedTaxTags in article.GetPropertyValue <List <RelatedTaxTagsSchema> >("RelatedTaxTags"))
                        {
                            if (relatedTaxTags.TaxTagId == content.TaxTagId)
                            {
                                List <RelatedTaxTagsSchema> relatedTaxTagsSchema = new List <RelatedTaxTagsSchema>();
                                relatedTaxTagsSchema = article.GetPropertyValue <List <RelatedTaxTagsSchema> >("RelatedTaxTags");

                                var index = relatedTaxTagsSchema.IndexOf(relatedTaxTagsSchema.Where(i => i.TaxTagId == content.TaxTagId).First());
                                if (index != -1)
                                {
                                    relatedTaxTagsSchema[index] = new RelatedTaxTagsSchema {
                                        TaxTagId = int.Parse(content.TaxTagId.ToString()), DisplayName = content.DisplayName
                                    }
                                }
                                ;
                                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        = relatedTaxTagsSchema,
                                             RelatedArticles       = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedArticles"),
                                             RelatedResources      = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedResources"),
                                             Discriminator         = article.GetPropertyValue <string>("Discriminator"),
                                             PartitionKey          = ""
                                };
                                await _Eventcontext.PublishThroughEventBusAsync(eventSourcingRelated);
                            }
                        }
                    }
                    var doc = taggroupDocs.FirstOrDefault(d => d.GetPropertyValue <int>("TaxTagId") == taxGroup.TaxTagId &&
                                                          d.GetPropertyValue <int?>("LanguageId") == content.LanguageId);
                    var eventSourcing = new TagGroupCommandEvent()
                    {
                        id = doc != null?doc.GetPropertyValue <Guid>("id") : Guid.NewGuid(),
                                 EventType         = doc != null ? ServiceBusEventType.Update : ServiceBusEventType.Create,
                                 Discriminator     = Constants.TaxTagsDiscriminator,
                                 TagId             = taxGroup.TaxTagId,
                                 ParentTagId       = taxGroup.ParentTagId,
                                 IsPublished       = taxGroup.IsPublished,
                                 CreatedBy         = taxGroup.CreatedBy,
                                 CreatedDate       = taxGroup.CreatedDate,
                                 UpdatedBy         = taxGroup.UpdatedBy,
                                 UpdatedDate       = taxGroup.UpdatedDate,
                                 RelatedCountryIds = (from rc in taxGroup.TaxTagRelatedCountries where rc != null select rc.CountryId).ToList(),
                                 TagContentId      = content.TaxTagContentId,
                                 LanguageId        = content.LanguageId,
                                 DisplayName       = content.DisplayName,
                                 PartitionKey      = ""
                    };
                    await _Eventcontext.PublishThroughEventBusAsync(eventSourcing);
                }
                foreach (int i in contentToDelete)
                {
                    foreach (var article in articleDocs.Where(ad => ad.GetPropertyValue <int>("LanguageId") == i))
                    {
                        foreach (var RelatedTaxTags in article.GetPropertyValue <List <RelatedTaxTagsSchema> >("RelatedTaxTags"))
                        {
                            if (RelatedTaxTags.TaxTagId == taxGroup.TaxTagId)
                            {
                                var DisplayNameInEnglish = taxTagDocs.Where(ad => ad.GetPropertyValue <int>("TaxTagId") == taxGroup.TaxTagId && ad.GetPropertyValue <int>("LanguageId") == 37).Select(ads => ads.GetPropertyValue <string>("DisplayName")).FirstOrDefault();
                                List <RelatedTaxTagsSchema> relatedTaxTagsSchema = new List <RelatedTaxTagsSchema>();
                                relatedTaxTagsSchema = article.GetPropertyValue <List <RelatedTaxTagsSchema> >("RelatedTaxTags");

                                var index = relatedTaxTagsSchema.IndexOf(relatedTaxTagsSchema.Where(ras => ras.TaxTagId == taxGroup.TaxTagId).First());
                                if (index != -1)
                                {
                                    if (DisplayNameInEnglish == "")
                                    {
                                        relatedTaxTagsSchema.Remove(relatedTaxTagsSchema.Where(rtt => rtt.TaxTagId == taxGroup.TaxTagId).First());
                                    }
                                    else
                                    {
                                        relatedTaxTagsSchema[index] = new RelatedTaxTagsSchema {
                                            TaxTagId = taxGroup.TaxTagId, DisplayName = (DisplayNameInEnglish == null ? "" : DisplayNameInEnglish)
                                        }
                                    }
                                }
                                ;
                                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        = relatedTaxTagsSchema,
                                             RelatedArticles       = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedArticles"),
                                             RelatedResources      = article.GetPropertyValue <List <RelatedArticlesSchema> >("RelatedResources"),
                                             Discriminator         = article.GetPropertyValue <string>("Discriminator"),
                                             PartitionKey          = ""
                                };
                                await _Eventcontext.PublishThroughEventBusAsync(eventSourcingRelated);
                            }
                        }
                    }
                    var deleteEvt = new TagGroupCommandEvent()
                    {
                        id = taggroupDocs.FirstOrDefault(d => d.GetPropertyValue <int>("TagId") == taxGroup.TaxTagId &&
                                                         d.GetPropertyValue <int?>("LanguageId") == i).GetPropertyValue <Guid>("id"),
                        EventType     = ServiceBusEventType.Delete,
                        Discriminator = Constants.TaxTagsDiscriminator,
                        PartitionKey  = ""
                    };
                    await _Eventcontext.PublishThroughEventBusAsync(deleteEvt);
                }
                scope.Complete();
            }
            return(response);
        }
    }
Exemple #10
0
        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);
        }