140 lines
5.0 KiB
C#
140 lines
5.0 KiB
C#
using PortalAccountsContracts.BindingModels;
|
||
using PortalAccountsContracts.BusinessLogicsContracts;
|
||
using PortalAccountsContracts.SearchModels;
|
||
using PortalAccountsContracts.ViewModels;
|
||
using System.Data;
|
||
|
||
namespace PortalAccountsView
|
||
{
|
||
public partial class FormAccount : Form
|
||
{
|
||
private int? _id;
|
||
|
||
private bool _isModified = false;
|
||
|
||
private readonly IAccountLogic _logic;
|
||
|
||
private readonly IRoleLogic _roleLogic;
|
||
|
||
private List<RoleViewModel> _roles;
|
||
|
||
public int Id { set { _id = value; } }
|
||
|
||
public FormAccount(IAccountLogic logic, IRoleLogic roleLogic)
|
||
{
|
||
InitializeComponent();
|
||
_logic = logic;
|
||
_roleLogic = roleLogic;
|
||
_roles = new List<RoleViewModel>();
|
||
comboBoxControlRole.ValueChanged += (_, _) => _isModified = true;
|
||
controlInputRating.ElementChanged += (_, _) => _isModified = true;
|
||
}
|
||
|
||
private void FormAccount_Load(object sender, EventArgs e)
|
||
{
|
||
_roles = _roleLogic.ReadList(null) ?? throw new Exception("Не удалось получить список ролей");
|
||
comboBoxControlRole.AddItems(_roles.Select(x => x.Name).ToList());
|
||
if (_id.HasValue)
|
||
{
|
||
try
|
||
{
|
||
var account = _logic.ReadElement(new AccountSearchModel { Id = _id.Value });
|
||
if (account != null)
|
||
{
|
||
textBoxLogin.Text = account.Login;
|
||
textBoxWarnings.Text = account.Warnings;
|
||
controlInputRating.Value = account.Rating;
|
||
comboBoxControlRole.SelectedValue = account.RoleName;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void ButtonCancel_Click(object sender, EventArgs e)
|
||
{
|
||
if (_isModified)
|
||
{
|
||
var result = MessageBox.Show(
|
||
"У вас есть несохранённые изменения. Вы действительно хотите закрыть форму?",
|
||
"Предупреждение",
|
||
MessageBoxButtons.YesNo,
|
||
MessageBoxIcon.Warning
|
||
);
|
||
|
||
if (result == DialogResult.No)
|
||
return;
|
||
}
|
||
|
||
DialogResult = DialogResult.Cancel;
|
||
Close();
|
||
}
|
||
|
||
private void ButtonSave_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(textBoxLogin.Text))
|
||
{
|
||
MessageBox.Show("Заполните логин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(comboBoxControlRole.SelectedValue))
|
||
{
|
||
MessageBox.Show("Выберите роль", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
var model = new AccountBindingModel
|
||
{
|
||
Id = _id ?? 0,
|
||
Login = textBoxLogin.Text,
|
||
Warnings = textBoxWarnings.Text,
|
||
Rating = controlInputRating.Value,
|
||
RoleId = _roles.First(x => x.Name == comboBoxControlRole.SelectedValue).Id,
|
||
};
|
||
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
|
||
if (!operationResult)
|
||
{
|
||
throw new Exception("Ошибка при сохранении");
|
||
}
|
||
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
DialogResult = DialogResult.OK;
|
||
Close();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
private void TextBoxLogin_TextChanged(object sender, EventArgs e)
|
||
{
|
||
_isModified = true;
|
||
}
|
||
|
||
private void TextBoxWarnings_TextChanged(object sender, EventArgs e)
|
||
{
|
||
_isModified = true;
|
||
}
|
||
|
||
private void FormAccount_FormClosing(object sender, FormClosingEventArgs e)
|
||
{
|
||
if (!_isModified)
|
||
return;
|
||
|
||
var result = MessageBox.Show(
|
||
"У вас есть несохранённые изменения. Вы действительно хотите закрыть форму?",
|
||
"Предупреждение",
|
||
MessageBoxButtons.YesNo,
|
||
MessageBoxIcon.Warning
|
||
);
|
||
|
||
if (result == DialogResult.No)
|
||
e.Cancel = true;
|
||
}
|
||
}
|
||
}
|