public void CadastrarIngrediente(Ingrediente ingrediente) { if (ingrediente == null) { throw new ArgumentNullException(); } this.repository.Add(ingrediente); this.repository.Save(); }
// Adicionar novo ingrediente ao banco de dados private void btnAdicionarIngrediente_Click(object sender, EventArgs e) { // Tentar adicionar o ingrediente ao banco de dados try { if (string.IsNullOrWhiteSpace(this.cboListaDeIngrediente.Text)) { throw new GenericWarningException("Informe o nome do ingrediente!"); } else{ foreach (var ing in ingredienteBusiness.RecuperarIngredientes()) { if (ing.Nome == this.cboListaDeIngrediente.Text) { throw new GenericWarningException("Ingrediente com o mesmo nome já está registrado!"); } } } Ingrediente novoIngrediente = new Ingrediente() { Nome = this.cboListaDeIngrediente.Text, Disponivel = true // Os produtos são sempre adicionados disponível }; this.ingredienteBusiness.CadastrarIngrediente(novoIngrediente); MessageBox.Show("Ingrediente registrado com sucesso", Mensagens.Mensagem, MessageBoxButtons.OK, MessageBoxIcon.Information); this.CarregarIngredientes(); } catch (GenericWarningException ex) { MessageBox.Show(ex.Message, Mensagens.Mensagem, MessageBoxButtons.OK, MessageBoxIcon.Warning); } catch (CategoryAlreadyExistsException ex) { MessageBox.Show(ex.Message, Mensagens.Mensagem, MessageBoxButtons.OK, MessageBoxIcon.Warning); } catch (Exception) { MessageBox.Show(Mensagens.CadastroCategoriaFalha, Mensagens.Mensagem, MessageBoxButtons.OK, MessageBoxIcon.Error); } }