99 lines
3.1 KiB
C#
99 lines
3.1 KiB
C#
using GasStation.Entities;
|
|
using GasStation.Entities.Enums;
|
|
using GasStation.Repositories;
|
|
|
|
|
|
namespace GasStation.Forms
|
|
{
|
|
public partial class FormGasman : Form
|
|
{
|
|
private readonly IGasmanRepository _gasmanRepository;
|
|
|
|
private int? _gasmanId;
|
|
|
|
public FormGasman(IGasmanRepository gasmanRepository)
|
|
{
|
|
InitializeComponent();
|
|
_gasmanRepository = gasmanRepository ??
|
|
throw new ArgumentNullException(nameof(gasmanRepository));
|
|
|
|
foreach (var elem in Enum.GetValues(typeof(Post)))
|
|
{
|
|
checkedListBoxPost.Items.Add(elem);
|
|
}
|
|
}
|
|
|
|
public int ID
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var gasman = _gasmanRepository.ReadGasmanByID(value);
|
|
if (gasman == null)
|
|
{
|
|
throw new InvalidDataException(nameof(gasman));
|
|
}
|
|
|
|
foreach (Post elem in Enum.GetValues(typeof(Post)))
|
|
{
|
|
if ((elem & gasman.Post) != 0)
|
|
{
|
|
checkedListBoxPost.SetItemChecked(checkedListBoxPost.Items.IndexOf(elem), true);
|
|
}
|
|
}
|
|
|
|
textBoxName.Text = gasman.GasmanName;
|
|
textBoxNumber.Text = gasman.PhoneNumber;
|
|
_gasmanId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(textBoxName.Text) ||
|
|
string.IsNullOrWhiteSpace(textBoxNumber.Text) ||
|
|
checkedListBoxPost.Items.Count == 0)
|
|
{
|
|
throw new Exception("Имеются незаполненые поля");
|
|
}
|
|
if (_gasmanId.HasValue)
|
|
{
|
|
_gasmanRepository.UpdateGasman(CreateGasman(_gasmanId.Value));
|
|
}
|
|
else
|
|
{
|
|
_gasmanRepository.CreateGasman(CreateGasman(0));
|
|
}
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
|
|
|
private Gasman CreateGasman(int id)
|
|
{
|
|
Post post = Post.None;
|
|
foreach (var elem in checkedListBoxPost.CheckedItems)
|
|
{
|
|
post |= (Post)elem;
|
|
}
|
|
|
|
return Gasman.CreateGasman(id, textBoxName.Text, textBoxNumber.Text, post);
|
|
}
|
|
}
|
|
}
|