public IActionResult Rebalance(int id, int currentId, [FromBody] ProposedClientProductsViewModel _product) { if (ModelState.IsValid) { var existingProduct = this.context.ProposedClientProducts.Where(b => b.RecId == id).FirstOrDefault(); if (existingProduct == null) { CurrentClientProducts prod = this.context.CurrentClientProducts.Where(a => a.RecId == currentId).FirstOrDefault(); ProposedClientProducts proposedProduct = new ProposedClientProducts(); proposedProduct.RecId = id; proposedProduct.ProductId = prod.ProductId; proposedProduct.Owner = prod.Owner; proposedProduct.ClientId = prod.ClientId; proposedProduct.Percentage = prod.Percentage; proposedProduct.Value = prod.UnutilizedValue; proposedProduct.Status = 1; this.context.ProposedClientProducts.Add(proposedProduct); //this.context.SaveChanges(); // newId = proposedProduct.RecId; ProductReplacement prReplacement = new ProductReplacement(); prReplacement.CurrentId = currentId; prReplacement.ProposedId = id; this.context.ProductReplacement.Add(prReplacement); this.context.SaveChanges(); var res = this.context.Product.Where(b => b.ProductId == proposedProduct.ProductId).FirstOrDefault(); var result = this.mapper.Map <ProposedClientProductsViewModel>(proposedProduct); result.Product = res.ProductName; return(Ok(result)); } else { this.mapper.Map <ProposedClientProductsViewModel, ProposedClientProducts>(_product, existingProduct); this.context.ProposedClientProducts.Update(existingProduct); this.context.SaveChanges(); var res = this.context.Product.Where(b => b.ProductId == _product.ProductId).FirstOrDefault(); var result = this.mapper.Map <ProposedClientProductsViewModel>(_product); result.Product = res.ProductName; return(Ok(result)); } } else { return(BadRequest(ModelState)); } }
public IActionResult UpdateProduct(int clientId, [FromBody] ProposedClientProductsViewModel _product) { using (var dbContextTransaction = context.Database.BeginTransaction()) { if (ModelState.IsValid) { var existingProduct = this.context.ProposedClientProducts.Where(b => b.RecId == _product.RecId).FirstOrDefault(); if (existingProduct == null) { var product = this.mapper.Map <ProposedClientProducts>(_product); // product.RecId = id; product.Status = 1; product.ClientId = clientId; product.Owner = product.Owner; this.context.ProposedClientProducts.Add(product); // this.context.SaveChanges(); // int newId = product.RecId; this.context.SaveChanges(); dbContextTransaction.Commit(); var res = this.context.Product.Where(b => b.ProductId == product.ProductId).FirstOrDefault(); // var result = this.mapper.Map<ProposedClientProductsViewModel>(product); var result = mapper.Map <ProposedClientProductsViewModel>(from s in context.ProposedClientProducts.AsEnumerable() join sa in context.Product.AsEnumerable() on s.ProductId equals sa.ProductId join p in context.Platform.AsEnumerable() on sa.PlatformId equals p.PlatformId where s.RecId == product.RecId select new ProposedClientProductsViewModel { RecId = s.RecId, ProductId = s.ProductId, ClientId = s.ClientId, Owner = s.Owner, Value = s.Value, Percentage = s.Percentage, Product = sa.ProductName, Status = s.Status, isEqual = 1, CurrentId = 0, PlatformName = p.PlatformName, PlatformId = p.PlatformId }); result.Product = res.ProductName; return(Ok(result)); } else { this.mapper.Map <ProposedClientProductsViewModel, ProposedClientProducts>(_product, existingProduct); this.context.ProposedClientProducts.Update(existingProduct); this.context.SaveChanges(); IEnumerable <ProposedClientFundsViewModel> list = this.mapper.Map <IEnumerable <ProposedClientFundsViewModel> >(this.context.ProposedClientFunds.AsEnumerable().Where(m => (m.HeaderId == existingProduct.RecId))); foreach (ProposedClientFundsViewModel fund in list) { if (fund != null) { var existingFund = this.context.ProposedClientFunds.Where(b => b.RecId == fund.RecId).FirstOrDefault(); if (fund.Percentage != 0) { fund.Value = existingProduct.Value * (fund.Percentage / 100); } this.mapper.Map <ProposedClientFundsViewModel, ProposedClientFunds>(fund, existingFund); this.context.ProposedClientFunds.Update(existingFund); } } this.context.SaveChanges(); dbContextTransaction.Commit(); var result = this.mapper.Map <ProposedClientProductsViewModel>(_product); return(Ok(result)); } } else { return(BadRequest(ModelState)); } } }
public IActionResult Retain(int id, [FromBody] ProposedClientProductsViewModel _product) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { if (ModelState.IsValid) { var existingProduct = this.context.ProposedClientProducts.Where(b => b.ProductId == _product.ProductId && b.RecId == _product.RecId).FirstOrDefault(); if (existingProduct == null) { var product = this.mapper.Map <ProposedClientProducts>(_product); product.Status = 0; this.context.ProposedClientProducts.Add(product); this.context.SaveChanges(); var header = this.mapper.Map <ProposedClientProductsViewModel>(product); //ProductReplacement prReplacement = new ProductReplacement(); //prReplacement.CurrentId = id; //prReplacement.ProposedId = _product.RecId; //this.context.ProductReplacement.Add(prReplacement); IEnumerable <CurrentClientFundsViewModel> list = this.mapper.Map <IEnumerable <CurrentClientFundsViewModel> >(this.context.CurrentClientFunds.AsEnumerable().Where(m => (m.HeaderId == id))); foreach (CurrentClientFundsViewModel fund in list) { if (fund != null) { ProposedClientFunds pf = new ProposedClientFunds(); pf.HeaderId = _product.RecId; pf.Apircode = fund.Apircode; if (_product.isEqual == 1) { pf.Value = fund.Value; pf.Percentage = fund.Percentage; } else { pf.Value = 0; pf.Percentage = 0; } var pfDetails = this.mapper.Map <ProposedClientFunds>(pf); this.context.ProposedClientFunds.Add(pfDetails); } } RopcurrentProducts currentProduct = new RopcurrentProducts(); currentProduct.RecId = _product.CurrentId; currentProduct.Owner = header.Owner; currentProduct.Percentage = header.Percentage; currentProduct.ProductId = header.ProductId; currentProduct.Value = header.Value; currentProduct.ProposedProduct = header.RecId; currentProduct.OriginalProduct = id; currentProduct.ClientId = header.ClientId; this.context.RopcurrentProducts.Add(currentProduct); this.context.SaveChanges(); var currentHeader = this.mapper.Map <RopcurrentProductsViewModel>(currentProduct); foreach (CurrentClientFundsViewModel fund in list) { if (fund != null) { RopcurrentFunds pf = new RopcurrentFunds(); pf.HeaderId = currentHeader.RecId; pf.Apircode = fund.Apircode; if (_product.isEqual == 1) { pf.Value = fund.Value; pf.Percentage = fund.Percentage; } else { pf.Value = 0; pf.Percentage = 0; } var pfDetails = this.mapper.Map <RopcurrentFunds>(pf); this.context.RopcurrentFunds.Add(pfDetails); } } this.context.SaveChanges(); dbContextTransaction.Commit(); var result = this.mapper.Map <ProposedClientProductsViewModel>(product); result.RopCurrentId = currentHeader.RecId; return(Ok(result)); } else { this.mapper.Map <ProposedClientProductsViewModel, ProposedClientProducts>(_product, existingProduct); this.context.ProposedClientProducts.Update(existingProduct); this.context.SaveChanges(); dbContextTransaction.Commit(); var result = this.mapper.Map <ProposedClientProductsViewModel>(_product); return(Ok(result)); } } else { return(BadRequest(ModelState)); } } catch (Exception ex) { throw ex; } } }