public async Task <JsonResult> Salads() { string emailAddress = HttpContext.User.Identity.Name; var restaurant = await _context.Restaurant .Where(rest => rest.EmailAddress.Equals(emailAddress)) .FirstOrDefaultAsync(); List <SaladInfo> saladsToReturn = new List <SaladInfo>(); List <Salad> restaurantSalads = await _context.Salad .Where(d => d.RestaurantId == restaurant.Id) .ToListAsync(); foreach (Salad salad in restaurantSalads) { SaladInfo saladInfo = new SaladInfo(salad.Name, salad.Price, salad.Description, salad.HouseSpecial, salad.Topping); saladsToReturn.Add(saladInfo); } return(new JsonResult(saladsToReturn)); }
public async Task <IActionResult> EditSalad([FromBody] SaladInfo saladInfo) { string emailAddress = HttpContext.User.Identity.Name; var restaurant = await _context.Restaurant .Where(rest => rest.EmailAddress.Equals(emailAddress)) .FirstOrDefaultAsync(); string saladName = Request.Headers["saladName"]; Salad saladToUpdate = await _context.Salad .Where(d => d.Name.Equals(saladName)) .FirstOrDefaultAsync(); saladToUpdate.Name = saladInfo.Name; saladToUpdate.Price = saladInfo.Price; saladToUpdate.Description = saladInfo.Description; saladToUpdate.HouseSpecial = saladInfo.HouseSpecial; saladToUpdate.Topping = saladInfo.Topping; if (ModelState.IsValid) { try { _context.Update(saladToUpdate); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } } HttpContext.Response.StatusCode = 200; return(new JsonResult("Salad successfully updated")); }
//[ValidateAntiForgeryToken] public async Task <JsonResult> CreateSalad([FromBody] SaladInfo saladInfo) { string emailAddress = HttpContext.User.Identity.Name; var restaurant = await _context.Restaurant .Where(rest => rest.EmailAddress.Equals(emailAddress)) .FirstOrDefaultAsync(); if (checkSaladNameInSameRestaurant(saladInfo.Name, restaurant.Id)) { HttpContext.Response.StatusCode = 400; return(new JsonResult("Salad " + saladInfo.Name + " already exists in restaurant")); } Salad newSalad = new Salad { Name = saladInfo.Name, Description = saladInfo.Description, Price = saladInfo.Price, HouseSpecial = saladInfo.HouseSpecial, Topping = saladInfo.Topping }; if (ModelState.IsValid) { newSalad.RestaurantId = restaurant.Id; _context.Add(newSalad); await _context.SaveChangesAsync(); //return RedirectToAction(nameof(Index), new { id = id }); } HttpContext.Response.StatusCode = 200; return(new JsonResult("Salad successfully created")); }