PIbd-31_Putincev.D.M._COP_29/COP/PortalAccountsView/FormAccount.cs

142 lines
5.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DocumentFormat.OpenXml.Vml.Office;
using PortalAccountsContracts.BindingModels;
using PortalAccountsContracts.BusinessLogicsContracts;
using PortalAccountsContracts.SearchModels;
using PortalAccountsContracts.ViewModels;
using RodionovLibrary.VisualComponents;
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 PortalAccountsView
{
public partial class FormAccount : Form
{
private int? _id;
private bool _isModified = false;
private readonly IAccountLogic _logic;
private readonly IInterestLogic _interestLogic;
private List<InterestViewModel> _interests;
public int Id { set { _id = value; } }
public FormAccount(IAccountLogic logic, IInterestLogic interestLogic)
{
InitializeComponent();
_logic = logic;
_interestLogic = interestLogic;
_interests = new List<InterestViewModel>();
customListBoxInterest.TextChanged += (_, _) => _isModified = true;
textBoxControlEmail.TextChanged += (_, _) => _isModified = true;
textBoxControlEmail.Pattern = @"^[a-z0-9._%+-]+\@([a-z0-9-]+\.)+[a-z]{2,4}$";
textBoxControlEmail.SetTooltipText("example@gmail.com");
}
private void FormAccount_Load(object sender, EventArgs e)
{
_interests = _interestLogic.ReadList(null) ?? throw new Exception("Не удалось получить список ролей");
customListBoxInterest.SetItems(_interests.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;
textBoxAvatar.Text = account.AvatarPath;
textBoxControlEmail.Text = account.Email;
customListBoxInterest.SelectedItem = account.InterestName;
_isModified = false;
}
}
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(customListBoxInterest.SelectedItem))
{
MessageBox.Show("Выберите интерес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new AccountBindingModel
{
Id = _id ?? 0,
Login = textBoxLogin.Text,
Email = textBoxControlEmail.Value,
AvatarPath = textBoxAvatar.Text,
InterestId = _interests.First(x => x.Name == customListBoxInterest.SelectedItem).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 textBoxAvatar_Click(object sender, EventArgs e)
{
using (var dialog = new OpenFileDialog { Filter = "jpg|*.jpg" })
{
if (dialog.ShowDialog() == DialogResult.OK)
{
textBoxAvatar.Text = dialog.FileName.ToString();
}
}
}
}
}