using ForumContracts.BindingModels; using ForumContracts.BusinessLogicContracts; using ForumContracts.SearchModels; 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 Forum { public partial class FormUser : Form { private readonly IUserLogic _userLogic; private readonly IRoleLogic _roleLogic; private int? _id; public int Id { set { _id = value; } } public FormUser(IRoleLogic roleLogic, IUserLogic userLogic) { InitializeComponent(); _roleLogic = roleLogic; _userLogic = userLogic; } private void buttonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxUsername.Text)) { MessageBox.Show("Заполните поле Username", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(textBoxLogin.Text)) { MessageBox.Show("Заполните поле Login", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(textBoxPassword.Text)) { MessageBox.Show("Заполните поле Password", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (comboBoxSelectRole.SelectedValue == null) { MessageBox.Show("Выберите Role", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { var model = new UserBindingModel { Id = _id ?? 0, Username = textBoxUsername.Text, Email = textBoxLogin.Text, Password = textBoxPassword.Text, RegistrationDate = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), RoleId = Convert.ToInt32(comboBoxSelectRole.SelectedValue), }; var operationResult = _id.HasValue ? _userLogic.Update(model) : _userLogic.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 buttonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } private void FormUser_Load(object sender, EventArgs e) { LoadData(); if (_id.HasValue) { try { var view = _userLogic.ReadElement(new UserSearchModel { Id = _id.Value }); if (view != null) { textBoxLogin.Text = view.Email; textBoxPassword.Text = view.Password; textBoxUsername.Text = view.Username; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void LoadData() { try { var role = _roleLogic.ReadList(null); if (role != null) { comboBoxSelectRole.DisplayMember = "Name"; comboBoxSelectRole.ValueMember = "Id"; comboBoxSelectRole.DataSource = role; comboBoxSelectRole.SelectedItem = null; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void buttonAddUsers_Click(object sender, EventArgs e) { try { _userLogic.UserInsertList(1000, _roleLogic.ReadList(null)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }