PIbd-21_Danilov_V.V._SUBD/NewsBlog/NewsBlogView/FormComment.cs

134 lines
3.9 KiB
C#
Raw Normal View History

2024-05-19 21:59:55 +04:00
using NewsBlogAbstractions.Models;
using NewsBlogAbstractions.WorkAbstractions;
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 NewsBlogView
{
public partial class FormComment : Form
{
private readonly ICommentWork _commentLogic;
private readonly IArticleWork _articleLogic;
public FormComment(ICommentWork commentLogic, IArticleWork articleLogic)
{
InitializeComponent();
_commentLogic = commentLogic;
_articleLogic = articleLogic;
}
private void FormComment_Load(object sender, EventArgs e)
{
LoadData();
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Comment newComment = new()
{
Title = textBoxTitle.Text,
Content = textBoxContent.Text,
CreateDate = DateTime.Now,
ArticleId = ((Article?)comboBoxArticle.SelectedItem)?.Id ?? 0,
};
_commentLogic.Create(newComment);
LoadData();
}
private void LoadData()
{
var comments = _commentLogic.GetAll();
dataGridView.Rows.Clear();
if (dataGridView.ColumnCount == 0)
{
dataGridView.Columns.Add("Id", "ID");
dataGridView.Columns.Add("Title", "Заголовок");
dataGridView.Columns.Add("Content", "Содержимое");
dataGridView.Columns.Add("Date", "Дата написания");
dataGridView.Columns.Add("ArticleId", "ArticleId");
dataGridView.Columns["ArticleId"].Visible = false;
dataGridView.Columns.Add("Article", "Статья");
}
dataGridView.Columns["Id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Title"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Content"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Date"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Article"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
comboBoxArticle.DataSource = _articleLogic.GetAll();
comboBoxArticle.DisplayMember = "Article";
comboBoxArticle.ValueMember = "Title";
foreach (var comment in comments)
{
dataGridView.Rows.Add(comment.Id, comment.Title, comment.Content, comment.CreateDate.ToString("d"), comment.ArticleId, _articleLogic.Get(comment.ArticleId)?.Title);
}
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int commentId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
Comment updatedComment = new()
{
Id = commentId,
Title = textBoxTitle.Text,
Content = textBoxContent.Text,
CreateDate = DateTime.Now,
ArticleId = ((Article?)comboBoxArticle.SelectedItem)?.Id ?? 0,
};
_commentLogic.Update(updatedComment);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите комментарий, информацию о котором необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int commentId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_commentLogic.Delete(commentId);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите комментарий, информацию о котором необходимо удалить");
}
}
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
textBoxTitle.Text = row.Cells["Title"].Value.ToString();
textBoxContent.Text = row.Cells["Content"].Value.ToString();
comboBoxArticle.SelectedValue = row.Cells["Article"].Value;
}
}
}
}