84 lines
3.2 KiB
C#
84 lines
3.2 KiB
C#
using ProjectLibrary.Entites;
|
||
using ProjectLibrary.Repositores;
|
||
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 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, IReaderRepository readerRepository)
|
||
{
|
||
InitializeComponent();
|
||
_ticketExtensionsRepository = ticketRepository ?? throw new ArgumentNullException(nameof(ticketRepository));
|
||
readerRepository = readerRepository ?? throw new ArgumentNullException(nameof(readerRepository));
|
||
|
||
|
||
comboBoxId.DataSource = readerRepository.ReadReaders();
|
||
comboBoxId.DisplayMember = "Name"; // Предполагается, что у Reader есть свойство Name
|
||
comboBoxId.ValueMember = "Id"; // И свойство Id для идентификации
|
||
}
|
||
|
||
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(
|
||
0,
|
||
(int)comboBoxId.SelectedValue!,
|
||
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();
|
||
}
|
||
}
|
||
}
|