70 lines
2.3 KiB
C#
70 lines
2.3 KiB
C#
using System;
|
|
using System.Windows.Forms;
|
|
using YourNamespace.Entities;
|
|
using YourNamespace.Repositories;
|
|
|
|
namespace YourNamespace.Forms
|
|
{
|
|
public partial class FormAirport : Form
|
|
{
|
|
private readonly IAirportRepository _airportRepository;
|
|
private int? _airportId;
|
|
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var airport = _airportRepository.ReadAirportById(value);
|
|
if (airport == null)
|
|
{
|
|
throw new InvalidDataException(nameof(airport));
|
|
}
|
|
textBoxName.Text = airport.Name;
|
|
textBoxLocation.Text = airport.Location;
|
|
_airportId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public FormAirport(IAirportRepository airportRepository)
|
|
{
|
|
InitializeComponent();
|
|
_airportRepository = airportRepository ?? throw new ArgumentNullException(nameof(airportRepository));
|
|
}
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(textBoxName.Text) || string.IsNullOrWhiteSpace(textBoxLocation.Text))
|
|
{
|
|
throw new Exception("Имеются незаполненные поля");
|
|
}
|
|
if (_airportId.HasValue)
|
|
{
|
|
_airportRepository.UpdateAirport(CreateAirport(_airportId.Value));
|
|
}
|
|
else
|
|
{
|
|
_airportRepository.CreateAirport(CreateAirport(0));
|
|
}
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
|
|
|
private Airport CreateAirport(int id) => Airport.CreateEntity(id, textBoxName.Text, textBoxLocation.Text);
|
|
}
|
|
} |