Component-oriented-programming/LibraryWinFormsApp/FormBooks.cs

327 lines
7.1 KiB
C#

using ComponentLibrary1.pdf_image;
using LibraryContracts.StorageContracts;
using LibraryDataModels.Dtos;
using LibraryDataModels.Views;
using System.Windows.Forms;
using YunusovComponentsLibrary;
using YunusovComponentsLibrary.OfficePackage.HelperModels;
using LibraryUtils.FileChooser;
using static LibraryUtils.FileChooser.FileChooser;
using static LibraryUtils.ImageConverter.ImageConverter;
namespace LibraryWinFormsApp
{
public partial class FormBooks : Form
{
private IBookStorage _bookStorage;
private IAuthorStorage _authorStorage;
private List<BookView> _books = new List<BookView>();
public FormBooks(IBookStorage bookStorage, IAuthorStorage authorStorage)
{
InitializeComponent();
_bookStorage = bookStorage;
_authorStorage = authorStorage;
}
#region event-handler-methods
private void FormBooks_Load(object sender, EventArgs e)
{
try
{
LoadData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void changeAuthorsToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormAuthors));
if (service is FormAuthors form)
{
form.ShowDialog();
}
}
private void createBookToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
CreateBook();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void updateBookToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
UpdateBook();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void deleteBookToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
DeleteBook();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void createPdfToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
CreatePdf();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void createExcellToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
CreateExcell();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void createWordToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
CreateWord();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void booksTable_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (!e.Control) { return; }
switch (e.KeyCode)
{
case Keys.A:
CreateBook();
break;
case Keys.U:
UpdateBook();
break;
case Keys.D:
DeleteBook();
break;
case Keys.S:
CreatePdf();
break;
case Keys.T:
CreateExcell();
break;
case Keys.C:
CreateWord();
break;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
private void LoadData()
{
_books = _bookStorage.GetFullList();
List<ColumnInfo> parameters = new List<ColumnInfo>()
{
new ColumnInfo("", 0, false, "Id"),
new ColumnInfo("Íàçâàíèå", 258, true, "Name"),
new ColumnInfo("", 0, false, "BookCover"),
new ColumnInfo("ÔÈÎ àâòîðà", 258, true, "Author"),
new ColumnInfo("Äàòà èçäàíèÿ", 258, true, "ReleaseDate"),
};
bookTable.ConfigColumn(parameters);
if (_books == null)
{
return;
}
foreach (var book in _books)
{
bookTable.AddItem(book);
}
}
private void CreateBook()
{
var service = Program.ServiceProvider?.GetService(typeof(FormBook));
if (service is FormBook form)
{
form.ShowDialog();
LoadData();
}
}
private void UpdateBook()
{
int selectedRow = bookTable.SelectedRow;
if (selectedRow == -1)
{
throw new Exception("Âûáåðèòå êíèãó äëÿ ðåäàêòèðîâàíèÿ äàííûõ");
}
else
{
int id = Convert.ToInt32(bookTable.Rows[selectedRow].Cells[0].Value);
var service = Program.ServiceProvider?.GetService(typeof(FormBook));
if (service is FormBook form)
{
form._id = id;
form.ShowDialog();
LoadData();
}
}
}
private void DeleteBook()
{
int selectedRow = bookTable.SelectedRow;
if (selectedRow == -1)
{
throw new Exception("Âûáåðèòå êíèãó äëÿ óäàëåíèÿ");
}
if (MessageBox.Show("Óäàëèòü çàïèñü?", "Âîïðîñ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
int id = Convert.ToInt32(bookTable.Rows[selectedRow].Cells[0].Value);
_bookStorage.Delete(new BookDto()
{
Id = id
});
LoadData();
}
}
private void CreatePdf()
{
string fileName = GetFileFullName(DocType.Pdf);
if (string.IsNullOrWhiteSpace(fileName))
{
return;
}
_books = _bookStorage.GetFullList();
List<byte[]> selectedImages = new List<byte[]>();
foreach (var book in _books)
{
selectedImages.Add(StringToByteArray(book.BookCover));
}
var info = new PdfImageInfo
{
FileName = fileName,
Title = "Îáëîæêè",
Images = selectedImages,
};
pdfImage.CreatePdf(info);
MessageBox.Show("PDF óñïåøíî ñîçäàí!", "Óñïåõ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void CreateExcell()
{
string fileName = GetFileFullName(DocType.Excel);
if (string.IsNullOrWhiteSpace(fileName))
{
return;
}
List<TreeNode> headers = new List<TreeNode>
{
new TreeNode
{
Text = "Èäåíòèôèêàòîð",
Tag = "Id",
Name = "40",
},
new TreeNode("Êíèãà", new TreeNode[]
{
new TreeNode
{
Text = "Íàçâàíèå",
Tag = "Name",
Name = "40",
},
new TreeNode
{
Text = "Àâòîð",
Tag = "Author",
Name = "40",
},
}),
new TreeNode
{
Text = "Äàòà",
Tag = "ReleaseDate",
Name = "40",
},
};
_books = _bookStorage.GetFullList();
configurableTable.CreateTable(
fileName,
"Êíèãè",
headers,
_books);
MessageBox.Show("Excell óñïåøíî ñîçäàí!", "Óñïåõ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void CreateWord()
{
string fileName = GetFileFullName(DocType.Word);
if (string.IsNullOrWhiteSpace(fileName))
{
return;
}
_books = _bookStorage.GetFullList();
var authors = _authorStorage.GetFullList();
WordDiagramSeries series = new WordDiagramSeries();
series.SeriesName = "Àâòîðû";
foreach (var author in authors)
{
double sum = 0.0;
foreach (var book in _books)
{
if (string.Equals(book.Author, author.Name))
{
sum++;
}
}
series.Data.Add(author.Name, sum);
}
wordDiagram.CreateDiagram(new WordDiagramInfo()
{
FileName = fileName,
Title = "Àâòîðû",
ChartTitle = "Àâòîðû",
Series = series,
});
MessageBox.Show("Word óñïåøíî ñîçäàí!", "Óñïåõ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}