using Microsoft.Extensions.Logging; using TaskTrackerContracts.BindingModels; using TaskTrackerContracts.BusinessLogicsContracts; using TaskTrackerContracts.SearchModels; using TaskTrackerDataModels.Models; 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 TaskTrackerView { public partial class FormOrganization : Form { private readonly IOrganizationLogic _logic; private int? _id; private Dictionary _organizationProjects; public int Id { set { _id = value; } } public FormOrganization(IOrganizationLogic logic) { InitializeComponent(); _logic = logic; _organizationProjects = new Dictionary(); } private void FormReinforced_Load(object sender, EventArgs e) { if (_id.HasValue) { try { var view = _logic.ReadElement(new OrganizationSearchModel { Id = _id.Value }); if (view != null) { textBoxName.Text = view.OrganizationName; _organizationProjects = view.OrganizationProjects ?? new Dictionary(); LoadData(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void LoadData() { try { if (_organizationProjects != null) { dataGridView.Rows.Clear(); foreach (var pc in _organizationProjects) { dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ProjectName, pc.Value.Item2 }); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonAdd_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormOrganizationProject)); if (service is FormOrganizationProject form) { if (form.ShowDialog() == DialogResult.OK) { if (form.ProjectModel == null) { return; } if (_organizationProjects.ContainsKey(form.Id)) { _organizationProjects[form.Id] = (form.ProjectModel, form.NumberEmployees); } else { _organizationProjects.Add(form.Id, (form.ProjectModel, form.NumberEmployees)); } LoadData(); } } } private void ButtonEdit_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { var service = Program.ServiceProvider?.GetService(typeof(FormOrganizationProject)); if (service is FormOrganizationProject form) { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); form.Id = id; form.NumberEmployees = _organizationProjects[id].Item2; if (form.ShowDialog() == DialogResult.OK) { if (form.ProjectModel == null) { return; } _organizationProjects[form.Id] = (form.ProjectModel, form.NumberEmployees); LoadData(); } } } } private void ButtonDel_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { try { _organizationProjects?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } LoadData(); } } } private void ButtonUpd_Click(object sender, EventArgs e) { LoadData(); } private void ButtonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxName.Text)) { MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (_organizationProjects == null || _organizationProjects.Count == 0) { MessageBox.Show("Заполните компоненты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { var model = new OrganizationBindingModel { Id = _id ?? 0, OrganizationName = textBoxName.Text, OrganizationProjects = _organizationProjects }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); } MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }