public SaleHistoryDto(SalesHistoryCreate create) { RestaurantId = create.RestaurantId; Value = create.Value; DishesCount = create.DishesForSale; SaleDate = create.Date; }
private void FillSelectedRecipes(SalesHistoryCreate history) { foreach (var dish in history.AllDishes.Where(d => d.IsSelected)) { var dto = new RecipeDto(dish); history.DishesForSale.Add(dto.Name, dish.Count); } }
public async Task <ActionResult> Create() { var model = new SalesHistoryCreate(); model.Restaurants = await _restaurantsService.GetAllAsync(); var dishes = await _recipesService.GetAllAsync(); model.AllDishes = ConvertToRecipesSelect(dishes); return(View(model)); }
public async Task <ActionResult> Create(SalesHistoryCreate model) { if (!ModelState.IsValid) { return(RedirectToAction("Create", model)); } model.Date = DateTime.Now; FillSelectedRecipes(model); var dto = new SaleHistoryDto(model); var restaurant = await _restaurantsService.GetAsync(model.RestaurantId); if (restaurant == null) { return(HttpNotFound()); } var selectedDishes = model.AllDishes.Where(d => d.IsSelected).ToList(); var dishesFromDb = await _recipesService.GetAllAsync(); foreach (var dish in selectedDishes) { dish.Ingredients = dishesFromDb.Single(d => d.Id == dish.Id).Ingredients; } //if (!DishesCanBePrepared( // selectedDishes, // restaurant.IngredientsCountDictionary)) //{ // model.Message = Properties.Resources.LackOfIngredients; // return View(model); //} try { SubtractIngredientsFromRestaurant(restaurant, selectedDishes); } catch (Exception e) { model.Message = e.Message; model.Restaurants = await _restaurantsService.GetAllAsync(); return(View(model)); } await _restaurantsService.UpdateAsync(restaurant); await _salesHistorysService.AddAsync(dto); return(RedirectToAction("Index")); }