ISEbd-22_Rozhkov.I.E._Simple/GasStation/Forms/FormGasman.cs

99 lines
3.1 KiB
C#
Raw Normal View History

2024-11-13 23:20:06 +04:00
using GasStation.Entities;
2024-12-02 14:38:24 +04:00
using GasStation.Entities.Enums;
2024-11-13 23:20:06 +04:00
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));
2024-12-02 14:38:24 +04:00
foreach (var elem in Enum.GetValues(typeof(Post)))
{
checkedListBoxPost.Items.Add(elem);
}
2024-11-13 23:20:06 +04:00
}
public int ID
{
set
{
try
{
var gasman = _gasmanRepository.ReadGasmanByID(value);
2024-11-15 18:27:06 +04:00
if (gasman == null)
2024-11-13 23:20:06 +04:00
{
throw new InvalidDataException(nameof(gasman));
}
2024-12-02 14:38:24 +04:00
foreach (Post elem in Enum.GetValues(typeof(Post)))
{
if ((elem & gasman.Post) != 0)
{
checkedListBoxPost.SetItemChecked(checkedListBoxPost.Items.IndexOf(elem), true);
}
}
2024-11-13 23:20:06 +04:00
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) ||
2024-12-02 14:38:24 +04:00
string.IsNullOrWhiteSpace(textBoxNumber.Text) ||
checkedListBoxPost.Items.Count == 0)
2024-11-13 23:20:06 +04:00
{
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();
2024-12-02 14:38:24 +04:00
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);
}
2024-11-13 23:20:06 +04:00
}
}