97 lines
3.5 KiB
C#
97 lines
3.5 KiB
C#
using ProjectFuel.Entities;
|
|
using ProjectFuel.Entities.Enums;
|
|
using ProjectFuel.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 ProjectFuel.Forms_
|
|
{
|
|
public partial class FormCar : Form
|
|
{
|
|
private readonly ICarRepository _carRepository;
|
|
|
|
private int? _carId;
|
|
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var car = _carRepository.ReadCarByID(value);
|
|
if (car == null)
|
|
throw new InvalidOperationException(nameof(car));
|
|
|
|
foreach (Driver_License elem in Enum.GetValues(typeof(Driver_License)))
|
|
{
|
|
if ((elem & car.License) != 0)
|
|
{
|
|
checkedListBoxDriverLicense.SetItemChecked(checkedListBoxDriverLicense.Items.IndexOf(elem), true);
|
|
}
|
|
}
|
|
textBoxCarMark.Text = car.Car_Mark;
|
|
textBoxCarModel.Text = car.Car_Model;
|
|
comboBoxCarType.SelectedItem = car.Car_Type;
|
|
numericUpDownConsumptionRate.Value = (decimal)car.Consumption_Rate;
|
|
|
|
_carId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
public FormCar(ICarRepository carRepository)
|
|
{
|
|
InitializeComponent();
|
|
_carRepository = carRepository ??
|
|
throw new ArgumentNullException(nameof(carRepository));
|
|
|
|
foreach (var elem in Enum.GetValues(typeof(Driver_License)))
|
|
checkedListBoxDriverLicense.Items.Add(elem);
|
|
|
|
comboBoxCarType.DataSource = Enum.GetValues(typeof(Car_Type));
|
|
}
|
|
private void ButtonCarSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(textBoxCarMark.Text) || string.IsNullOrWhiteSpace(textBoxCarModel.Text) || comboBoxCarType.SelectedIndex < 1 || checkedListBoxDriverLicense.CheckedItems.Count == 0)
|
|
throw new Exception("Имеются незаполненные поля");
|
|
|
|
if (_carId.HasValue)
|
|
_carRepository.UpdateCar(CreateCar(_carId.Value));
|
|
else
|
|
_carRepository.CreateCar(CreateCar(0));
|
|
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
|
|
|
private Car CreateCar(int id)
|
|
{
|
|
Driver_License driver_License = Driver_License.None;
|
|
foreach (var elem in checkedListBoxDriverLicense.CheckedItems)
|
|
{
|
|
driver_License |= (Driver_License)elem;
|
|
}
|
|
|
|
return Car.CreateEntity(id, textBoxCarMark.Text, textBoxCarModel.Text, (Car_Type)comboBoxCarType.SelectedItem!, driver_License, (float)numericUpDownConsumptionRate.Value);
|
|
}
|
|
}
|
|
}
|