using ProjectOptika.Scripts.Entities; using ProjectOptika.Scripts.Repositories; namespace ProjectOptika.Scripts.Forms { public partial class FormSpecification : Form { private readonly ISpecificationsRepository _specificationsRepositories; private int? _specificationsID; public int ID { set { try { var specifications = _specificationsRepositories.GetSpecificationsByID(value); if (specifications == null) throw new InvalidDataException(nameof(specifications)); _specificationsID = specifications.ID; textBoxMaterial.Text = specifications.Material; textBoxAstigmatism.Text = specifications.Astigmatism; textBoxDioptericity.Text = specifications.Dioptericity; textBoxOriginCountry.Text = specifications.OriginCountry; numericUpDownTimeProduction.Value = (decimal)specifications.TimeProduction; } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } public FormSpecification(ISpecificationsRepository specifications, IAccessoriesRepository accessories) { InitializeComponent(); _specificationsRepositories = specifications ?? throw new ArgumentNullException(nameof(specifications)); comboBoxAccessory.DataSource = accessories.GetAccessories(); comboBoxAccessory.DisplayMember = "Name"; comboBoxAccessory.ValueMember = "ID"; } private void ButtonSave_Click(object sender, EventArgs e) { try { if (string.IsNullOrEmpty(textBoxMaterial.Text) || string.IsNullOrEmpty(textBoxAstigmatism.Text) || string.IsNullOrEmpty(textBoxDioptericity.Text) || string.IsNullOrEmpty(textBoxOriginCountry.Text)) { throw new Exception("Имеются незаполненные данные"); } if (_specificationsID.HasValue) { _specificationsRepositories.UpdateSpecifications(CreateSpecifications(_specificationsID.Value)); } else { _specificationsRepositories.CreateSpecifications(CreateSpecifications(0)); } Close(); } catch (System.Exception ex) { MessageBox.Show(ex.Message, "Ошибка при получении сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonCancel_Click(object sender, EventArgs e) { Close(); } private Specifications CreateSpecifications(int id) { return Specifications.CreateEntity(id, (int)comboBoxAccessory.SelectedValue!, textBoxMaterial.Text, textBoxAstigmatism.Text, textBoxDioptericity.Text, textBoxOriginCountry.Text, (double)numericUpDownTimeProduction.Value); } } }