80 lines
2.6 KiB
C#
80 lines
2.6 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 FormFuel : Form
|
|
{
|
|
private readonly IFuelRepository _fuelRepository;
|
|
|
|
private int? _fuelId;
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var fuel = _fuelRepository.ReadFuelByID(value);
|
|
if (fuel == null)
|
|
throw new InvalidOperationException(nameof(fuel));
|
|
|
|
comboBoxFuelType.SelectedItem = fuel.Fuel_Type;
|
|
numericUpDownPrice.Value = (decimal)fuel.Price_Per_Liter;
|
|
numericUpDownAmount.Value = (decimal)fuel.Amount;
|
|
|
|
_fuelId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
public FormFuel(IFuelRepository fuelRepository)
|
|
{
|
|
InitializeComponent();
|
|
_fuelRepository = fuelRepository ??
|
|
throw new ArgumentNullException(nameof(fuelRepository));
|
|
|
|
comboBoxFuelType.DataSource = Enum.GetValues(typeof(Fuel_Type));
|
|
}
|
|
private void ButtonFuelSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (comboBoxFuelType.SelectedIndex < 1)
|
|
throw new Exception("Имеются незаполненные поля");
|
|
|
|
if (_fuelId.HasValue)
|
|
_fuelRepository.UpdateFuel(CreateFuel(_fuelId.Value));
|
|
else
|
|
_fuelRepository.CreateFuel(CreateFuel(0));
|
|
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
|
|
|
private Fuel CreateFuel(int id)
|
|
{
|
|
return Fuel.CreateEntity(id, (Fuel_Type)comboBoxFuelType.SelectedItem!, (float)numericUpDownPrice.Value, (float)numericUpDownAmount.Value);
|
|
}
|
|
}
|
|
}
|
|
|