/*public override Task OnConnectedAsync() * { * _userId = Context.UserIdentifier; * Console.WriteLine(_userId); * * return base.OnConnectedAsync(); * }*/ public async Task UpdateIngredientOwned(string stringId, string stringOwned) { // await Clients.All.SendAsync("UpdateIngredientOwned", user, id, owned); _userId = Context.User.FindFirstValue(ClaimTypes.NameIdentifier); try { var id = Int32.Parse(stringId); var owned = Boolean.Parse(stringOwned); // System.Console.WriteLine(_userId + ", " + id + ", " + owned); // Console.WriteLine(userId); // Console.WriteLine("User id: " + _userId); Ingredient ingredient = await _context.Ingredient.FirstOrDefaultAsync(m => m.IngredientID == id && m.User == _userId); ingredient.Owned = owned; _context.Attach(ingredient).State = EntityState.Modified; await _context.SaveChangesAsync(); } catch (FormatException) { _logger.LogError("Unable to mark ingredient id " + stringId + " as owned. Unable to parse id."); } catch (DbUpdateConcurrencyException) { _logger.LogError("Error trying to access database for ingredient id " + stringId); } }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(item).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!_dbUtil.IngredientExists(_userId, item.IngredientID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var ingredient = await _context.Ingredient .AsNoTracking() .FirstOrDefaultAsync(m => m.IngredientID == id && m.User == _userId); if (ingredient == null) { return(NotFound()); } try { _context.Ingredient.Remove(ingredient); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) return(RedirectToAction("./Delete", new { id, saveChangesError = true })); } }
public async Task <IActionResult> OnPostAsync(int?id) { if (!ModelState.IsValid) { return(Page()); } var recipeToUpdate = await _context.Recipe.FirstOrDefaultAsync(r => r.RecipeID == id && r.User == _userId); recipeToUpdate.UpdatedDate = DateTime.Now; // Console.WriteLine(recipeToUpdate); if (await TryUpdateModelAsync <Recipe>( recipeToUpdate, "recipe", r => r.Name, r => r.Instructions, r => r.Description, r => r.Favorite, r => r.Rating, r => r.Price)) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (!ModelState.IsValid) { return(Page()); } var ingredientToUpdate = await _context.Ingredient.FirstOrDefaultAsync(i => i.IngredientID == id && i.User == _userId); Console.WriteLine(ingredientToUpdate); if (await TryUpdateModelAsync <Ingredient>( ingredientToUpdate, "ingredient", i => i.Name, i => i.Favorite, i => i.PurchaseDate, i => i.Owned, i => i.Notes)) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(Page()); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var emptyRecipe = new Recipe { User = _userId, UpdatedDate = DateTime.Now, AddedDate = DateTime.Now }; if (await TryUpdateModelAsync <Recipe>( emptyRecipe, "recipe", // Prefix for form value. r => r.Name, r => r.Instructions, r => r.Description, r => r.Rating, r => r.Price)) { _context.Recipe.Add(emptyRecipe); try { await _context.SaveChangesAsync(); } catch (Exception e) { return(Page()); } finally { _context.ChangeTracker.AutoDetectChangesEnabled = true; } return(RedirectToPage("./Index")); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var emptyIngredient = new Ingredient { User = _userId }; if (await TryUpdateModelAsync <Ingredient>( emptyIngredient, "ingredient", // Prefix for form value. i => i.Name, i => i.Favorite, i => i.Owned, i => i.PurchaseDate, i => i.Notes)) { _context.Ingredient.Add(emptyIngredient); try { await _context.SaveChangesAsync(); } catch (Exception e) { return(Page()); } finally { _context.ChangeTracker.AutoDetectChangesEnabled = true; } return(RedirectToPage("./Index")); } return(RedirectToPage("./Index")); }