PIbd-23_Baryshev_D.A._Garage/ProjectGarage/Forms/FormDriver.cs

88 lines
3.0 KiB
C#
Raw Normal View History

2024-11-17 16:52:57 +04:00
using ProjectGarage.Entities;
using ProjectGarage.Entities.Enums;
using ProjectGarage.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 ProjectGarage.Forms
{
public partial class FormDriver : Form
{
2024-11-17 16:52:57 +04:00
private readonly IDriverRepository _driverRepository;
2024-12-17 19:58:30 +04:00
2024-11-17 16:52:57 +04:00
private int? _driverId;
public int Id
{
set
{
try
{
var driver = _driverRepository.ReadDriverByID(value);
if (driver == null)
{
throw new
InvalidDataException(nameof(driver));
}
2024-12-03 23:06:20 +04:00
textBoxFirstName.Text = driver.Fname;
textBoxLastName.Text = driver.Lname;
2024-12-17 19:58:30 +04:00
comboBoxTruckID.SelectedItem = driver.TruckId;
2024-11-17 16:52:57 +04:00
_driverId = value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
2024-11-20 00:53:28 +04:00
public FormDriver(IDriverRepository driverRepository, ITruckRepository truckRepository)
{
InitializeComponent();
2024-11-17 16:52:57 +04:00
_driverRepository = driverRepository ?? throw new ArgumentNullException(nameof(driverRepository));
2024-12-03 23:06:20 +04:00
comboBoxTruckID.DataSource = truckRepository.ReadTrucks();
2024-12-18 02:25:21 +04:00
comboBoxTruckID.DisplayMember = "TruckInfo";
2024-11-20 00:53:28 +04:00
comboBoxTruckID.ValueMember = "Id";
}
2024-11-17 16:52:57 +04:00
private void ButtonSaveDriver_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(textBoxFirstName.Text) || string.IsNullOrWhiteSpace(textBoxLastName.Text)
2024-12-03 23:06:20 +04:00
|| comboBoxTruckID.SelectedIndex < 0)
2024-11-17 16:52:57 +04:00
{
throw new Exception("Имеются незаполненные поля");
}
if (_driverId.HasValue)
{
_driverRepository.UpdateDriver(CreateDriver(_driverId.Value));
}
else
{
_driverRepository.CreateDriver(CreateDriver(0));
}
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2024-11-20 00:53:28 +04:00
private void ButtonCancelDriver_Click(object sender, EventArgs e) => Close();
2024-11-17 16:52:57 +04:00
private Driver CreateDriver(int id) => Driver.CreateDriver(id, textBoxFirstName.Text,
2024-12-17 19:58:30 +04:00
textBoxLastName.Text, (int)comboBoxTruckID.SelectedIndex);
}
}