public IActionResult PostDish([FromBody] PostDishDTO dishDTO) { _requestLogService.SaveRequest(User.Identity.GetUserId(), "POST", "api/v1/dishes", "PostDish"); if (!ModelState.IsValid) { return(BadRequest("Invalid fields provided, please double check the parameters")); } if (dishDTO.RestaurantId.Equals(null)) { return(BadRequest("Dish is not related any Restaurant")); } if (!IsRestaurantUserOrAdmin(dishDTO.RestaurantId)) { return(StatusCode(403, "New dish can only be added by admin or by restaurant user")); } if (!(User.IsInRole("premiumUser") && !User.IsInRole("admin")) && dishDTO.PromotionId != null) { return(BadRequest("New dish with promotion can only be added by admin or premium user")); } var newDish = _dishService.AddNewDish(dishDTO, User.Identity.GetUserId()); return(CreatedAtAction(nameof(GetDish), new { id = newDish.DishId }, newDish)); }
public async Task <IActionResult> Index(NewDishViewModel model) { if (!ModelState.IsValid) { var serializedValue = (HttpContext.Session.GetString("ingredients")); var produktList = JsonConvert.DeserializeObject <List <Produkt> >(serializedValue); model.IngredientViewModel = new AddIngredientViewModel { Ingredients = produktList }; var categories = await _dishService.GetDishCategoriesAsync(); model.FoodTypeSelectList = new SelectList(categories, "MatrattTyp1", "Beskrivning"); model.IngredientViewModel.IngredientsList = await _dishService.GetDishIngredientsAsync(); return(View(model)); } // Creating the new Dish and store it to the DB. var dish = new Matratt { MatrattNamn = model.Name.ToFirstLetterUpper(), Beskrivning = model.Description.ToFirstLetterUpper(), MatrattTyp = model.FoodType, Pris = model.Price, }; dish = _dishService.AddNewDish(dish); // Get the selected ingredients from the session var value = (HttpContext.Session.GetString("ingredients")); var ingredientsList = JsonConvert.DeserializeObject <List <Produkt> >(value); // Attach them to the dish var dishIngredients = new List <MatrattProdukt>(); foreach (var ingredient in ingredientsList) { dishIngredients.Add(new MatrattProdukt { MatrattId = dish.MatrattId, ProduktId = ingredient.ProduktId }); } // Add the list of MattrattProdukter to the property of the Matratt, save changes. dish.MatrattProdukt = dishIngredients; await _dishService.UpdateDishAsync(dish); return(RedirectToAction("AdminPage", "Admin")); }