PIbd-22-Ismailov_SUBD/BlogDataModels/BlogViewModel/FormMain.cs
2023-09-06 21:16:28 +04:00

147 lines
4.7 KiB
C#

using ForumBusinessLogic;
using ForumContracts.BindingModels;
using ForumContracts.BusinessLogicContracts;
using System.Windows.Forms;
namespace Forum
{
public partial class FormMain : Form
{
private readonly IMessageLogic _messageLogic;
public FormMain(IMessageLogic messageLogic)
{
InitializeComponent();
_messageLogic = messageLogic;
}
private void ðîëèToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormRoles));
if (service is FormRoles form)
{
form.ShowDialog();
}
}
private void ïîëüçîâàòåëèToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormUsers));
if (service is FormUsers form)
{
form.ShowDialog();
}
}
private void êàòåãîðèèToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormCategories));
if (service is FormCategories form)
{
form.ShowDialog();
}
}
private void buttonCreate_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormCreateMessage));
if (service is FormCreateMessage form)
{
form.ShowDialog();
LoadData();
}
}
private void FormMain_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var list = _messageLogic.ReadList(null);
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["UserId"].Visible = false;
dataGridView.Columns["TopicId"].Visible = false;
dataGridView.Columns["Username"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["TopicName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Text"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Date"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Óäàëèòü çàïèñü?", "Âîïðîñ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
try
{
if (!_messageLogic.Delete(new MessageBindingModel
{
Id = id
}))
{
throw new Exception("Îøèáêà ïðè óäàëåíèè.");
}
LoadData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
LoadData();
}
private void buttonChange_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var service = Program.ServiceProvider?.GetService(typeof(FormCreateMessage));
if (service is FormCreateMessage form)
{
form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}
private void òåñòûToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormTests));
if (service is FormTests form)
{
form.ShowDialog();
}
}
}
}