ProjectLib/ProjectLibrary/Forms/FTicket_Extension.cs

76 lines
2.7 KiB
C#
Raw Normal View History

2024-11-20 19:31:06 +04:00
using ProjectLibrary.Entites;
using ProjectLibrary.Repositores;
2024-11-19 14:49:58 +04:00
namespace ProjectLibrary.Forms
{
public partial class FTicket_Extension : Form
{
private readonly ITicketExtensionsRepository _ticketExtensionsRepository;
private int? _extensionId;
2024-11-19 14:49:58 +04:00
2024-11-20 19:31:06 +04:00
public int Id
2024-11-19 14:49:58 +04:00
{
set
{
try
{
2024-11-20 19:31:06 +04:00
var ticket = _ticketExtensionsRepository.ReadTicketExtensionById(value);
if (ticket == null)
{
2024-11-20 19:31:06 +04:00
throw new InvalidOperationException("Заказ не найден.");
}
2024-11-20 19:31:06 +04:00
comboBoxId.SelectedItem = ticket.ReaderID;
dtpLastUpdateDate.Text = ticket.LastUpdateDate.ToString();
dtpNextUpdateDate.Text = ticket.NextUpdateDate.ToString();
_extensionId = value;
}
catch (Exception ex)
{
2024-11-20 19:31:06 +04:00
MessageBox.Show(ex.Message, "Ошибка при загрузке данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2024-11-19 14:49:58 +04:00
}
2024-11-20 19:31:06 +04:00
public FTicket_Extension(ITicketExtensionsRepository ticketRepository)
2024-11-19 14:49:58 +04:00
{
InitializeComponent();
2024-11-20 19:31:06 +04:00
_ticketExtensionsRepository = ticketRepository ?? throw new ArgumentNullException(nameof(ticketRepository));
2024-11-19 14:49:58 +04:00
}
2024-11-20 19:31:06 +04:00
private void btnSave_Click(object sender, EventArgs e)
2024-11-19 14:49:58 +04:00
{
try
{
2024-11-20 19:31:06 +04:00
if (string.IsNullOrWhiteSpace(dtpLastUpdateDate.Text) || string.IsNullOrWhiteSpace(dtpNextUpdateDate.Text) || comboBoxId.SelectedItem == null)
{
2024-11-20 19:31:06 +04:00
throw new Exception("Не все поля заполнены.");
}
2024-11-19 14:49:58 +04:00
2024-11-20 19:31:06 +04:00
var ticket = TicketExtensions.CreateEntity(
_extensionId ?? 0,
DateTime.Parse(dtpLastUpdateDate.Text),
DateTime.Parse(dtpNextUpdateDate.Text)
);
2024-11-19 14:49:58 +04:00
if (_extensionId.HasValue)
{
2024-11-20 19:31:06 +04:00
_ticketExtensionsRepository.UpdateTicketExtension(ticket);
}
else
{
2024-11-20 19:31:06 +04:00
_ticketExtensionsRepository.CreateTicketExtension(ticket);
}
Close();
}
catch (Exception ex)
{
2024-11-20 19:31:06 +04:00
MessageBox.Show(ex.Message, "Ошибка при сохранении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2024-11-20 19:31:06 +04:00
private void btnCancel_Click(object sender, EventArgs e)
2024-11-19 14:49:58 +04:00
{
2024-11-20 19:31:06 +04:00
this.Close();
2024-11-19 14:49:58 +04:00
}
}
}