public async Task <IActionResult> DeleteIngrInShoppingList(ApiIngrInShoppingList ingredient) { var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (ingredient.UserId != currentUserId) { return(Unauthorized()); } var deletedIngredient = ingredient.ToIngrInShoppingList(); _context.Entry(deletedIngredient).State = EntityState.Deleted; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!IngrInShoppingListExists(deletedIngredient)) { return(NotFound()); } throw; } return(NoContent()); }
public async Task <IActionResult> PutIngrInShoppingList(ApiIngrInShoppingList apiIngredient) { var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (apiIngredient.UserId != currentUserId) { return(Unauthorized()); } var query = from ing in _context.IngrInShoppingList where ing.IngrName == apiIngredient.IngrName select ing; foreach (var ingrInShoppingList in query) { _context.Entry(ingrInShoppingList).State = EntityState.Deleted; } var updatedIngredient = apiIngredient.ToIngrInShoppingList(); await _context.IngrInShoppingList.AddAsync(updatedIngredient); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!IngrInShoppingListExists(updatedIngredient)) { return(NotFound()); } throw; } return(NoContent()); }
public async Task <ActionResult <ApiIngrInShoppingList> > PostIngrInShoppingList( ApiIngrInShoppingList apiIngredient) { var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (apiIngredient.UserId != currentUserId) { return(Unauthorized()); } var shoppingList = await _context.ShoppingList.FindAsync(currentUserId); if (shoppingList == null) { var defaultShoppingList = new ShoppingList { Name = "My Shopping List", LastUpdate = DateTime.Now, UserId = currentUserId, NumIngredients = 0 }; await _context.AddAsync(defaultShoppingList); } var ingredient = apiIngredient.ToIngrInShoppingList(); _context.IngrInShoppingList.Add(ingredient); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (IngrInShoppingListExists(ingredient)) { return(Conflict()); } throw; } return(Created("", apiIngredient)); }