Пример #1
0
        public async Task <CreateResourceGroupCommandResponse> Handle(CreateResourceGroupCommand request, CancellationToken cancellationToken)
        {
            CreateResourceGroupCommandResponse response = new CreateResourceGroupCommandResponse()
            {
                IsSuccessful = false
            };
            ResourceGroups _resourceGroup = new ResourceGroups();

            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                //List<Languages> _languages = _ResourceGroupRepository.GetAllLanguages();
                _resourceGroup.IsPublished = true;
                _resourceGroup.Position    = request.Position;
                _resourceGroup.CreatedBy   = "";
                _resourceGroup.CreatedDate = DateTime.Now;
                _resourceGroup.UpdatedBy   = "";
                _resourceGroup.UpdatedDate = DateTime.Now;
                foreach (var langName in request.languageName)
                {
                    var resourceGroupContent = new ResourceGroupContents();
                    resourceGroupContent.GroupName  = langName.Name.Trim();
                    resourceGroupContent.LanguageId = langName.LanguageId;
                    _resourceGroup.ResourceGroupContents.Add(resourceGroupContent);
                }
                _ResourceGroupRepository.Add(_resourceGroup);
                await _ResourceGroupRepository.UnitOfWork
                .SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                foreach (var contnet in _resourceGroup.ResourceGroupContents)
                {
                    var eventSourcing = new ResourceGroupCommandEvent()
                    {
                        EventType              = 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 = contnet.ResourceGroupContentId,
                        LanguageId             = contnet.LanguageId,
                        GroupName              = contnet.GroupName,
                        PartitionKey           = ""
                    };
                    await _Eventcontext.PublishThroughEventBusAsync(eventSourcing);
                }
                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);
        }
Пример #3
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);
        }