예제 #1
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);
        }