public IActionResult DeleteProduct(int id) { var existingProduct = this.context.ProposedClientProducts.Where(b => b.RecId == id).FirstOrDefault(); if (existingProduct == null) { return(NotFound()); } else { var funds = this.context.ProposedClientFunds.Where(b => b.HeaderId == id); this.context.ProposedClientFunds.RemoveRange(funds); RopcurrentProducts currentProduct = this.context.RopcurrentProducts.Where(r => r.ProposedProduct == id).FirstOrDefault(); if (currentProduct != null) { var currentFunds = this.context.RopcurrentFunds.Where(f => f.HeaderId == currentProduct.RecId); this.context.RopcurrentFunds.RemoveRange(currentFunds); this.context.RopcurrentProducts.Remove(currentProduct); } AlternativeClientProducts altProduct = this.context.AlternativeClientProducts.Where(r => r.ProposedProduct == id).FirstOrDefault(); if (altProduct != null) { var altFunds = this.context.AlternativeClientFunds.Where(f => f.HeaderId == altProduct.RecId); this.context.AlternativeClientFunds.RemoveRange(altFunds); this.context.AlternativeClientProducts.Remove(altProduct); } var replacement = this.context.ProductReplacement.Where(p => p.ProposedId == id); if (replacement != null) { this.context.RemoveRange(replacement); } this.context.ProposedClientProducts.Remove(existingProduct); this.context.SaveChanges(); return(Ok(id)); } }
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; } } }