2024-11-27 00:48:24 +04:00
|
|
|
|
using ProjectAirline.Entities;
|
|
|
|
|
using ProjectAirline.Repositories;
|
|
|
|
|
using System;
|
2024-11-13 00:12:08 +04:00
|
|
|
|
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;
|
|
|
|
|
|
2024-11-27 00:48:24 +04:00
|
|
|
|
namespace ProjectAirline.Forms;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public partial class FormFlight : Form
|
2024-11-13 00:12:08 +04:00
|
|
|
|
{
|
2024-11-27 00:48:24 +04:00
|
|
|
|
private readonly IFlightRepository _flightRepository;
|
|
|
|
|
private int? _flightId;
|
|
|
|
|
|
|
|
|
|
public int Id
|
2024-11-13 00:12:08 +04:00
|
|
|
|
{
|
2024-11-27 00:48:24 +04:00
|
|
|
|
set
|
2024-11-13 00:12:08 +04:00
|
|
|
|
{
|
2024-11-27 00:48:24 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
2024-11-13 00:12:08 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-27 00:48:24 +04:00
|
|
|
|
|
|
|
|
|
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.CreateFlight(id, DateTime.Now, DateTime.Now.AddHours(2), textBoxArrivalLocation.Text, Convert.ToInt32(numericUpDownTicketPrice.Value));
|
2024-11-13 00:12:08 +04:00
|
|
|
|
}
|