PIbd-22-Ismailov_SUBD/BlogDataModels/BlogViewModel/FormRole.cs

103 lines
3.0 KiB
C#
Raw Normal View History

2023-09-06 21:16:28 +04:00
using ForumContracts.BindingModels;
using ForumContracts.BusinessLogicContracts;
using ForumContracts.SearchModels;
using Microsoft.EntityFrameworkCore.Diagnostics;
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 FormRole : Form
{
private readonly IRoleLogic _roleLogic;
private int? _id;
public int Id { set { _id = value; } }
public FormRole(IRoleLogic roleLogic)
{
InitializeComponent();
_roleLogic = roleLogic;
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxRoleName.Text))
{
MessageBox.Show("Введите название роли", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
try
{
var model = new RoleBindingModel
{
Id = _id ?? 0,
Name = textBoxRoleName.Text
};
var operationResult = _id.HasValue ? _roleLogic.Update(model) : _roleLogic.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 FormRole_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _roleLogic.ReadElement(new RoleSearchModel { Id = _id.Value });
if (view != null)
{
textBoxRoleName.Text = view.Name;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void buttonAddRoles_Click(object sender, EventArgs e)
{
try
{
_roleLogic.RoleInsertList(1000);
FormRole_Load(sender, e);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}