68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
|
using IT_Company.Entities;
|
|||
|
using IT_Company.Entities.Repositories;
|
|||
|
using System.Data;
|
|||
|
namespace IT_Company.Forms
|
|||
|
{
|
|||
|
public partial class FormOrganization: Form
|
|||
|
{
|
|||
|
private readonly IOrganizationRepository _organizationRepository;
|
|||
|
|
|||
|
private int? _organizationId;
|
|||
|
public int Id
|
|||
|
{
|
|||
|
set
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var organization = _organizationRepository.ReadOrganizationById(value);
|
|||
|
if (organization != null)
|
|||
|
{
|
|||
|
throw new InvalidDataException(nameof(organization));
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public FormOrganization(IOrganizationRepository organizationRepository)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_organizationRepository = organizationRepository ?? throw new ArgumentNullException(nameof(organizationRepository));
|
|||
|
}
|
|||
|
|
|||
|
private void buttonSave_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if(string.IsNullOrEmpty(name.Text) || string.IsNullOrEmpty(address.Text) || string.IsNullOrEmpty(bankAccount.Text))
|
|||
|
{
|
|||
|
throw new DataException("Имеются незаполненные поля");
|
|||
|
}
|
|||
|
if (_organizationId.HasValue)
|
|||
|
{
|
|||
|
_organizationRepository.UpdateOrganization(CreateOrganization(_organizationId.Value));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_organizationRepository.CreateOrganization(CreateOrganization(0));
|
|||
|
}
|
|||
|
Close();
|
|||
|
}catch(Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
Close();
|
|||
|
}
|
|||
|
|
|||
|
private Organization CreateOrganization(int id) => Organization.CreateEntity(id,name.Text,address.Text,bankAccount.Text);
|
|||
|
|
|||
|
}
|
|||
|
}
|