ISEbd-21_Sharonov_I_A_Proje.../ProjectAirline/Forms/FormPlane.cs
2024-11-27 00:48:24 +04:00

79 lines
2.2 KiB
C#

using ProjectAirline.Repositories;
using ProjectAirline.Entities;
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 FormPlane : Form
{
private readonly IPlaneRepository _planeRepository;
private int? _planeId;
public int Id
{
set
{
try
{
var plane = _planeRepository.ReadPlaneById(value);
if (plane == null)
{
throw new InvalidDataException(nameof(plane));
}
textBoxName.Text = plane.Name;
numericUpDownCapacity.Value = plane.Capacity;
_planeId = value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
public FormPlane(IPlaneRepository planeRepository)
{
InitializeComponent();
_planeRepository = planeRepository ?? throw new ArgumentNullException(nameof(planeRepository));
}
private void ButtonSave_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(textBoxName.Text))
{
throw new Exception("Имеются незаполненные поля");
}
if (_planeId.HasValue)
{
_planeRepository.UpdatePlane(CreatePlane(_planeId.Value));
}
else
{
_planeRepository.CreatePlane(CreatePlane(0));
}
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private Plane CreatePlane(int id) => Plane.CreatePlane(id, textBoxName.Text, Convert.ToInt32(numericUpDownCapacity.Value));
}