Exemple #1
0
        public IHttpActionResult Putingredients(string id, ingredients ingredients)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != ingredients.nom)
            {
                return(BadRequest());
            }

            db.Entry(ingredients).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ingredientsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemple #2
0
        public IHttpActionResult Postingredients(ingredients ingredients)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.ingredients.Add(ingredients);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (ingredientsExists(ingredients.nom))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("DefaultApi", new { id = ingredients.nom }, ingredients));
        }
Exemple #3
0
        // GET api/<controller>
        public List <ingredients> Get()
        {
            ingredients        ingredients = new ingredients();
            List <ingredients> ingList     = ingredients.Read();

            return(ingList);
        }
Exemple #4
0
        private void addIngrBtn_Click(object sender, RoutedEventArgs e)
        {
            ingredients newItem = new ingredients()
            {
                name = IngrNameTextBox.Text
            };

            _db.ingredients.Add(newItem);
            _db.SaveChanges();
            this.Hide();
        }
Exemple #5
0
        public IHttpActionResult Deleteingredients(string id)
        {
            ingredients ingredients = db.ingredients.Find(id);

            if (ingredients == null)
            {
                return(NotFound());
            }

            db.ingredients.Remove(ingredients);
            db.SaveChanges();

            return(Ok(ingredients));
        }
Exemple #6
0
        //--------------------------------------------------------------------
        // Build the Insert command String
        //--------------------------------------------------------------------
        private String BuildInsertCommand(ingredients ingredients)
        {
            String command;

            StringBuilder sb = new StringBuilder();

            // use a string builder to create the dynamic string
            sb.AppendFormat("Values('{0}', '{1}','{2}')", ingredients.Name, ingredients.Image, ingredients.Calories);
            String prefix = "INSERT INTO ingredients_G_2021 " + "( [name], [image],[calories]) ";

            command = prefix + sb.ToString();

            return(command);
        }
Exemple #7
0
        public IHttpActionResult Getingredients(string id)
        {
            ingredients         ingredients = db.ingredients.Find(id);
            ingredientViewModel ivm         = new ingredientViewModel();

            if (ingredients == null)
            {
                return(NotFound());
            }
            else
            {
                ivm.nom      = ingredients.nom;
                ivm.calories = ingredients.calories;
                ivm.label    = ingredients.label;
                return(Ok(ivm));
            }
        }
        private void AddStorageItemBtn_Click(object sender, RoutedEventArgs e)
        {
            ingredients newItem = new ingredients()
            {
                name = StorageItemNameTextBox.Text
            };

            _db.ingredients.Add(newItem);
            _db.SaveChanges();
            storage_ingredient newItem2 = new storage_ingredient()
            {
                count                  = 0,
                ingredient_id          = (from m in _db.ingredients select m.id).ToList().Last(),
                unit_of_measurement_id = unit_id
            };

            _db.storage_ingredient.Add(newItem2);
            _db.SaveChanges();
            this.Hide();
        }
Exemple #9
0
        public List <ingredients> ReadIng()
        {
            SqlConnection      con     = null;
            List <ingredients> INGList = new List <ingredients>();

            try
            {
                con = connect("DBConnectionString"); // create a connection to the database using the connection String defined in the web config file

                String     selectSTR = "SELECT * FROM ingredients_G_2021";
                SqlCommand cmd       = new SqlCommand(selectSTR, con);

                // get a reader
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); // CommandBehavior.CloseConnection: the connection will be closed after reading has reached the end

                while (dr.Read())
                {   // Read till the end of the data into a row
                    ingredients b = new ingredients();

                    b.Id       = Convert.ToInt32(dr["id"]);
                    b.Name     = (string)dr["name"];
                    b.Image    = (string)dr["image"];
                    b.Calories = Convert.ToDouble(dr["calories"]);
                    INGList.Add(b);
                }

                return(INGList);
            }
            catch (Exception ex)
            {
                // write to log
                throw (ex);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }
Exemple #10
0
        //--------------------------------------------------------------------------------------------------
        // This method inserts a student to the student table
        //--------------------------------------------------------------------------------------------------
        public int Insert(ingredients ingredients)
        {
            SqlConnection con;
            SqlCommand    cmd;

            try
            {
                con = connect("DBConnectionString"); // create the connection
            }
            catch (Exception ex)
            {
                // write to log
                throw (ex);
            }

            String cStr = BuildInsertCommand(ingredients); // helper method to build the insert string

            cmd = CreateCommand(cStr, con);                // create the command

            try
            {
                int numEffected = cmd.ExecuteNonQuery(); // execute the command
                return(numEffected);
            }
            catch (Exception ex)
            {
                // write to log
                throw (ex);
            }

            finally
            {
                if (con != null)
                {
                    // close the db connection
                    con.Close();
                }
            }
        }
Exemple #11
0
 // POST api/<controller>
 public void Post([FromBody] ingredients ing)
 {
     ing.Insert();
 }