private async void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateChildren())
                {
                    List <int> ingredientstypes = new List <int>();
                    ingredientstypes = clbTypes.CheckedItems.Cast <Model.IngredientTypes>().Select(x => x.ID).ToList();
                    var request = new IngredientsUpsertRequest()
                    {
                        Description      = txtDesc.Text,
                        Name             = txtName.Text,
                        Status           = cbStatus.Checked,
                        UnitID           = 1,
                        IngredientsTypes = ingredientstypes
                    };

                    var idObjVP = cblUnits.SelectedValue;                 //idObj because it is object in service
                    if (int.TryParse(idObjVP.ToString(), out int unitId)) //so we need to parse it
                    {
                        request.UnitID = unitId;
                    }

                    await _service.Insert <Model.Ingredients>(request);

                    MessageBox.Show("Succesfully added!");
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemple #2
0
        private async void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if ((bool)!_ingredient.Status)
                {
                    throw new Exception("Ingredient is already inactive!");
                }

                IngredientsUpsertRequest request = new IngredientsUpsertRequest
                {
                    Description = _ingredient.Description,
                    Id          = _ingredient.Id,
                    Name        = _ingredient.Name,
                    Status      = false,
                    UnitID      = _ingredient.UnitID
                };

                await _service.Update <Model.Ingredients>(_ingredient.Id, request);

                MessageBox.Show("Success, ingredient is inactive!");
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemple #3
0
        private async void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ValidateChildren())
            {
                _ingredient.Name = txtName.Text;

                IngredientsUpsertRequest request = new IngredientsUpsertRequest
                {
                    Description = _ingredient.Description,
                    Id          = _ingredient.Id,
                    Name        = _ingredient.Name,
                    Status      = _ingredient.Status,
                    UnitID      = _ingredient.UnitID
                };

                try
                {
                    _ingredient = await _service.Update <Model.Ingredients>(_ingredient.Id, request);

                    MessageBox.Show("Success");
                    Close();
                }
                catch
                {
                    MessageBox.Show("Error");
                }
            }
        }
        public void Update(int id, IngredientsUpsertRequest request)
        {
            var entity = _context.Ingredients.Find(id);

            _context.Ingredients.Attach(entity);
            _context.Ingredients.Update(entity);
            _mapper.Map(request, entity);
            _context.SaveChanges();
        }
        public void Insert(IngredientsUpsertRequest request)
        {
            Database.Ingredients entity = _mapper.Map <Database.Ingredients>(request);
            _context.Ingredients.Add(entity);
            _context.SaveChanges();

            foreach (var type in request.IngredientsTypes)
            {
                _context.IngredientsIngredientTypes.Add(new Database.IngredientsIngredientTypes()
                {
                    IngredientTypeId = type,
                    IngredientId     = entity.Id,
                    Description      = ""
                });
            }

            _context.SaveChanges();
        }
Exemple #6
0
 public void Update(int id, [FromBody] IngredientsUpsertRequest request)
 {
     _service.Update(id, request);
 }
Exemple #7
0
 public void Insert(IngredientsUpsertRequest request)
 {
     _service.Insert(request);
 }