ProjectLib/ProjectLibrary/Forms/FTicket_Extension.cs

76 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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