76 lines
2.4 KiB
C#
76 lines
2.4 KiB
C#
|
using IT_Company.Entities;
|
|||
|
using IT_Company.Entities.Repositories;
|
|||
|
using System.Data;
|
|||
|
namespace IT_Company.Forms
|
|||
|
{
|
|||
|
public partial class FormService : Form
|
|||
|
{
|
|||
|
private readonly IServiceRepository _serviceRepository;
|
|||
|
|
|||
|
private int? _serviceId;
|
|||
|
public int Id
|
|||
|
{
|
|||
|
set
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var service = _serviceRepository.ReadServiceById(value);
|
|||
|
if (service != null)
|
|||
|
{
|
|||
|
throw new InvalidDataException(nameof(service));
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public FormService(IServiceRepository serviceRepository)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_serviceRepository = serviceRepository ?? throw new ArgumentNullException(nameof(serviceRepository));
|
|||
|
}
|
|||
|
|
|||
|
private void buttonSave_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(description.Text) || string.IsNullOrEmpty(price.Text) || string.IsNullOrEmpty(bankAccount.Text))
|
|||
|
{
|
|||
|
throw new DataException("Имеются незаполненные поля");
|
|||
|
}
|
|||
|
if (_serviceId.HasValue)
|
|||
|
{
|
|||
|
_serviceRepository.UpdateService(CreateService(_serviceId.Value));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_serviceRepository.CreateService(CreateService(0));
|
|||
|
}
|
|||
|
Close();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
Close();
|
|||
|
}
|
|||
|
|
|||
|
private Service CreateService(int id) => Service.CreateEntity(id,int.Parse(price.Text),description.Text);
|
|||
|
|
|||
|
|
|||
|
private void price_KeyPress(object sender, KeyPressEventArgs e)
|
|||
|
{
|
|||
|
char c = e.KeyChar;
|
|||
|
if (!Char.IsDigit(c) && c != 8)
|
|||
|
e.Handled = true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|