public IActionResult Create([FromBody] Dish dish) { if (!ModelState.IsValid) { return(BadRequest()); } db.Create(dish); db.Save(); return(Ok(dish)); }
private void btnSaveEntity_Click(object sender, EventArgs e) { if (txtBoxName.Text.Length == 0) { txtBoxName.Focus(); return; } var dishRepo = new DishRepository(_connectionString); var unitRepo = new UnitRepository(_connectionString); try { if (_entity != null && _entity.GetType() == typeof(Dish)) { var dish = _entity as Dish; dish.DishName = txtBoxName.Text; if (dish.DishID == 0) { dishRepo.Create(dish); } else { dishRepo.Update(dish); } } } catch (SqlException sqlException) { if (sqlException.Number == 2627) { MessageBox.Show("Наименование блюда должно быть уникальным", "Предупреждение", MessageBoxButtons.OK); } } try { if (_entity != null && _entity.GetType() == typeof(Unit)) { var unit = _entity as Unit; unit.UnitName = txtBoxName.Text; if (unit.UnitID == 0) { unitRepo.Create(unit); } else { unitRepo.Update(unit); } } } catch (SqlException sqlException) { if (sqlException.Number == 2627) { MessageBox.Show("Наименование единицы измерения должно быть уникальным", "Предупреждение", MessageBoxButtons.OK); } } DialogResult = DialogResult.OK; Close(); }