PIbd-21_Danilov_V.V._SUBD/NewsBlog/NewsBlogView/FormAuthor.cs
Владимир Данилов cb6251ab2b Лаба4
2024-05-19 21:59:55 +04:00

122 lines
3.2 KiB
C#
Raw 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 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 FormAuthor : Form
{
private readonly IAuthorWork _authorLogic;
public FormAuthor(IAuthorWork authorLogic)
{
InitializeComponent();
_authorLogic = authorLogic;
}
private void FormAuthor_Load(object sender, EventArgs e)
{
LoadData();
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Author newAuthor = new()
{
Name = textBoxName.Text,
Description = textBoxDescription.Text,
Phone = textBoxPhone.Text,
};
_authorLogic.Create(newAuthor);
LoadData();
}
private void LoadData()
{
var authors = _authorLogic.GetAll();
dataGridView.Rows.Clear();
if (dataGridView.ColumnCount == 0)
{
dataGridView.Columns.Add("Id", "ID");
dataGridView.Columns.Add("Name", "Имя");
dataGridView.Columns.Add("Description", "Описание");
dataGridView.Columns.Add("Phone", "Телефон");
}
dataGridView.Columns["Id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Description"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Phone"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
foreach (var author in authors)
{
dataGridView.Rows.Add(author.Id, author.Name, author.Description, author.Phone);
}
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int authorId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
Author updatedAuthor = new()
{
Id = authorId,
Name = textBoxName.Text,
Description = textBoxDescription.Text,
Phone = textBoxPhone.Text,
};
_authorLogic.Update(updatedAuthor);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите автора, информацию о котором необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int authorId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_authorLogic.Delete(authorId);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите автора, информацию о котором необходимо удалить");
}
}
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
textBoxName.Text = row.Cells["Name"].Value.ToString();
textBoxDescription.Text = row.Cells["Description"].Value.ToString();
textBoxPhone.Text = row.Cells["Phone"].Value.ToString();
}
}
}
}