Пример #1
0
        private void SaveChanges()
        {
            if (Request.QueryString["id"] != null)
            {
                int           id          = int.Parse(Request.QueryString["id"]);
                List <string> foodNames   = SQLProcedures.GetFoodNames();
                string        newFoodName = txtNazivNamirnice.Text;
                if (foodNames.Contains(newFoodName.ToUpper()) && foodName.ToUpper() != newFoodName.ToUpper())
                {
                    lblErrorMessage.Text    = "Namirnica već postoji!";
                    lblErrorMessage.Visible = true;
                    return;
                }
                string foodType = rblOdaberiTipNamirnice.SelectedItem.Text;
                Food   food     = new Food
                {
                    IDFood = id,
                    Name   = foodName,
                    Type   = foodType
                };
                SQLProcedures.UpdateFood(food);

                Dictionary <string, int> newUnits = new Dictionary <string, int>();
                string unitName;
                int    unitKcal;
                foreach (ListItem item in lbDodaneJedinice.Items)
                {
                    string unit = item.Text;
                    unitName = unit.Split('(')[0];
                    unitKcal = int.Parse(unit.Split('(')[1].TrimEnd(')'));
                    newUnits.Add(unitName, unitKcal);
                }
                foreach (var item in newUnits)
                {
                    if (oldUnits.ContainsKey(item.Key))
                    {
                        SQLProcedures.UpdateMeasuringUnitForFood(item.Key, item.Value, id);
                    }
                    else if (!oldUnits.ContainsKey(item.Key))
                    {
                        SQLProcedures.InsertMeasuringUnitForFood(item.Key, item.Value, id);
                    }
                }
                foreach (var item in oldUnits)
                {
                    if (!newUnits.ContainsKey(item.Key))
                    {
                        SQLProcedures.DeleteMeasuringUnitForFood(item.Key, id);
                    }
                }
                Response.Redirect("Foods.aspx");
            }
            else
            {
                List <string> foodNames = SQLProcedures.GetFoodNames();
                string        foodName  = txtNazivNamirnice.Text;
                if (foodNames.Contains(foodName.ToUpper()))
                {
                    lblErrorMessage.Text    = "Namirnica već postoji!";
                    lblErrorMessage.Visible = true;
                    return;
                }
                string foodType = rblOdaberiTipNamirnice.SelectedItem.Text;
                Food   food     = new Food
                {
                    Name = foodName,
                    Type = foodType
                };
                int id = (int)SQLProcedures.InsertFood(food);

                Dictionary <string, int> newUnits = new Dictionary <string, int>();
                string unitName;
                int    unitKcal;
                foreach (ListItem item in lbDodaneJedinice.Items)
                {
                    string unit = item.Text;
                    unitName = unit.Split('(')[0];
                    unitKcal = int.Parse(unit.Split('(')[1].TrimEnd(')'));
                    newUnits.Add(unitName, unitKcal);
                }
                foreach (var item in newUnits)
                {
                    SQLProcedures.InsertMeasuringUnitForFood(item.Key, item.Value, id);
                }
                Response.Redirect("Foods.aspx");
            }
        }