Владимир Данилов 74adae9fc2 Lab8
2024-05-22 01:47:51 +04:00

181 lines
5.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using NewsBlogAbstractions.Models;
using NewsBlogAbstractions.WorkAbstractions;
using NewsBlogMongoDB.Models;
using NewsBlogMongoDB.StoragesContracts;
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;
private readonly StorageModel _mongoLogic;
public FormComment(ICommentWork commentLogic, IArticleWork articleLogic, StorageModel mongoLogic)
{
InitializeComponent();
_commentLogic = commentLogic;
_articleLogic = articleLogic;
_mongoLogic = mongoLogic;
}
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,
};
if (Program.isPostgreSQL)
_commentLogic.Create(newComment);
else
_mongoLogic.AddComment(newComment);
LoadData();
}
private void LoadData()
{
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";
if (Program.isPostgreSQL)
{
var comments = _commentLogic.GetAll();
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);
}
}
else
{
var comments = _mongoLogic.GetComments();
foreach (var comment in comments)
{
dataGridView.Rows.Add(comment.Id, comment.Title, comment.Content, comment.Create_date, comment.Article_id, _mongoLogic.GetArticleById(comment.Article_id)?.Title);
}
}
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
if (Program.isPostgreSQL)
{
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);
}
else
{
string? commentId = selectedRow.Cells["Id"].Value.ToString();
CommentMongo updatedComment = new()
{
Id = commentId,
Title = textBoxTitle.Text,
Content = textBoxContent.Text,
Create_date = DateTime.Now.ToString("d"),
Article_id = _mongoLogic.GetMongoId("Article", ((Article?)comboBoxArticle.SelectedItem)?.Id.ToString() ?? String.Empty),
};
_mongoLogic.UpdateComment(updatedComment);
}
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 commentId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_commentLogic.Delete(commentId);
}
else
{
string? commentId = selectedRow.Cells["Id"].Value.ToString();
_mongoLogic.DeleteComment(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;
}
}
}
}