using DressAtelierContracts.BusinessLogicContracts; using DressAtelierContracts.BindingModels; using DressAtelierContracts.SearchModels; using Microsoft.Extensions.Logging; 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; namespace SewingDresses { public partial class FormMaterials : Form { private readonly ILogger _logger; private readonly IMaterialLogic _logic; public FormMaterials(ILogger logger, IMaterialLogic logic) { InitializeComponent(); _logger = logger; _logic = logic; } private void FormMaterials_Load(object sender, EventArgs e) { LoadData(); } private void LoadData() { try { var list = _logic.ReadList(null); if (list != null) { materialGridView.DataSource = list; materialGridView.Columns["ID"].Visible = false; materialGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation("Loading materials"); } catch (Exception ex) { _logger.LogError(ex, "Error loading materials"); MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonAdd_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormMaterial)); if (service is FormMaterial form) { if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } private void ButtonUpdate_Click(object sender, EventArgs e) { if (materialGridView.SelectedRows.Count == 1) { var service = Program.ServiceProvider?.GetService(typeof(FormMaterial)); if (service is FormMaterial form) { form.ID = Convert.ToInt32(materialGridView.SelectedRows[0].Cells["ID"].Value); if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } } private void ButtonDelete_Click(object sender, EventArgs e) { if (materialGridView.SelectedRows.Count == 1) { if (MessageBox.Show("Delete record?", "Question",MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int id = Convert.ToInt32(materialGridView.SelectedRows[0].Cells["ID"].Value); _logger.LogInformation("Material deletion"); try { if (!_logic.Delete(new MaterialBindingModel { ID = id })) { throw new Exception("Deletion error. Extra information in logs."); } LoadData(); } catch (Exception ex) { _logger.LogError(ex, "Material deletion error"); MessageBox.Show(ex.Message, "Error",MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } private void ButtonRefresh_Click(object sender, EventArgs e) { LoadData(); } } }