Example #1
0
    //public object CreateShoppingList(List<Foods.NewFood> x, int consumers, string lang) {
    //    List<FoodQty> fq = LoadFoodQty();
    //    object res = new object();
    //    List<Foods.NewFood> list = new List<Foods.NewFood>();
    //    Foods f = new Foods();
    //    List<Foods.NewFood> foods = new List<Foods.NewFood>();
    //    if (consumers >= 1) {
    //        foods = f.MultipleConsumers(x, consumers);
    //    } else {
    //        foods = x;
    //    }
    //    var group = foods.GroupBy(a => a.food).Select(a => new {
    //        id = a.Select(i => i.id).FirstOrDefault(),
    //        food = a.Key,
    //        qty = a.Sum(q => q.quantity),
    //        unit = f.GetUnit(a.Sum(q => q.quantity), a.Select(u => u.unit).FirstOrDefault()),
    //        mass = Math.Round(a.Sum(m => m.mass), 0),
    //        smartQty = SmartQty(a.Select(i => i.id).FirstOrDefault()
    //                            , a.Sum(q => q.quantity)
    //                            , f.GetUnit(a.Sum(q => q.quantity), a.Select(u => u.unit).FirstOrDefault())
    //                            , Math.Round(a.Sum(m => m.mass), 0)
    //                            , fq
    //                            , lang),
    //        smartMass = SmartMass(Math.Round(a.Sum(m => m.mass), 0), lang),
    //        price = Math.Round(a.Sum(p => p.price.value), 2),
    //        currency = a.Select(u => u.price.currency).FirstOrDefault()
    //    }).ToList();
    //    var totalPrice = Math.Round(foods.Sum(a => a.price.value), 2);
    //    string currency = null;
    //    if (foods.Any(a => a.price.currency != null)) {
    //        currency = foods.FirstOrDefault(a => a.price.currency != null).price.currency;
    //    }
    //    res = new {
    //        foods = group,
    //        total = new {
    //            price = totalPrice,
    //            currency = currency
    //        }
    //    };
    //    return res;
    //}

    public List <FoodQty> LoadFoodQty()
    {
        List <FoodQty> xx = new List <FoodQty>();

        try {
            using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + Server.MapPath("~/App_Data/" + dataBase))) {
                connection.Open();
                string sql = "SELECT id, food, qty, unit FROM foodQty";
                using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                    using (SQLiteDataReader reader = command.ExecuteReader()) {
                        while (reader.Read())
                        {
                            FoodQty x = new FoodQty();
                            x.id   = reader.GetValue(0) == DBNull.Value ? "" : reader.GetString(0);
                            x.food = reader.GetValue(1) == DBNull.Value ? "" : reader.GetString(1);
                            x.qty  = reader.GetValue(2) == DBNull.Value ? 0 : Convert.ToDouble(reader.GetString(2));
                            x.unit = reader.GetValue(3) == DBNull.Value ? "" : reader.GetString(3);
                            xx.Add(x);
                        }
                    }
                }
            }
            return(xx);
        } catch (Exception e) {
            L.SendErrorLog(e, null, null, "ShoppingList", "LoadFoodQty");
            return(xx);
        }
    }
Example #2
0
 public ActionResult <FoodQty> PostFoodQtyItem(FoodQty foodQty)
 {
     _context.FoodQty.Add(foodQty);
     _context.SaveChanges();
     return(CreatedAtAction("GetFoodQtyItem", new FoodQty {
         foId = foodQty.foId
     }, foodQty));
 }
Example #3
0
 public ActionResult PutFoodQtyItem(int id, FoodQty foodQty)
 {
     if (id != foodQty.foId)
     {
         return(BadRequest());
     }
     _context.Entry(foodQty).State = EntityState.Modified;
     _context.SaveChanges();
     return(CreatedAtAction("GetFoodQtyItem", new FoodQty {
         foId = foodQty.foId
     }, foodQty));
 }
Example #4
0
    public string SmartQty(string id, double qty, string unit, double mass, List <FoodQty> foodQty, string lang)
    {
        string  resp    = null;
        string  resp_   = null;
        Foods   food    = new Foods();
        double  baseQty = 0;
        string  unit_   = food.GetUnit(qty, unit);
        FoodQty fq      = foodQty.Where(a => a.id == id).FirstOrDefault();

        if (fq != null)
        {
            baseQty = fq.qty;
            unit    = fq.unit;
        }

        if (baseQty > 0)
        {
            if (unit == "l")
            {
                //***** Only for liquids (milk, oil, beverages...) ***** //
                if (mass / baseQty > 0.5)
                {
                    qty   = Math.Round(mass / baseQty, 1);
                    unit_ = food.GetUnit(qty, t.Tran(unit, lang));
                    resp_ = string.Format(" (1{0}={1}{2})", t.Tran(unit, lang), baseQty, t.Tran("g", lang));
                }
            }
            else
            {
                if (mass > baseQty)
                {
                    qty   = Math.Round(mass / baseQty, 1);
                    unit_ = food.GetUnit(qty, t.Tran(unit, lang));
                    resp_ = string.Format(" (1{0}={1}{2})", t.Tran(unit, lang), baseQty, t.Tran("g", lang));
                }
            }
        }

        resp = string.Format("{0} {1}{2}"
                             , qty.ToString()
                             , unit_
                             , !string.IsNullOrWhiteSpace(resp_) ? resp_ : null);

        return(resp);
    }
Example #5
0
 public List <FoodQty> LoadFoodQty()
 {
     try {
         SQLiteConnection connection = new SQLiteConnection("Data Source=" + Server.MapPath("~/App_Data/" + dataBase));
         connection.Open();
         string           sql     = "SELECT id, food, qty, unit FROM foodQty";
         SQLiteCommand    command = new SQLiteCommand(sql, connection);
         List <FoodQty>   xx      = new List <FoodQty>();
         SQLiteDataReader reader  = command.ExecuteReader();
         while (reader.Read())
         {
             FoodQty x = new FoodQty();
             x.id   = reader.GetValue(0) == DBNull.Value ? "" : reader.GetString(0);
             x.food = reader.GetValue(1) == DBNull.Value ? "" : reader.GetString(1);
             x.qty  = reader.GetValue(2) == DBNull.Value ? 0 : Convert.ToDouble(reader.GetString(2));
             x.unit = reader.GetValue(3) == DBNull.Value ? "" : reader.GetString(3);
             xx.Add(x);
         }
         connection.Close();
         return(xx);
     } catch (Exception e) { return(new List <FoodQty>()); }
 }
Example #6
0
    public string SmartQty(string id, double qty, string unit, double mass, List <FoodQty> foodQty, string lang)
    {
        Foods   food    = new Foods();
        double  baseQty = 0;
        string  unit_   = food.GetUnit(qty, unit);
        FoodQty fq      = foodQty.Where(a => a.id == id).FirstOrDefault();

        if (fq != null)
        {
            baseQty = fq.qty;
            unit    = fq.unit;
        }
        if (baseQty > 0 && mass > baseQty)
        {
            qty   = Math.Round(mass / baseQty, 1);
            unit_ = food.GetUnit(qty, t.Tran(unit, lang));
        }

        return(string.Format("{0} {1}{2}"
                             , qty.ToString()
                             , unit_
                             , baseQty > 0 && mass > baseQty ? string.Format(" (1 {0} = {1} {2})", t.Tran(unit, lang), baseQty, t.Tran("g", lang)) : ""));
    }