Пример #1
0
 public static List <Reseptit> GetReseptit()
 {
     try
     {
         List <Reseptit> resepti = new List <Reseptit>();
         string          cs      = GetMysqlConnectionString();
         string          sql     = "SELECT nimi, valmistusaika, haaste FROM RESEPTIT ORDER BY nimi";
         using (MySqlConnection conn = new MySqlConnection(cs))
         {
             conn.Open();
             MySqlCommand cmd = new MySqlCommand(sql, conn);
             using (MySqlDataReader rdr = cmd.ExecuteReader())
             {
                 while (rdr.Read())
                 {
                     Reseptit a = new Reseptit();
                     a.Nimi          = rdr.GetString(0);
                     a.Valmistusaika = rdr.GetInt32(1);
                     a.Haaste        = rdr.GetString(2);
                     resepti.Add(a);
                 }
             }
         }
         return(resepti);
     }
     catch
     {
         throw;
     }
 }
Пример #2
0
 public static List <Reseptit> HaeToteutettavat()   //listaa reseptit nimeltä jotka voi toteuttaa
 {
     try
     {
         List <Reseptit> resepti = new List <Reseptit>();
         string          cs      = GetMysqlConnectionString();
         string          sql     = "select nimi, valmistusaika, haaste from RESEPTIT where Resepti_ID not in (select RESEPTIT.Resepti_ID from OHJEET, AINEET, RESEPTIT where OHJEET.Resepti_ID = RESEPTIT.Resepti_ID and OHJEET.Aine_ID = AINEET.Aine_ID and AINEET.maara < OHJEET.Maara)";
         using (MySqlConnection conn = new MySqlConnection(cs))
         {
             conn.Open();
             MySqlCommand cmd = new MySqlCommand(sql, conn);
             using (MySqlDataReader rdr = cmd.ExecuteReader())
             {
                 while (rdr.Read())
                 {
                     Reseptit a = new Reseptit();
                     a.Nimi          = rdr.GetString(0);
                     a.Valmistusaika = rdr.GetInt32(1);
                     a.Haaste        = rdr.GetString(2);
                     resepti.Add(a);
                 }
             }
         }
         return(resepti);
     }
     catch
     {
         throw;
     }
 }
Пример #3
0
        public static bool LisaaResepti(Reseptit reseptit, List <Aineet> L)
        {
            bool   b   = false;
            bool   c   = false;
            string cs  = GetMysqlConnectionString();
            string sql = string.Format("INSERT INTO RESEPTIT (Nimi, Haaste, Valmistusaika, Ohje) VALUES (@0,@1,@2,@3)");

            using (MySqlConnection conn = new MySqlConnection(cs))
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@0", reseptit.Nimi); //SQL-injektion esto
                cmd.Parameters.AddWithValue("@1", reseptit.Haaste);
                cmd.Parameters.AddWithValue("@2", reseptit.Valmistusaika);
                cmd.Parameters.AddWithValue("@3", reseptit.Ohje);
                int lkm = cmd.ExecuteNonQuery();
                if (lkm == 1)
                {
                    b = true;
                }
                else
                {
                    b = false;
                }
            }
            Aineet a = new Aineet();

            foreach (var item in L)
            {
                a = (Aineet)item;
                string mysql = string.Format("INSERT INTO OHJEET (Resepti_ID, Aine_ID, maara, mittayksikko) VALUES ((Select Resepti_ID from RESEPTIT where nimi = @0),(Select Aine_ID from AINEET where nimi = @1), @2,(Select mittayksikko from AINEET where nimi = @3))");
                using (MySqlConnection conn = new MySqlConnection(cs))
                {
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand(mysql, conn);
                    cmd.Parameters.AddWithValue("@0", reseptit.Nimi);
                    cmd.Parameters.AddWithValue("@1", a.Nimi);
                    cmd.Parameters.AddWithValue("@2", a.Maara);
                    cmd.Parameters.AddWithValue("@3", a.Nimi);
                    int lkm = cmd.ExecuteNonQuery();
                    if (lkm == 1)
                    {
                        c = true;
                    }
                    else
                    {
                        c = false;
                    }
                }
            }
            if (b && c)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #4
0
        public static List <Reseptit> VertaaPuutteet() // Palauttaa reseptin nimen jos reseptissä on puutteita
        {
            List <Reseptit> resepti = new List <Reseptit>();

            string cs  = GetMysqlConnectionString();
            string sql = string.Format("select distinct RESEPTIT.nimi from OHJEET, AINEET, RESEPTIT where OHJEET.Resepti_ID = RESEPTIT.Resepti_ID and OHJEET.Aine_ID = AINEET.Aine_ID  and  AINEET.maara < OHJEET.Maara;");

            using (MySqlConnection conn = new MySqlConnection(cs))
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                using (MySqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        Reseptit r = new Reseptit();
                        r.Nimi = rdr.GetString(0);
                        resepti.Add(r);
                    }
                }
            }
            return(resepti);
        }
Пример #5
0
        public static bool TallennaResepti(Reseptit resepti, List <Aineet> ainelista)  //Reseptin aineiden määrän ja ohjeen syöttäminen
        {
            bool   b   = false;
            bool   c   = false;
            string cs  = GetMysqlConnectionString();
            string sql = "UPDATE RESEPTIT SET ohje = @0 where nimi = @1";

            using (MySqlConnection conn = new MySqlConnection(cs))
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@0", resepti.Ohje);
                cmd.Parameters.AddWithValue("@1", resepti.Nimi);
                int lkm = cmd.ExecuteNonQuery();
                if (lkm == 1)
                {
                    b = true;
                }
                else
                {
                    b = false;
                }
            }

            // päivitetään tietokannan reseptiin aineiden nimet ja määrät (ainemäärät)

            Aineet a = new Aineet();

            foreach (var item in ainelista)
            {
                a = (Aineet)item;

                cs  = GetMysqlConnectionString();
                sql = "UPDATE OHJEET SET Maara = @0 where Resepti_ID = (Select Resepti_ID from RESEPTIT where nimi = @1) and Aine_ID = (Select Aine_ID from AINEET where nimi = @2)";
                using (MySqlConnection conn = new MySqlConnection(cs))
                {
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@0", a.Maara);
                    cmd.Parameters.AddWithValue("@1", resepti.Nimi);
                    cmd.Parameters.AddWithValue("@2", a.Nimi);

                    int lkm = cmd.ExecuteNonQuery();
                    if (lkm == 1)
                    {
                        c = true;
                    }
                    else
                    {
                        c = false;
                    }
                }
            }

            if (b && c)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }