PIbd-22_Shabunov_O.A._SushiBar/SushiBarView/Forms/FormIngredients.cs
2024-05-24 01:56:34 +04:00

99 lines
3.1 KiB
C#

using SushiBarBusinessLogic;
using SushiBarContracts.BindingModels;
namespace SushiBarView.Forms
{
public partial class FormIngredients : Form
{
private readonly IngredientLogic _logic;
public FormIngredients(IngredientLogic Logic)
{
InitializeComponent();
_logic = Logic;
}
private void FormIngredients_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var List = _logic.ReadList(null);
if (List != null)
{
DataGridView.DataSource = List;
DataGridView.Columns["Id"].Visible = false;
DataGridView.Columns["IngredientName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void AddButton_Click(object sender, EventArgs e)
{
FormIngredient Form = new FormIngredient(new IngredientLogic(new SushiBarDatabaseImplement.Storages.IngredientStorage()));
if (Form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
private void ChangeButton_Click(object sender, EventArgs e)
{
if (DataGridView.SelectedRows.Count == 1)
{
FormIngredient Form = new FormIngredient(new IngredientLogic(new SushiBarDatabaseImplement.Storages.IngredientStorage()));
Form.Id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value);
if (Form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
private void DeleteButton_Click(object sender, EventArgs e)
{
if (DataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
int id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value);
try
{
if (!_logic.Delete(new IngredientBindingModel
{
Id = id
}))
{
throw new Exception("Ошибка при удалении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void RefreshButton_Click(object sender, EventArgs e)
{
LoadData();
}
}
}