public void Edit(EditLicenseeData data) { using (var scope = CustomTransactionScope.GetTransactionScope()) { var validationResult = _licenseeQueries.ValidateCanEdit(data); if (!validationResult.IsValid) { throw new RegoValidationException(validationResult); } var licensee = _repository.Licensees .Include(x => x.Products) .Include(x => x.Currencies) .Include(x => x.Countries) .Include(x => x.Cultures) .Include(x => x.Contracts) .Single(x => x.Id == data.Id); licensee.Name = data.Name; licensee.CompanyName = data.CompanyName; licensee.AffiliateSystem = data.AffiliateSystem; licensee.ContractStart = data.ContractStart; licensee.ContractEnd = data.ContractEnd; licensee.Email = data.Email; licensee.AllowedBrandCount = data.BrandCount; licensee.AllowedWebsiteCount = data.WebsiteCount; licensee.TimezoneId = data.TimeZoneId; licensee.DateUpdated = DateTimeOffset.UtcNow; licensee.UpdatedBy = _actorInfoProvider.Actor.UserName; licensee.Remarks = data.Remarks; var currentContract = licensee.Contracts.Single(x => x.IsCurrentContract); currentContract.StartDate = data.ContractStart; currentContract.EndDate = data.ContractEnd; licensee.Products.Clear(); EnumerableExtensions.ForEach(data.Products, x => licensee.Products.Add(new LicenseeProduct { Licensee = licensee, ProductId = new Guid(x) })); licensee.Currencies.Clear(); EnumerableExtensions.ForEach(data.Currencies, x => licensee.Currencies.Add(_repository.Currencies.Single(y => y.Code == x))); licensee.Countries.Clear(); EnumerableExtensions.ForEach(data.Countries, x => licensee.Countries.Add(_repository.Countries.Single(y => y.Code == x))); licensee.Cultures.Clear(); EnumerableExtensions.ForEach(data.Languages, x => licensee.Cultures.Add(_repository.Cultures.Single(y => y.Code == x))); _repository.SaveChanges(); _eventBus.Publish(new LicenseeUpdated(licensee)); scope.Complete(); } }
public ValidationResult ValidateCanEdit(EditLicenseeData data) { return(new EditLicenseeValidator(_repository).Validate(data)); }