149 lines
4.8 KiB
C#
149 lines
4.8 KiB
C#
using NewsBlogAbstractions.Models;
|
||
using NewsBlogAbstractions.WorkAbstractions;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.ComponentModel.Design;
|
||
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 FormArticle : Form
|
||
{
|
||
private readonly IArticleWork _articleLogic;
|
||
private readonly IAuthorWork _authorLogic;
|
||
private readonly ICategoryWork _categoryLogic;
|
||
public FormArticle(IArticleWork articleLogic, IAuthorWork authorLogic, ICategoryWork categoryLogic)
|
||
{
|
||
InitializeComponent();
|
||
_articleLogic = articleLogic;
|
||
_authorLogic = authorLogic;
|
||
_categoryLogic = categoryLogic;
|
||
}
|
||
|
||
private void FormArticle_Load(object sender, EventArgs e)
|
||
{
|
||
LoadData();
|
||
}
|
||
|
||
private void ButtonCreate_Click(object sender, EventArgs e)
|
||
{
|
||
Article newArticle = new()
|
||
{
|
||
Title = textBoxTitle.Text,
|
||
Content = textBoxContent.Text,
|
||
PublicationDate = DateTime.Now,
|
||
AuthorId = ((Author?)comboBoxAuthor.SelectedItem)?.Id ?? 0,
|
||
CategoryId = ((Category?)comboBoxCategory.SelectedItem)?.Id ?? 0,
|
||
};
|
||
|
||
_articleLogic.Create(newArticle);
|
||
|
||
LoadData();
|
||
}
|
||
|
||
private void LoadData()
|
||
{
|
||
var articles = _articleLogic.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("AuthorId", "AuthorId");
|
||
dataGridView.Columns.Add("CategoryId", "CategoryId");
|
||
dataGridView.Columns["AuthorId"].Visible = false;
|
||
dataGridView.Columns["CategoryId"].Visible = false;
|
||
dataGridView.Columns.Add("Author", "Автор");
|
||
dataGridView.Columns.Add("Category", "Категория");
|
||
}
|
||
|
||
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["Author"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
|
||
dataGridView.Columns["Category"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
|
||
|
||
comboBoxAuthor.DataSource = _authorLogic.GetAll();
|
||
comboBoxAuthor.DisplayMember = "Author";
|
||
comboBoxAuthor.ValueMember = "Name";
|
||
|
||
comboBoxCategory.DataSource = _categoryLogic.GetAll();
|
||
comboBoxCategory.DisplayMember = "Category";
|
||
comboBoxCategory.ValueMember = "Name";
|
||
|
||
foreach (var article in articles)
|
||
{
|
||
dataGridView.Rows.Add(article.Id, article.Title, article.Content, article.PublicationDate.ToString("d"),
|
||
article.AuthorId, article.CategoryId, _authorLogic.Get(article.AuthorId)?.Name, _categoryLogic.Get(article.CategoryId)?.Name);
|
||
}
|
||
}
|
||
|
||
private void ButtonUpdate_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count > 0)
|
||
{
|
||
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
|
||
int articleId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
|
||
|
||
Article updatedArticle = new()
|
||
{
|
||
Id = articleId,
|
||
Title = textBoxTitle.Text,
|
||
Content = textBoxContent.Text,
|
||
PublicationDate = DateTime.Now,
|
||
AuthorId = ((Author?)comboBoxAuthor.SelectedItem)?.Id ?? 0,
|
||
CategoryId = ((Category?)comboBoxCategory.SelectedItem)?.Id ?? 0,
|
||
};
|
||
|
||
_articleLogic.Update(updatedArticle);
|
||
|
||
LoadData();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Пожалуйста, выберите статью, информацию о которой необходимо обновить");
|
||
}
|
||
}
|
||
|
||
private void ButtonDelete_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count > 0)
|
||
{
|
||
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
|
||
int articleId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
|
||
|
||
_articleLogic.Delete(articleId);
|
||
|
||
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();
|
||
comboBoxAuthor.SelectedValue = row.Cells["Author"].Value ?? new Author { Name = "Неизвестный автор" };
|
||
comboBoxCategory.SelectedValue = row.Cells["Category"].Value ?? new Category { Name = "Неизвестная категория" };
|
||
}
|
||
}
|
||
}
|
||
}
|