Example #1
0
        public List <Recepie> GetRecepiesByFilter(RecepieFilter filter, int userId)
        {
            List <Recepie> lstRes = new List <Recepie>();

            List <Recepie> lstFinalRec = new List <Recepie>();

            try
            {
                sqlConnection.Open();
                string where = "recepiecalory.RECEPY_ID = recepies.ID ";
                if (filter.FilterByCategory != -1)
                {
                    where += " AND recepies.ID IN (SELECT RECEPY_ID FROM recepycategory WHERE CATEGORY_ID=" + filter.FilterByCategory + ")";
                }
                sqlCommand.CommandText = "SELECT recepies.ID, recepies.RECEPY_NAME, recepies.RECEPY_TEXT, recepies.IMAGE, recepiecalory.RECEPY_SUM FROM recepies,recepiecalory WHERE " + where;
                MySqlDataReader reader = sqlCommand.ExecuteReader();
                while (reader.Read())
                {
                    int    id         = reader.GetInt32("ID");
                    string name       = reader.GetString("RECEPY_NAME");
                    string text       = reader.GetString("RECEPY_TEXT");
                    string pictureUrl = reader.GetString("IMAGE");
                    int    cals       = reader.GetInt32("RECEPY_SUM");

                    Recepie res = new Recepie(id, name, text);
                    res.Calories   = cals;
                    res.pictureUrl = pictureUrl;
                    lstRes.Add(res);
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                sqlConnection.Close();
            }

            if (lstRes.Count > 0)
            {
                List <Ingredient> lstUserIng = new List <Ingredient>();
                lstUserIng = GetAllIngredients(userId);

                foreach (Recepie res in lstRes)
                {
                    bool ok = false;
                    List <Ingredient> lstIng = new List <Ingredient>();

                    lstIng = GetAllRecepieIngs(res.Id);

                    foreach (Ingredient ing in lstIng)
                    {
                        ok = false;
                        foreach (Ingredient ingg in lstUserIng)
                        {
                            if (ingg.Id == ing.Id)
                            {
                                ok = true;
                            }
                        }

                        if (!ok)
                        {
                            break;
                        }
                    }

                    if (ok)
                    {
                        lstFinalRec.Add(res);
                    }
                }
            }

            return(lstFinalRec);
        }
        public List <Recepie> GetRecepies(int CategoryId, bool Sort, int userId)
        {
            RecepieFilter filter = new RecepieFilter(CategoryId, Sort);

            return(dal.GetRecepiesByFilter(filter, userId));
        }