80 lines
2.3 KiB
C#
80 lines
2.3 KiB
C#
|
using ProjectGSM.Entities;
|
|||
|
using ProjectGSM.Repositories;
|
|||
|
|
|||
|
namespace ProjectGSM.Forms
|
|||
|
{
|
|||
|
|
|||
|
public partial class FormCourt : Form
|
|||
|
{
|
|||
|
private readonly ICourtRepository _courtRepository;
|
|||
|
|
|||
|
private int? _courtId;
|
|||
|
|
|||
|
public int Id
|
|||
|
{
|
|||
|
set
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var court =
|
|||
|
_courtRepository.ReadCourtById(value);
|
|||
|
if (court == null)
|
|||
|
{
|
|||
|
throw new
|
|||
|
InvalidDataException(nameof(court));
|
|||
|
}
|
|||
|
nameTextBox.Text = court.Name;
|
|||
|
adressBox.Text = court.Address;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public FormCourt(ICourtRepository courtRepository)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_courtRepository = courtRepository ??
|
|||
|
throw new
|
|||
|
ArgumentNullException(nameof(courtRepository));
|
|||
|
}
|
|||
|
|
|||
|
private void saveButton_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (string.IsNullOrWhiteSpace(nameTextBox.Text) || string.IsNullOrWhiteSpace(adressBox.Text))
|
|||
|
{
|
|||
|
throw new Exception("Имеются незаполненные поля");
|
|||
|
}
|
|||
|
if (_courtId.HasValue)
|
|||
|
{
|
|||
|
_courtRepository.UpdateCourt(CreateCourt(_courtId.Value));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_courtRepository.CreateCourt(CreateCourt(0));
|
|||
|
}
|
|||
|
Close();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка при сохранении",
|
|||
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void cancelButton_Click(object sender, EventArgs e) => Close();
|
|||
|
|
|||
|
private Court CreateCourt(int id) => Court.CreateEntity(id,
|
|||
|
nameTextBox.Text,
|
|||
|
adressBox.Text);
|
|||
|
|
|||
|
}
|
|||
|
}
|