92 lines
3.1 KiB
C#
92 lines
3.1 KiB
C#
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;
|
|
using ProjectAirline.Repositories;
|
|
using ProjectAirline.Entities;
|
|
|
|
namespace ProjectAirline.Forms
|
|
{
|
|
public partial class FormTicket : Form
|
|
{
|
|
private readonly ITicketRepository _ticketRepository;
|
|
private int? _ticketId;
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var appointment =
|
|
_ticketRepository.ReadTicketById(value);
|
|
if (appointment == null)
|
|
{
|
|
throw new
|
|
InvalidDataException(nameof(appointment));
|
|
}
|
|
comboBoxFlight.SelectedIndex = appointment.FlightID;
|
|
comboBoxPassenger.SelectedIndex = appointment.PassengerID;
|
|
_ticketId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
public FormTicket(ITicketRepository ticketRepository, IPassengerRepository passengerRepository, IFlightRepository flightRepository)
|
|
{
|
|
InitializeComponent();
|
|
_ticketRepository = ticketRepository ??
|
|
throw new ArgumentNullException(nameof(ticketRepository));
|
|
|
|
comboBoxFlight.DataSource = flightRepository.ReadFlights();
|
|
comboBoxFlight.DisplayMember = "AirplaneID";
|
|
comboBoxFlight.ValueMember = "Id";
|
|
|
|
comboBoxPassenger.DataSource = passengerRepository.ReadPassengers();
|
|
comboBoxPassenger.DisplayMember = "FirstName";
|
|
comboBoxPassenger.ValueMember = "Id";
|
|
|
|
}
|
|
|
|
private void ButtonAdd_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (comboBoxPassenger.SelectedIndex < 0 || comboBoxFlight.SelectedIndex < 0 || numericUpDownPrice.Value <= 0)
|
|
{
|
|
throw new Exception("Имеются незаполненны поля");
|
|
}
|
|
if (_ticketId.HasValue)
|
|
{
|
|
_ticketRepository.UpdateTicket(CreateTicket(_ticketId.Value));
|
|
}
|
|
else
|
|
{
|
|
_ticketRepository.CreateTicket(CreateTicket(0));
|
|
}
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e)
|
|
{
|
|
Close();
|
|
}
|
|
private Ticket CreateTicket(int id) => Ticket.CreateTicket(id, comboBoxFlight.SelectedIndex,
|
|
comboBoxPassenger.SelectedIndex, (int)numericUpDownPrice.Value);
|
|
}
|
|
}
|