ISEbd-21_Sharonov_I_A_Proje.../ProjectAirline/Forms/FormFlight.cs

80 lines
2.3 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 FormFlight : Form
{
private readonly IFlightRepository _flightRepository;
private int? _flightId;
public int Id
{
set
{
try
{
var flight = _flightRepository.ReadFlightById(value);
if (flight == null)
{
throw new InvalidDataException(nameof(flight));
}
textBoxArrivalLocation.Text = flight.ArrivalLocation;
numericUpDownTicketPrice.Value = flight.TicketPrice;
_flightId = value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
public FormFlight(IFlightRepository flightRepository)
{
InitializeComponent();
_flightRepository = flightRepository ?? throw new ArgumentNullException(nameof(flightRepository));
}
private void ButtonSave_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(textBoxArrivalLocation.Text))
{
throw new Exception("Имеются незаполненные поля");
}
if (_flightId.HasValue)
{
_flightRepository.UpdateFlight(CreateFlight(_flightId.Value));
}
else
{
_flightRepository.CreateFlight(CreateFlight(0));
}
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private Flight CreateFlight(int id) => Flight.CreateOperation(id, DateTime.Now, DateTime.Now.AddHours(2), textBoxArrivalLocation.Text, Convert.ToInt32(numericUpDownTicketPrice.Value));
}