using AccountsContracts.BindingModels; using AccountsContracts.BusinessLogicContracts; using AccountsContracts.SearchModels; using AccountsContracts.ViewModels; using AccountsDataModels.Models; 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 AccountsApp { public partial class FormAccount : Form { private int? _id; private readonly IAccountLogic _logic; private readonly IInterestLogic _interestLogic; private List _interests; public int Id { set { _id = value; } } public FormAccount(IAccountLogic logic, IInterestLogic interestLogic) { InitializeComponent(); _logic = logic; _interestLogic = interestLogic; _interests = new List(); } private void FormAccount_Load(object sender, EventArgs e) { _interests = _interestLogic.ReadList(null); abazovCheckedListBox.addItems(_interests.Select(x => x.Name).ToList()); mailControl.validateEmailRegex = new System.Text.RegularExpressions.Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); mailControl.setTooltipText("name@example.com"); if (_id.HasValue) { try { var view = _logic.ReadElement(new AccountSearchModel { Id = _id.Value }); if (view != null) { textBoxLogin.Text = view.Login; textBoxPassword.Text = view.Password; mailControl.Email = view.Email; abazovCheckedListBox.selectedItem = view.InterestName; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void buttonCancel_Click(object sender, EventArgs e) { 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(textBoxPassword.Text)) { MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(mailControl.Email)) { MessageBox.Show("Заполните почту или исправьте написание почты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (abazovCheckedListBox.selectedItem == null) { MessageBox.Show("Заполните интересы", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { var model = new AccountBindingModel { Id = _id ?? 0, Login = textBoxLogin.Text, Password = textBoxPassword.Text, Email = mailControl.Email, InterestId = _interests.First(x => x.Name == abazovCheckedListBox.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); } } } }