Exemplo n.º 1
0
        public void AssignBrandCountry(AssignBrandCountryRequest assignBrandCountryRequest)
        {
            using (var scope = CustomTransactionScope.GetTransactionScope())
            {
                var validationResult = new AssignBrandCountryValidator(_repository).Validate(assignBrandCountryRequest);

                if (!validationResult.IsValid)
                {
                    throw new RegoValidationException(validationResult);
                }

                var brand = _repository.Brands
                            .Include(x => x.BrandCountries.Select(y => y.Country))
                            .Single(x => x.Id == assignBrandCountryRequest.Brand);

                var oldCountries = brand.BrandCountries
                                   .Where(x => !assignBrandCountryRequest.Countries.Contains(x.CountryCode))
                                   .ToArray();

                foreach (var oldCountry in oldCountries)
                {
                    brand.BrandCountries.Remove(oldCountry);
                }

                var newCountries = assignBrandCountryRequest.Countries
                                   .Where(x => brand.BrandCountries.All(y => y.CountryCode != x))
                                   .ToArray();

                foreach (var country in newCountries)
                {
                    var countryToAdd = _repository.Countries.Single(x => x.Code == country);

                    brand.BrandCountries.Add(new BrandCountry
                    {
                        BrandId     = brand.Id,
                        Brand       = brand,
                        CountryCode = countryToAdd.Code,
                        Country     = countryToAdd,
                        DateAdded   = DateTimeOffset.Now.ToBrandOffset(brand.TimezoneId),
                        AddedBy     = _actorInfoProvider.Actor.UserName
                    });
                }

                brand.DateUpdated = DateTimeOffset.Now.ToBrandOffset(brand.TimezoneId);
                brand.UpdatedBy   = _actorInfoProvider.Actor.UserName;

                _repository.SaveChanges();

                _eventBus.Publish(new BrandCountriesAssigned(brand)
                {
                    EventCreated = DateTimeOffset.Now.ToBrandOffset(brand.TimezoneId),
                });

                scope.Complete();
            }
        }
Exemplo n.º 2
0
        public ValidationResult ValidateThatBrandCountryCanBeAssigned(AssignBrandCountryRequest request)
        {
            var validator = new AssignBrandCountryValidator(_repository);

            return(validator.Validate(request));
        }