75 lines
2.3 KiB
C#
Raw Normal View History

using Microsoft.Extensions.Logging;
using LDBproject.Repositories;
namespace LDBproject.Reports;
internal class DocReport
{
private readonly IBookRep _bookRep;
private readonly ILibrarianRep _librarianRep;
private readonly ICustomerCardsRep _customerRep;
private readonly ILogger<DocReport> _logger;
public DocReport(IBookRep bookRep, ILibrarianRep librarianRep,
ICustomerCardsRep customerRep, ILogger<DocReport> logger)
{
_bookRep = bookRep ?? throw new ArgumentNullException(nameof(bookRep));
_librarianRep = librarianRep ?? throw new ArgumentNullException(nameof(librarianRep));
_customerRep = customerRep ?? throw new ArgumentNullException(nameof(customerRep));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public bool CreateDoc(string filePath, bool includeOrders, bool includeLibrarians, bool includeCustomers)
{
try
{
var builder = new WordBuilder(filePath).AddHeader("Reports Document");
if (includeOrders)
{
builder.AddParagraph("Книги").AddTable([2400, 2400, 1200], GetBooks());
}
if (includeLibrarians)
{
builder.AddParagraph("Работники").AddTable([2400, 1200], GetLibrarians());
}
if (includeCustomers)
{
builder.AddParagraph("Читатели").AddTable([2400, 2400], GetCustomers());
}
builder.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "< Error while forming document >");
return false;
}
}
private List<string[]> GetBooks()
{
return [
["Title", "Author", "Year of publishing"],
.. _bookRep.GetBookList().Select(x => new string[]
{ x.Title, x.Author, x.PublishYear.ToString() }),];
}
private List<string[]> GetLibrarians()
{
return [["FIO", "Genres"],
.. _librarianRep.GetCards().Select(x => new string[]
{ x.FIO, x.GenreMask.ToString() ?? "Unknown" }),];
}
private List<string[]> GetCustomers()
{
return [["FIO", "Birthday"],
.. _customerRep.GetCards().Select(x => new string[]
{ x.FIO, x.AgeBirthday.ToString() }),];
}
}