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

118 lines
3.5 KiB
C#
Raw Normal View History

2023-09-06 22:02:39 +04:00
using BlogContracts.BindingModels;
using BlogContracts.BusinessLogicContracts;
using BlogContracts.SearchModels;
2023-09-06 21:16:28 +04:00
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;
2023-09-06 22:02:39 +04:00
namespace Blog
2023-09-06 21:16:28 +04:00
{
public partial class FormAddTopics : Form
{
private readonly ICategoryLogic _categoryLogic;
private readonly ITopicLogic _topicLogic;
private int? _id;
public int Id { set { _id = value; } }
public FormAddTopics(ITopicLogic topicLogic, ICategoryLogic categoryLogic)
{
InitializeComponent();
_topicLogic = topicLogic;
_categoryLogic = categoryLogic;
}
private void FormAddTopic_Load(object sender, EventArgs e)
{
LoadData();
try
{
var view = _categoryLogic.ReadElement(new CategorySearchModel { Id = _id.Value });
if (view != null)
{
textBoxCategory.Text = view.Name;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCreate_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormAddTopic));
if (service is FormAddTopic form)
{
form.Id = _id ?? 0;
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
private void LoadData()
{
try
{
var list = _topicLogic.ReadList(new TopicSearchModel { CategoryId = _id });
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["CategoryId"].Visible = false;
dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
}
catch (Exception)
{ }
}
private void buttonChange_Click(object sender, EventArgs e)
{
}
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 (!_topicLogic.Delete(new TopicBindingModel
{
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();
}
}
}