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;
private int? _driverId;
public int Id
{
set
{
try
{
var driver = _driverRepository.ReadDriverByID(value);
if (driver == null)
{
throw new
InvalidDataException(nameof(driver));
}
textBoxFirstName.Text = driver.First_name;
textBoxLastName.Text = driver.Last_name;
2024-11-20 00:53:28 +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-11-20 00:53:28 +04:00
comboBoxTruckID.DataSource = truckRepository.ReadTrucks();//
comboBoxTruckID.DisplayMember = "Numbers";
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-11-20 00:53:28 +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-03 09:30:28 +04:00
textBoxLastName.Text, (int)comboBoxTruckID.SelectedIndex!);//
}
}