public async Task <IActionResult> ChangeState(int id) { if (id != 0) { var customerState = ""; var resultsCustomerID = await _customerShoppingCartContext.Customers.FindAsync(id); if (resultsCustomerID == null) { return(new NotFoundObjectResult($"No Customer with id = {id} was found in the database")); } if (resultsCustomerID.IsActive == true) { resultsCustomerID.IsActive = false; customerState = "InActive"; } else { resultsCustomerID.IsActive = true; customerState = "Active"; } //To solve the problem of tracking entity. Make sure entity is in stable state _dataBaseChanges.Update(resultsCustomerID); await _dataBaseChanges.CommitAsync(); _customerShoppingCartContext.Entry(resultsCustomerID).State = EntityState.Detached; return(new OkObjectResult($"Customer with id = {id} is now in {customerState} state")); } return(new BadRequestObjectResult("Customer cannot be null")); }
/// <summary> /// /// </summary> /// <param name="shoppingCart"></param> /// <returns></returns> public async Task <ShoppingCart> UpdateItem(ShoppingCart shoppingCart) { try { var results = await _context.ShoppingCarts.FindAsync(shoppingCart.Id); if (results == null) { return(new ShoppingCart { Message = $"User with Id: {shoppingCart.Id} does not exist in shopping cart" }); } //To solve the problem of tracking entity. Make sure entity is in stable state _context.Entry(results).State = EntityState.Detached; _dataBaseChanges.Update(shoppingCart); await _dataBaseChanges.CommitAsync(); } catch (Exception ex) { _logger.LogError($"An error occured during getting item from DB => {ex.InnerException}"); } return(new ShoppingCart { Message = $"Successfully updated user with Id:{shoppingCart.Id}" }); }