public IHttpActionResult PostEndUser(AssignedProductData[] selectedProducts) { var e = User.Identity.Name; if (e == null) { return null; } var endUserToUpdate = db.EndUser .Include(i => i.ProductNews) .Where(i => i.Email == e) .Single(); try { UpdateEndUserProducts(selectedProducts, endUserToUpdate); db.SaveChanges(); } catch (Exception ) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } PopulateAssignedProductData(endUserToUpdate); return Ok(endUserToUpdate); }
public IHttpActionResult PostEndUser(AssignedProductData[] selectedProducts) { var e = User.Identity.Name; if (e == null) { return null; } var endUserToUpdate = db.EndUser .Include(i => i.ProductNews) .Where(i => i.Email == e) .Single(); try { UpdateEndUserProducts(selectedProducts, endUserToUpdate); db.SaveChanges(); // return null; } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } PopulateAssignedProductData(endUserToUpdate); return Ok(endUserToUpdate); }
public IHttpActionResult PostProductNew(AssignedProductData productnew) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var e = User.Identity.Name; ProductOwner po = db.ProductOwner.FirstOrDefault(d => d.Email == e); ProductNew pn = new ProductNew() { ProductName = productnew.ProductName, ProductOwner_ID = po.ID }; db.ProductNew.Add(pn); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = productnew.ProductID }, productnew); }
////original // if (!ModelState.IsValid) // { // return BadRequest(ModelState); // } // db.EveryBody.Add(enduser); // db.SaveChanges(); // return CreatedAtRoute("DefaultApi", new { id = enduser.ID }, enduser); private void UpdateEndUserProducts(AssignedProductData[] selectedProducts, EndUser endUserToUpdate) { if (selectedProducts == null) { endUserToUpdate.ProductNews = new List<ProductNew>(); return; } var selectedProductsHS = new HashSet<int>(selectedProducts.Where(i=>i.Assigned).Select(i=>i.ProductID)); // Select(c=>c.ProductID)); var endUserProducts = new HashSet<int>(endUserToUpdate.ProductNews.Select(c => c.ProductID)); foreach (var product in db.ProductNew) { if (selectedProductsHS.Contains(product.ProductID)) { if (!endUserProducts.Contains(product.ProductID)) { endUserToUpdate.ProductNews.Add(product); } } else { if (endUserProducts.Contains(product.ProductID)) { endUserToUpdate.ProductNews.Remove(product); } } } }