79 lines
2.7 KiB
C#
79 lines
2.7 KiB
C#
using ProjectAirline.Entities;
|
|
using ProjectAirline.Repositories;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace ProjectAirline.Forms
|
|
{
|
|
public partial class FormAirplane : Form
|
|
{
|
|
private readonly IAirplaneRepository _airplaneRepository;
|
|
private int? _airplaneId;
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var airplane = _airplaneRepository.ReadAirplaneById(value);
|
|
if (airplane == null)
|
|
{
|
|
throw new InvalidDataException(nameof(airplane));
|
|
}
|
|
textBoxCountry.Text = airplane.Country;
|
|
textBoxModel.Text = airplane.Model;
|
|
numericUpDownCapacity.Value = airplane.Capacity;
|
|
_airplaneId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
public FormAirplane(IAirplaneRepository airplaneRepository)
|
|
{
|
|
InitializeComponent();
|
|
_airplaneRepository = airplaneRepository ?? throw new ArgumentNullException(nameof(airplaneRepository));
|
|
}
|
|
|
|
private void ButtonAdd_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(textBoxCountry.Text) || string.IsNullOrWhiteSpace(textBoxModel.Text))
|
|
{
|
|
throw new Exception("Имеются незаполненные поля");
|
|
}
|
|
if (_airplaneId.HasValue)
|
|
{
|
|
_airplaneRepository.UpdateAirplane(CreateAirplane(_airplaneId.Value));
|
|
}
|
|
else
|
|
{
|
|
_airplaneRepository.CreateAirplane(CreateAirplane(0));
|
|
}
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
|
|
|
private Airplane CreateAirplane(int id) => Airplane.CreateEntity(id, textBoxCountry.Text,
|
|
textBoxModel.Text, Convert.ToInt32(numericUpDownCapacity.Value));
|
|
|
|
}
|
|
}
|