76 lines
2.7 KiB
C#
76 lines
2.7 KiB
C#
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();
|
||
}
|
||
}
|
||
}
|