public async Task <IActionResult> AddAsync(AddMealBindingModel bindingModel) { var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value); var result = await _mealService.AddAsync(bindingModel, userId); if (result.ErrorOccurred) { return(BadRequest(result)); } return(Ok(result)); }
public IHttpActionResult AddMeal(AddMealBindingModel model) { if (model == null) { return(this.BadRequest("Model cannot be null")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var restaurant = this.Data.Restaurants.Find(model.RestaurantId); if (restaurant == null) { return(this.BadRequest("Invalid restaurant id: " + model.RestaurantId)); } var loggerUserId = this.User.Identity.GetUserId(); if (restaurant.OwnerId != loggerUserId) { return(this.Unauthorized()); } var type = this.Data.MealTypes.Find(model.TypeId); if (type == null) { return(this.BadRequest("Invalid meal type id: " + model.TypeId)); } var meal = new Meal() { Name = model.Name, Price = model.Price, RestaurantId = model.RestaurantId, TypeId = model.TypeId }; this.Data.Meals.Add(meal); this.Data.SaveChanges(); var mealData = this.Data.Meals.All() .Where(m => m.Id == meal.Id) .Select(MealViewModel.Create) .FirstOrDefault(); return(this.CreatedAtRoute("DefaultApi", new { controller = "meals", id = meal.Id }, mealData)); }
public async Task <IActionResult> AddMeal(AddMealBindingModel model) { if (this.ModelState.IsValid) { await this.diaryService.AddMeal(model, this.User.Identity.Name); return(RedirectToAction("Index")); } var foodItemNames = this.contentService.GetFoodItemsNames(); var recipesNames = this.contentService.GetRecipesNames(); this.ViewData["FoodItemsNames"] = foodItemNames.ToList(); this.ViewData["RecipesNames"] = recipesNames.ToList(); return(View(model)); }
public async Task <Response <AddMealDto> > AddAsync(AddMealBindingModel bindingModel, int userId) { var response = new Response <AddMealDto>(); var mealProductsTmp = new List <MealProducts>(); var user = await _userRepository.GetByAsync(x => x.Id == userId); if (user == null) { response.AddError(Key.User, Error.NotExist); return(response); } var meal = _mapper.Map <Meal>(bindingModel); meal.User = user; meal.UserId = userId; var mealAddSuccess = await _mealRepository.AddAsync(meal); //Dodanie posilku bez produktow if (!mealAddSuccess) { response.AddError(Key.Meal, Error.AddError); return(response); } //Dla kazdego podanego id produktu dodaj wpis w mealProducts foreach (var productId in bindingModel.ProductsIds) { var mealProduct = new MealProducts() { MealId = meal.Id, ProductId = productId, UserId = userId }; var addMealProductSuccess = await _mealProductsRepository.AddAsync(mealProduct); if (!addMealProductSuccess) { response.AddError(Key.MealProducts, Error.AddError); return(response); } mealProductsTmp.Add(mealProduct); } meal.MealProducts = mealProductsTmp; var mealDto = _mapper.Map <AddMealDto>(meal); mealDto.Products = new List <DetailsProductDto>(); //Pobranie produktów z tab Product na podstawie id zapisanych w junctionTable foreach (var mealProduct in meal.MealProducts) { var productResponse = await _productService.GetAsync(mealProduct.ProductId); if (productResponse.ErrorOccurred) { response.AddError(Key.Product, Error.NotExist); return(response); } mealDto.Products.Add(productResponse.SuccessResult); } //Zliczalnie kalorycznosci //foreach (var product in mealDto.Products) //{ // mealDto.Kcal += product.Kcal; //} //mealDto to posiłki z produktami do wyswietlenia na zwrotce response.SuccessResult = mealDto; return(response); }
public async Task <Meal> AddMeal(AddMealBindingModel model, string username) { var user = this.context.Users.FirstOrDefault(u => u.UserName == username); if (user == null) { return(null); } Location location = null; if (!string.IsNullOrEmpty(model.Location)) { location = this.HandleLocation(model.Location, user.Id); } var mealFoodItems = new List <MealFoodItem>(); var mealRecipes = new List <MealRecipe>(); if (model.FoodItems != null) { foreach (var foodItemBindingModel in model.FoodItems) { var foodItem = this.context.FoodItems.FirstOrDefault(fi => fi.Name == foodItemBindingModel.Name); if (foodItem != null && foodItemBindingModel.AmountInGrams > 0) { var mealFoodItem = new MealFoodItem { FoodItem = foodItem, AmountInGrams = foodItemBindingModel.AmountInGrams }; mealFoodItems.Add(mealFoodItem); } } } if (model.Recipes != null) { foreach (var recipeBindingModel in model.Recipes) { var recipe = this.context.Recipes.FirstOrDefault(r => r.Name == recipeBindingModel.Name); if (recipe != null && recipeBindingModel.AmountInGrams > 0) { var mealRecipe = new MealRecipe { Recipe = recipe, AmountInGrams = recipeBindingModel.AmountInGrams }; mealRecipes.Add(mealRecipe); } } } var meal = new Meal { FoodyUser = user, Location = location, MealFoodItems = mealFoodItems, MealRecipes = mealRecipes, Note = model.Note, TimeOfConsumption = model.TimeOfConsumption }; this.context.Meals.Add(meal); this.context.SaveChanges(); await this.SetMealCaloriesAsync(meal.Id); return(meal); }