191 lines
6.3 KiB
C#
191 lines
6.3 KiB
C#
using NewsBlogAbstractions.Models;
|
||
using NewsBlogAbstractions.WorkAbstractions;
|
||
using NewsBlogMongoDB.Models;
|
||
using NewsBlogMongoDB.StoragesContracts;
|
||
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;
|
||
private readonly StorageModel _mongoLogic;
|
||
public FormArticle(IArticleWork articleLogic, IAuthorWork authorLogic, ICategoryWork categoryLogic, StorageModel mongoLogic)
|
||
{
|
||
InitializeComponent();
|
||
_articleLogic = articleLogic;
|
||
_authorLogic = authorLogic;
|
||
_categoryLogic = categoryLogic;
|
||
_mongoLogic = mongoLogic;
|
||
}
|
||
|
||
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,
|
||
};
|
||
|
||
if (Program.isPostgreSQL)
|
||
_articleLogic.Create(newArticle);
|
||
else
|
||
_mongoLogic.AddArticle(newArticle);
|
||
|
||
LoadData();
|
||
}
|
||
|
||
private void LoadData()
|
||
{
|
||
var articlesSql = _articleLogic.GetAll();
|
||
|
||
var articlesMongo = _mongoLogic.GetArticles();
|
||
|
||
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";
|
||
|
||
if(Program.isPostgreSQL)
|
||
foreach (var article in articlesSql)
|
||
{
|
||
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);
|
||
}
|
||
else
|
||
foreach (var article in articlesMongo)
|
||
{
|
||
dataGridView.Rows.Add(article.Id, article.Title, article.Content, article.Publication_date,
|
||
article.Author_id, article.Category_id, _mongoLogic.GetAuthorById(article.Author_id)?.Name, _mongoLogic.GetCategoryById(article.Category_id)?.Name);
|
||
}
|
||
}
|
||
|
||
private void ButtonUpdate_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count > 0)
|
||
{
|
||
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
|
||
|
||
if (Program.isPostgreSQL)
|
||
{
|
||
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);
|
||
}
|
||
else
|
||
{
|
||
string? articleId = selectedRow.Cells["Id"].Value.ToString();
|
||
|
||
ArticleMongo updated = new()
|
||
{
|
||
Id = articleId,
|
||
Title = textBoxTitle.Text,
|
||
Content = textBoxContent.Text,
|
||
Publication_date = DateTime.Now.ToString("d"),
|
||
Author_id = _mongoLogic.GetMongoId("Author", ((Author?)comboBoxAuthor.SelectedItem)?.Id.ToString() ?? string.Empty),
|
||
Category_id = _mongoLogic.GetMongoId("Category", ((Category?)comboBoxCategory.SelectedItem)?.Id.ToString() ?? string.Empty),
|
||
};
|
||
_mongoLogic.UpdateArticle(updated);
|
||
}
|
||
|
||
LoadData();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Пожалуйста, выберите статью, информацию о которой необходимо обновить");
|
||
}
|
||
}
|
||
|
||
private void ButtonDelete_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count > 0)
|
||
{
|
||
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
|
||
|
||
if (Program.isPostgreSQL)
|
||
{
|
||
int articleId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
|
||
_articleLogic.Delete(articleId);
|
||
}
|
||
else
|
||
{
|
||
string? articleIdMongo = selectedRow.Cells["Id"].Value.ToString();
|
||
_mongoLogic.DeleteArticle(articleIdMongo);
|
||
}
|
||
|
||
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 = "Неизвестная категория" };
|
||
}
|
||
}
|
||
}
|
||
}
|