private WalletTemplate CreateWalletTemplate( Guid brandId, string name, IEnumerable <Guid> productIds, bool isMain) { var brand = _repository.Brands.Single(x => x.Id == brandId); var walletTemplate = new WalletTemplate { Id = Guid.NewGuid(), DateCreated = DateTime.UtcNow, CreatedBy = _actorInfoProvider.Actor.Id, Name = name, CurrencyCode = brand.DefaultCurrency, IsMain = isMain }; foreach (var productId in productIds) { var walletProduct = new WalletTemplateProduct { Id = Guid.NewGuid(), ProductId = productId }; walletTemplate.WalletTemplateProducts.Add(walletProduct); } brand.WalletTemplates.Add(walletTemplate); return(walletTemplate); }
private void UpdateWalletTemplate(Guid brandId, WalletViewModel walletModel) { var brand = _repository.Brands.Single(x => x.Id == brandId); var wallet = _repository.Brands .Include(x => x.WalletTemplates.Select(wt => wt.WalletTemplateProducts)) .Single(x => x.Id == brandId) .WalletTemplates .Single(wt => wt.Id == walletModel.Id); wallet.DateUpdated = DateTimeOffset.Now.ToBrandOffset(brand.TimezoneId); wallet.UpdatedBy = _actorInfoProvider.Actor.Id; wallet.Name = walletModel.Name; var walletTemplateProductsToRemove = wallet .WalletTemplateProducts .Where(x => !walletModel.ProductIds.Contains(x.ProductId)) .ToList(); var walletTemplateProductsToAdd = walletModel .ProductIds .Where(x => !wallet.WalletTemplateProducts.Select(y => y.ProductId).Contains(x)) .ToList(); if (walletTemplateProductsToRemove.Any() && wallet.Brand.Status == BrandStatus.Active) { throw new RegoValidationException("It is not allowed to change product wallets for active brands."); } walletTemplateProductsToRemove.ForEach(product => wallet.WalletTemplateProducts.Remove(product)); walletTemplateProductsToAdd.ForEach(productId => { var walletTemplateProduct = new WalletTemplateProduct { Id = Guid.NewGuid(), WalletTemplateId = wallet.Id, WalletTemplate = wallet, ProductId = productId }; wallet.WalletTemplateProducts.Add(walletTemplateProduct); }); }