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 <IActionResult> PutRecipeRating(string userId, int RecipeId, ApiRecipeRating apiObj) { //convert input to database object var updatedRating = new RecipeRating() { UserId = userId, RecipeId = RecipeId, Rating = apiObj.Rating }; //notifier to entity framework core _context.Entry(updatedRating).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RecipeRatingExists(updatedRating)) { return(NotFound()); } throw; } return(NoContent()); }
public async Task <IActionResult> PutRecipe(int id, Recipe apiRecipe) { var userId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (userId != apiRecipe.AuthorId) { return(Unauthorized()); } if (id != apiRecipe.Id) { return(BadRequest()); } _context.Entry(apiRecipe).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RecipeExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutSupplies(string userId, QuantifiedIngredient quantifiedIngredient) { var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (userId != currentUserId) { return(Unauthorized()); } var query = from s in _context.Supplies where s.IngrName == quantifiedIngredient.Ingredient.Name select s; foreach (var s in query) { _context.Entry(s).State = EntityState.Deleted; } var updatedSupplies = new Supplies() { Quantity = quantifiedIngredient.Quantity, UnitName = quantifiedIngredient.Unit.Name, IngrName = quantifiedIngredient.Ingredient.Name, UserId = userId }; await _context.Supplies.AddAsync(updatedSupplies); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SuppliesExists(updatedSupplies)) { return(NotFound()); } throw; } return(NoContent()); }
public async Task <IActionResult> PutApiUses(ApiUses apiUses) { var query = from u in _context.Uses where u.IngrName == apiUses.IngrName select u; foreach (var u in query) { _context.Entry(u).State = EntityState.Deleted; } Uses uses = new Uses { RecipeId = apiUses.RecipeId, IngrName = apiUses.IngrName, Quantity = apiUses.Quantity, UnitName = apiUses.UnitName }; await _context.Uses.AddAsync(uses); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException e) { System.Console.WriteLine(e.Message); if (!ApiUsesExists(apiUses.RecipeId, apiUses.UnitName, apiUses.IngrName)) { System.Console.WriteLine(apiUses.RecipeId); System.Console.WriteLine(apiUses.UnitName); System.Console.WriteLine(apiUses.IngrName); return(NotFound()); } throw; } return(NoContent()); }
public async Task <IActionResult> PutSupplier(string id, ApiSupplier apiSupplier) { var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (id != currentUserId) { return(Unauthorized()); } if (id != apiSupplier.UserId) { return(BadRequest()); } var supplier = new Supplier() { Email = apiSupplier.Email, Website = apiSupplier.Website, PhoneNo = apiSupplier.PhoneNo, SupplierName = apiSupplier.SupplierName, UserId = apiSupplier.UserId, StoreVisitCount = apiSupplier.StoreVisitCount }; _context.Entry(supplier).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SupplierExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutCustomer(string id, ApiCustomer apiCustomer) { var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (id != currentUserId) { return(Unauthorized()); } if (id != apiCustomer.UserId) { return(BadRequest()); } var customer = new Customer() { Age = apiCustomer.Age, Weight = apiCustomer.Weight, FavMeal = apiCustomer.FavMeal, UserId = apiCustomer.UserId, CustomerName = apiCustomer.CustomerName }; _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } throw; } return(NoContent()); }