ISEbd-22_Alimova_M.S._Confe.../Confectionery/ConfectioneryView/FormPastrys.cs

102 lines
2.7 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
2024-02-28 21:24:29 +04:00
using Microsoft.Extensions.Logging;
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.BusinessLogicsContracts;
using ConfectioneryContracts.DI;
namespace ConfectioneryView
{
public partial class FormPastrys : Form
{
private readonly ILogger _logger;
private readonly IPastryLogic _logic;
public FormPastrys(ILogger<FormPastrys> logger, IPastryLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void FormPastrys_Load(object sender, EventArgs e)
{
LoadData();
}
2024-02-28 21:24:29 +04:00
private void LoadData()
{
try
{
dataGridView.FillAndConfigGrid(_logic.ReadList(null));
_logger.LogInformation("Загрузка кондитерского изделия");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки кондитерского изделия");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2024-02-28 21:24:29 +04:00
private void buttonAdd_Click(object sender, EventArgs e)
{
var form = DependencyManager.Instance.Resolve<FormPastry>();
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
2024-02-28 21:24:29 +04:00
private void buttonUpd_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var form = DependencyManager.Instance.Resolve<FormPastry>();
form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
2024-02-28 21:24:29 +04:00
private void buttonDel_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);
_logger.LogInformation("Удаление кондитерского изделия");
try
{
if (!_logic.Delete(new PastryBindingModel
{
Id = id
}))
{
throw new Exception("Ошибка при удалении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления кондитерского изделия");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
2024-02-28 21:24:29 +04:00
private void buttonRef_Click(object sender, EventArgs e)
{
LoadData();
}
}
}