using System; using System.Windows.Forms; using ProjectLibrary.Entites; using ProjectLibrary.Entities; using ProjectLibrary.Repositories; namespace ProjectLibrary.Forms { public partial class FTicket_Extension : Form { private readonly ITicketExtensionsRepository _ticketExtensionsRepository; private int? _extensionId; public int ID { set { try { var ticketExtension = _ticketExtensionsRepository.GetTicketExtensionById(value); if (ticketExtension == null) { throw new InvalidOperationException($"Ticket extension with ID {value} not found."); } txtReaderID.Text = ticketExtension.ReaderID.ToString(); dtpLastUpdateDate.Value = ticketExtension.LastUpdateDate; dtpNextUpdateDate.Value = ticketExtension.NextUpdateDate; _extensionId = value; } catch (Exception ex) { MessageBox.Show(ex.Message, "[ Error : wrong data ]", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } public FTicket_Extension(ITicketExtensionsRepository ticketExtensionsRepository) { InitializeComponent(); _ticketExtensionsRepository = ticketExtensionsRepository ?? throw new ArgumentNullException(nameof(ticketExtensionsRepository)); } private void SaveBtn_Click(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(txtReaderID.Text)) { throw new Exception("[ Error : blank spaces were left, not enough information ]"); } var ticketExtension = CreateTicketExtension(); if (_extensionId.HasValue) { _ticketExtensionsRepository.UpdateTicketExtension(ticketExtension); } else { _ticketExtensionsRepository.AddTicketExtension(ticketExtension); } MessageBox.Show("Данные успешно сохранены!", "[ Success ]", MessageBoxButtons.OK, MessageBoxIcon.Information); Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "[ Error : while saving ]", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void DiscardBtn_Click(object sender, EventArgs e) { Close(); } private TicketExtensions CreateTicketExtension() { return TicketExtensions.CreateEntity( int.Parse(txtReaderID.Text), dtpLastUpdateDate.Value, dtpNextUpdateDate.Value ); } } }