public async Task <ManipulateCountryGroupsCommandResponse> Handle(ManipulateCountryGroupsCommand request, CancellationToken cancellationToken)
        {
            ManipulateCountryGroupsCommandResponse response = new ManipulateCountryGroupsCommandResponse()
            {
                IsSuccessful = false
            };

            List <CountryGroups> countryGroups = _CountryGroupsRepository.getCountryGroups(request.CountryGroupIds);

            if (request.CountryGroupIds.Count != countryGroups.Count)
            {
                throw new RulesException("Invalid", @"CountryGroup not found");
            }

            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                if (request.Operation == "Publish")
                {
                    foreach (var countryGroup in countryGroups)
                    {
                        countryGroup.IsPublished = true;
                        _CountryGroupsRepository.Update <CountryGroups>(countryGroup);
                    }
                }
                else if (request.Operation == "UnPublish")
                {
                    foreach (var countryGroup in countryGroups)
                    {
                        countryGroup.IsPublished = false;
                        _CountryGroupsRepository.Update <CountryGroups>(countryGroup);
                    }
                }
                else if (request.Operation == "Delete")
                {
                    foreach (CountryGroups countryGroup in countryGroups)
                    {
                        foreach (var countryCountry in countryGroup.CountryGroupAssociatedCountries.ToList())
                        {
                            countryGroup.CountryGroupAssociatedCountries.Remove(countryCountry);
                            _CountryGroupsRepository.Delete <CountryGroupAssociatedCountries>(countryCountry);
                        }

                        foreach (var countryContents in countryGroup.CountryGroupContents.ToList())
                        {
                            countryGroup.CountryGroupContents.Remove(countryContents);
                            _CountryGroupsRepository.Delete <CountryGroupContents>(countryContents);
                        }
                        _CountryGroupsRepository.DeleteCountryGroup(countryGroup);
                    }
                }
                else
                {
                    throw new RulesException("Operation", @"The Operation " + request.Operation + " is not valied");
                }
                await _CountryGroupsRepository.UnitOfWork
                .SaveEntitiesAsync();

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

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

                if (request.Operation == "Publish" || request.Operation == "UnPublish")
                {
                    foreach (var countrygrp in countryGroups)
                    {
                        foreach (var doc in countrygrpDocs.Where(d => d.GetPropertyValue <int>("CountryGroupId") == countrygrp.CountryGroupId))
                        {
                            var eventsource = new CountryGroupCommandEvent()
                            {
                                id                    = doc.GetPropertyValue <Guid>("id"),
                                EventType             = ServiceBusEventType.Update,
                                Discriminator         = Constants.CountryGroupsDiscriminator,
                                CountryGroupId        = countrygrp.CountryGroupId,
                                IsPublished           = countrygrp.IsPublished,
                                AssociatedCountryIds  = doc.GetPropertyValue <List <int> >("AssociatedCountryIds"),
                                CountryGroupContentId = doc.GetPropertyValue <int>("CountryGroupContentId"),
                                CreatedBy             = doc.GetPropertyValue <string>("CreatedBy"),
                                CreatedDate           = doc.GetPropertyValue <DateTime>("CreatedDate"),
                                UpdatedBy             = doc.GetPropertyValue <string>("UpdatedBy"),
                                UpdatedDate           = doc.GetPropertyValue <DateTime>("UpdatedDate"),
                                GroupName             = doc.GetPropertyValue <string>("GroupName"),
                                LanguageId            = doc.GetPropertyValue <int?>("LanguageId"),
                                PartitionKey          = ""
                            };
                            await _Eventcontext.PublishThroughEventBusAsync(eventsource);
                        }
                    }
                }
                else if (request.Operation == "Delete")
                {
                    foreach (var countrygrp in countryGroups)
                    {
                        foreach (var doc in countrygrpDocs.Where(d => d.GetPropertyValue <int>("CountryGroupId") == countrygrp.CountryGroupId))
                        {
                            var countryevent = new CountryGroupCommandEvent()
                            {
                                id            = doc.GetPropertyValue <Guid>("id"),
                                EventType     = ServiceBusEventType.Delete,
                                Discriminator = Constants.CountryGroupsDiscriminator,
                                PartitionKey  = doc.GetPropertyValue <int>("LanguageId").ToString()
                            };
                            await _Eventcontext.PublishThroughEventBusAsync(countryevent);
                        }
                    }
                }
                scope.Complete();
            }
            return(response);
        }
Beispiel #2
0
        public async Task <CreateCountryGroupsCommandResponse> Handle(CreateCountryGroupsCommand request, CancellationToken cancellationToken)
        {
            CreateCountryGroupsCommandResponse response = new CreateCountryGroupsCommandResponse()
            {
                IsSuccessful = false
            };

            CountryGroups _countryGroup = new CountryGroups();

            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                //   List<Languages> _languages = _ResourceGroupRepository.GetAllLanguages();
                _countryGroup.IsPublished = true;
                _countryGroup.CreatedBy   = "";
                _countryGroup.CreatedDate = DateTime.Now;
                _countryGroup.UpdatedBy   = "";
                _countryGroup.UpdatedDate = DateTime.Now;
                foreach (var langName in request.LanguageName)
                {
                    var countryGroupContent = new CountryGroupContents();
                    countryGroupContent.GroupName  = langName.Name.Trim();
                    countryGroupContent.LanguageId = langName.LanguageId;
                    _countryGroup.CountryGroupContents.Add(countryGroupContent);
                }
                foreach (var countryId in request.CountryIds)
                {
                    var countryGroupCountriesContent = new CountryGroupAssociatedCountries();
                    countryGroupCountriesContent.CountryId = countryId;
                    _countryGroup.CountryGroupAssociatedCountries.Add(countryGroupCountriesContent);
                }
                _CountryGroupsRepository.Add(_countryGroup);
                await _CountryGroupsRepository.UnitOfWork
                .SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                foreach (var content in _countryGroup.CountryGroupContents)
                {
                    var eventSourcing = new CountryGroupCommandEvent()
                    {
                        EventType             = ServiceBusEventType.Create,
                        CountryGroupId        = _countryGroup.CountryGroupId,
                        IsPublished           = _countryGroup.IsPublished,
                        CreatedBy             = _countryGroup.CreatedBy,
                        CreatedDate           = _countryGroup.CreatedDate,
                        UpdatedBy             = _countryGroup.UpdatedBy,
                        UpdatedDate           = _countryGroup.UpdatedDate,
                        GroupName             = content.GroupName,
                        CountryGroupContentId = content.CountryGroupContentId,
                        LanguageId            = content.LanguageId,
                        AssociatedCountryIds  = (from cg in _countryGroup.CountryGroupAssociatedCountries where cg != null select cg.CountryId).ToList(),
                        Discriminator         = Constants.CountryGroupsDiscriminator,
                        PartitionKey          = ""
                    };
                    await _Eventcontext.PublishThroughEventBusAsync(eventSourcing);
                }
                scope.Complete();
            }
            return(response);
        }
        public async Task <UpdateCountryGroupsCommandResponse> Handle(UpdateCountryGroupsCommand request, CancellationToken cancellationToken)
        {
            UpdateCountryGroupsCommandResponse response = new UpdateCountryGroupsCommandResponse()
            {
                IsSuccessful = false
            };

            List <int> objresourceGroupId = new List <int>();

            objresourceGroupId.Add(request.CountryGroupsId);
            var countryGroup    = _CountryGroupsRepository.getCountryGroups(objresourceGroupId)[0];
            var contentToDelete = new List <int>();

            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                countryGroup.CreatedBy   = "";
                countryGroup.CreatedDate = DateTime.Now;
                countryGroup.UpdatedBy   = "";
                countryGroup.UpdatedDate = DateTime.Now;

                foreach (var content in request.LanguageName)
                {
                    var countryGroupContents = countryGroup.CountryGroupContents.Where(s => s.LanguageId == content.LanguageId).FirstOrDefault();
                    if (countryGroupContents == null)
                    {
                        CountryGroupContents objresourceGroupContents = new CountryGroupContents();
                        objresourceGroupContents.GroupName  = content.Name;
                        objresourceGroupContents.LanguageId = content.LanguageId;
                        countryGroup.CountryGroupContents.Add(objresourceGroupContents);
                    }
                    else
                    {
                        countryGroupContents.GroupName = content.Name;
                        _CountryGroupsRepository.Update(countryGroupContents);
                    }
                }
                //var countryGroupCountries = countryGroup.CountryGroupAssociatedCountries.Where(s => s.CountryGroupId == request.CountryGroupsId);
                foreach (var content in request.CountryIds)
                {
                    if (countryGroup.CountryGroupAssociatedCountries.Where(s => s.CountryId == content).FirstOrDefault() == null)
                    {
                        CountryGroupAssociatedCountries objcountryGroupCountries = new CountryGroupAssociatedCountries();
                        objcountryGroupCountries.CountryId = content;
                        countryGroup.CountryGroupAssociatedCountries.Add(objcountryGroupCountries);
                    }
                }
                //    List<ResourceGroupContents> ResourceGroupContents = resourceGroup.ResourceGroupContents.Where(s => s.ResourceGroupId == request.ResourceGroupId).ToList();
                foreach (var countryGroupContent in countryGroup.CountryGroupContents.ToList())
                {
                    if (request.LanguageName.Where(s => s.LanguageId == countryGroupContent.LanguageId).Count() == 0)
                    {
                        contentToDelete.Add((int)countryGroupContent.LanguageId);
                        countryGroup.CountryGroupContents.Remove(countryGroupContent);
                        _CountryGroupsRepository.Delete(countryGroupContent);
                    }
                }
                foreach (var countryGroupCountries in countryGroup.CountryGroupAssociatedCountries.ToList())
                {
                    if (request.CountryIds.Where(s => s == countryGroupCountries.CountryId).Count() == 0)
                    {
                        countryGroup.CountryGroupAssociatedCountries.Remove(countryGroupCountries);
                        _CountryGroupsRepository.Delete(countryGroupCountries);
                    }
                }
                countryGroup.UpdatedBy   = "";
                countryGroup.UpdatedDate = DateTime.Now;
                await _CountryGroupsRepository.UnitOfWork
                .SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var countryGroupDocs = _context.GetAll(Constants.CountryGroupsDiscriminator);
                foreach (var content in countryGroup.CountryGroupContents)
                {
                    var doc = countryGroupDocs.FirstOrDefault(d => d.GetPropertyValue <int>("CountryGroupId") == countryGroup.CountryGroupId &&
                                                              d.GetPropertyValue <int?>("LanguageId") == content.LanguageId);
                    var eventSourcing = new CountryGroupCommandEvent()
                    {
                        id = doc != null?doc.GetPropertyValue <Guid>("id") : Guid.NewGuid(),
                                 EventType             = doc != null ? ServiceBusEventType.Update : ServiceBusEventType.Create,
                                 CountryGroupId        = countryGroup.CountryGroupId,
                                 IsPublished           = countryGroup.IsPublished,
                                 CreatedBy             = countryGroup.CreatedBy,
                                 CreatedDate           = countryGroup.CreatedDate,
                                 UpdatedBy             = countryGroup.UpdatedBy,
                                 UpdatedDate           = countryGroup.UpdatedDate,
                                 GroupName             = content.GroupName,
                                 CountryGroupContentId = content.CountryGroupContentId,
                                 LanguageId            = content.LanguageId,
                                 AssociatedCountryIds  = (from cg in countryGroup.CountryGroupAssociatedCountries where cg != null select cg.CountryId).ToList(),
                                 Discriminator         = Constants.CountryGroupsDiscriminator,
                                 PartitionKey          = ""
                    };
                    await _Eventcontext.PublishThroughEventBusAsync(eventSourcing);
                }
                foreach (int i in contentToDelete)
                {
                    var deleteEvt = new CountryGroupCommandEvent()
                    {
                        id = countryGroupDocs.FirstOrDefault(d => d.GetPropertyValue <int>("CountryGroupId") == countryGroup.CountryGroupId &&
                                                             d.GetPropertyValue <int>("LanguageId") == i).GetPropertyValue <Guid>("id"),
                        EventType     = ServiceBusEventType.Delete,
                        Discriminator = Constants.CountryGroupsDiscriminator,
                        PartitionKey  = ""
                    };
                    await _Eventcontext.PublishThroughEventBusAsync(deleteEvt);
                }
                scope.Complete();
            }
            return(response);
        }