using DressAtelierBusinessLogic.BusinessLogic; using DressAtelierContracts.BindingModels; using DressAtelierContracts.BusinessLogicContracts; using DressAtelierDataModels.Enums; 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 FormMain : Form { private readonly ILogger _logger; private readonly IOrderLogic _orderLogic; private readonly IReportLogic _reportLogic; private readonly IWorkImitation _workImitation; private readonly IBackupLogic _backupLogic; public FormMain(ILogger logger, IOrderLogic orderLogic, IReportLogic reportLogic, IWorkImitation workImitation, IBackupLogic backupLogic) { InitializeComponent(); _logger = logger; _orderLogic = orderLogic; _reportLogic = reportLogic; _workImitation = workImitation; _backupLogic = backupLogic; } private void FormMain_Load(object sender, EventArgs e) { LoadData(); } private void LoadData() { _logger.LogInformation("Downloading orders"); try { dataGridView.FillAndConfigGrid(_orderLogic.ReadList(null)); } catch (Exception ex) { _logger.LogError(ex, "Error downloading orders"); MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void materialsToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormMaterials)); if (service is FormMaterials form) { form.ShowDialog(); } } private void dressesToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormDresses)); if (service is FormDresses form) { form.ShowDialog(); } } private void ButtonCreateOrder_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormOrderCreation)); if (service is FormOrderCreation form) { form.ShowDialog(); LoadData(); } } private void ButtonIssuedOrder_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DressID"].Value); _logger.LogInformation("Order №{id}. Changing status to 'Given'", id); try { var operationResult = _orderLogic.GivenOrder(new OrderBindingModel { ID = id, }); if (!operationResult) { MessageBox.Show("Order must be in 'Ready' state.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); throw new Exception("Saving error. Extra information in logs."); } _logger.LogInformation("Order №{id} is given", id); LoadData(); } catch (Exception ex) { _logger.LogError(ex, "Error in giving 'Is given' status to order"); MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void ButtonRef_Click(object sender, EventArgs e) { LoadData(); } private void materialsListToolStripMenuItem_Click(object sender, EventArgs e) { using var dialog = new SaveFileDialog { Filter = "docx|*.docx" }; if (dialog.ShowDialog() == DialogResult.OK) { _reportLogic.SaveMaterialsToWordFile(new ReportBindingModel { FileName = dialog.FileName }); MessageBox.Show("Done", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void materialsByDressesToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormReportDressMaterials)); if (service is FormReportDressMaterials form) { form.ShowDialog(); } } private void ordersListToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormReportOrders)); if (service is FormReportOrders form) { form.ShowDialog(); } } private void clientsToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormClients)); if (service is FormClients form) { form.ShowDialog(); } } private void startWorkingToolStripMenuItem_Click(object sender, EventArgs e) { _workImitation.DoWork((Program.ServiceProvider?.GetService(typeof(IEmployeeLogic)) as IEmployeeLogic)!, _orderLogic); MessageBox.Show("Work process has started!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void employeesToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormEmployees)); if (service is FormEmployees form) { form.ShowDialog(); } } private void emailsToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormEmails)); if (service is FormEmails form) { form.ShowDialog(); } } private void createBackupToolStripMenuItem_Click(object sender, EventArgs e) { try { if (_backupLogic != null) { var fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { _backupLogic.CreateBackup(new BackupSaveBindingModel { FolderName = fbd.SelectedPath }); MessageBox.Show("Backup has been created.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }