using ProjectFamilyBudget.Entities; using ProjectFamilyBudget.Entities.Enums; using ProjectFamilyBudget.Repositories; 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 ProjectFamilyBudget.Forms { public partial class FormFamily : Form { private readonly IFamily _family; private int? _familyId; public int Id { set { try { var family = _family.ReadFamilyById(value); if (family == null) { throw new InvalidDataException(nameof(family)); } textBoxFamilyName.Text = family.Name; comboBoxFamilyType.SelectedItem = family.FamilyType; _familyId = value; } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при получени данных", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } public FormFamily(IFamily family) { InitializeComponent(); _family = family ?? throw new ArgumentNullException(nameof(family)); comboBoxFamilyType.DataSource = Enum.GetValues(typeof(FamilyType)); } private void buttonSave_Click(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(textBoxFamilyName.Text) || string.IsNullOrWhiteSpace(comboBoxFamilyType.Text)) { throw new Exception("Имеются незаполненные поля"); } if (_familyId.HasValue) { _family.UpdateFamily(CreateFamily(_familyId.Value)); } else { _family.CreateFamily(CreateFamily(0)); } Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void buttonCansel_Click(object sender, EventArgs e) => Close(); private Family CreateFamily(int id) => Family.CreateFamily(id, textBoxFamilyName.Text, (FamilyType)comboBoxFamilyType.SelectedItem!); } }