Пример #1
0
        public static IngredientComparitor FromOunces(double ounces)
        {
            var output = new IngredientComparitor();

            if (ounces <= 0.5)
            {
                output.Amount = Math.Ceiling(ounces * 6);
                output.Unit   = "tsp";
            }
            else if (ounces <= 1.5)
            {
                output.Amount = Math.Ceiling(ounces * 3);
                output.Unit   = "tbs";
            }
            else if (ounces <= 64)
            {
                output.Amount = Math.Ceiling(ounces);
                output.Unit   = "ounce";
            }
            else
            {
                output.Amount = Math.Round(ounces / 64, 1);
                output.Unit   = "gallon";
            }
            return(output);
        }
Пример #2
0
        // Convert externally-provided unit name to database preference
        // Default returns the unitName as-is
        public static IngredientComparitor IPantry(List <IPantryItem> items)
        {
            Volume totalVol   = new Volume();
            double totalPound = 0;
            double totalCount = 0;
            string countUnit  = "";

            foreach (IPantryItem item in items)
            {
                switch (item.Unit)
                {
                case "pinch":
                    totalVol = Volume.FromUsTeaspoons(item.Amount / 16);
                    break;

                case "dash":
                    totalVol = Volume.FromUsTeaspoons(item.Amount / 16);
                    break;

                // Ounce
                case "ounce":
                    totalVol += Volume.FromUsOunces(item.Amount);
                    break;

                // Teaspoon
                case "teaspoon":
                    totalVol += Volume.FromUsTeaspoons(item.Amount);;
                    break;

                // Tablespoon
                case "tablespoon":
                    totalVol += Volume.FromUsTablespoons(item.Amount);
                    break;

                // Cup
                case "cup":
                    totalVol += Volume.FromUsCustomaryCups(item.Amount);
                    break;

                // Pint
                case "pint":
                    totalVol += Volume.FromUsGallons(item.Amount * 8);
                    break;

                // Quart
                case "quart":
                    totalVol += Volume.FromUsGallons(item.Amount * 4);
                    break;

                // Gallon
                case "gallon":
                    totalVol += Volume.FromUsGallons(item.Amount);
                    break;

                // Pound
                case "pound":
                    totalPound += item.Amount;
                    break;

                default:
                    totalCount += item.Amount;
                    countUnit   = item.Unit;
                    break;
                }
            }
            // If the ingredient units are a volume measurement, convert to ounces
            var output = new IngredientComparitor()
            {
                IngredientId = items.ElementAt(0).IngredientId, Count = totalCount, CountUnit = countUnit
            };

            if (totalVol.UsOunces > 0)
            {
                output.Amount = totalVol.UsOunces;
                output.Unit   = "ounce";
            }
            else if (totalPound > 0)
            {
                output.Amount = totalPound;
                output.Unit   = "pound";
            }
            return(output);
        }
Пример #3
0
        public async Task <JsonResponse> Post([FromBody] PlanRequest plan)
        {
            // Check ModelState
            if (!ModelState.IsValid)
            {
                // If invalid, return error message
                return(new JsonResponse {
                    success = false, message = "Something went wrong, please resubmit with all required fields."
                });
            }
            // Create Plan record and save to database
            var newPlan = new Plan {
                Date = plan.date, Meal = plan.meal, ServingsYield = plan.servingsYield, RecipeName = plan.recipe.title, RecipeId = plan.recipe.id, RecipeImage = plan.recipe.image, UserId = plan.userId
            };

            _context.Plan.Add(newPlan);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch
            {
                return(new JsonResponse {
                    success = false, message = "Something went wrong while saving your plan to the database, please try again."
                });
            }

            // Retrieve all active plans to be used in the following loop
            var activePlans = await _context.Plan.Where(p => DateTime.Compare(p.Date, DateTime.Now) >= 0).Where(p => p.DateCompleted == null).Select(p => p.PlanId).ToListAsync();

            // Loop over recipe ingredients and add grocery items for each
            foreach (extIngredient planIngredient in plan.recipe.extendedIngredients)
            {
                // Add this ingredient to the database, if it doesn't already exist
                Ingredient ingredient = await _ingManager.CheckDB(planIngredient);

                // Handle database error
                if (ingredient.IngredientId == -1)
                {
                    return(new JsonResponse {
                        success = false, message = "Something went wrong while saving your ingredients to the database, please try again."
                    });
                }
                // Normalize the ingredient unit name
                planIngredient.unit = Normalizer.UnitName(planIngredient.unit);
                // Add a PlanIngredient record for this ingredient
                var newPlanIngredient = new PlanIngredient {
                    PlanId = newPlan.PlanId, Amount = planIngredient.amount, Unit = planIngredient.unit, IngredientId = ingredient.IngredientId
                };
                _context.PlanIngredient.Add(newPlanIngredient);
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch
                {
                    return(new JsonResponse {
                        success = false, message = "Something went wrong while saving your plan ingredients to the database, please try again."
                    });
                }
                // Sum up total requirement for this ingredient for all active plans
                List <IPantryItem> totalRequirement = await _context.PlanIngredient.Where(pi => _context.Plan.Where(p => DateTime.Compare(p.Date, DateTime.Now) >= 0).Where(p => p.DateCompleted == null).SingleOrDefault(p => p.PlanId == pi.PlanId) != null).Where(pi => pi.IngredientId == planIngredient.id).ToListAsync <IPantryItem>();

                // Normalize total ingredient requirement
                IngredientComparitor req = Normalizer.IPantry(totalRequirement);

                // Sum up total existing inventory for this ingredient
                List <IPantryItem> totalInventory = await _context.InventoryItem.Where(ii => ii.IngredientId == planIngredient.id).ToListAsync <IPantryItem>();

                // Normalize total ingredient requirement
                IngredientComparitor inv = new IngredientComparitor();
                if (totalInventory.Count > 0)
                {
                    inv = Normalizer.IPantry(totalInventory);
                }
                else
                {
                    inv.Amount = 0;
                    inv.Count  = 0;
                }

                // Check whether totalInventory and totalRequirement are the same type first
                if (inv.Amount < req.Amount || inv.Count < req.Count)
                {
                    // Find any existing GroceryItem record for this ingredient, if it exists, modify it with the new quantity
                    List <GroceryItem> groceryItems = await _context.GroceryItem.Where(gi => gi.DateCompleted == null).Where(gi => gi.IngredientId == planIngredient.id).ToListAsync <GroceryItem>();

                    if (groceryItems.Count > 0)
                    {
                        foreach (GroceryItem item in groceryItems)
                        {
                            switch (item.Unit)
                            {
                            case "pinch":
                            case "dash":
                            case "ounce":
                            case "cup":
                            case "pint":
                            case "quart":
                            case "gallon":
                            case "teaspoon":
                            case "tablespoon":
                                var display = DisplayUnit.FromOunces(req.Amount);
                                item.Amount = display.Amount;
                                item.Unit   = display.Unit;
                                req.Amount  = 0;
                                break;

                            case "pound":
                                item.Amount = req.Amount;
                                req.Amount  = 0;
                                break;

                            default:
                                item.Amount = req.Count;
                                req.Count   = 0;
                                break;
                            }
                            _context.Entry(item).State = EntityState.Modified;
                        }
                    }
                    // Else add a new GroceryItem record
                    if (req.Amount > 0 && req.Unit != "pound")
                    {
                        var display        = DisplayUnit.FromOunces(req.Amount);
                        var newGroceryItem = new GroceryItem {
                            IngredientId = req.IngredientId, Amount = display.Amount, Unit = display.Unit, UserId = plan.userId
                        };
                        _context.GroceryItem.Add(newGroceryItem);
                    }
                    else if (req.Amount > 0 && req.Unit == "pound")
                    {
                        var newGroceryItem = new GroceryItem {
                            IngredientId = req.IngredientId, Amount = req.Amount, Unit = req.Unit, UserId = plan.userId
                        };
                        _context.GroceryItem.Add(newGroceryItem);
                    }
                    if (req.Count > 0)
                    {
                        var newGroceryItem = new GroceryItem {
                            IngredientId = req.IngredientId, Amount = req.Count, Unit = req.CountUnit, UserId = plan.userId
                        };
                        _context.GroceryItem.Add(newGroceryItem);
                    }
                }
            }
            try
            {
                await _context.SaveChangesAsync();
            }
            catch
            {
                return(new JsonResponse {
                    success = false, message = "Something went wrong while saving your grocery items to the database, please try again."
                });
            }

            return(new JsonResponse {
                success = true, message = "Plan saved successfully"
            });
        }