diff --git a/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/ReportLoad.cs b/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/ReportLoad.cs new file mode 100644 index 0000000..f359d10 --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/ReportLoad.cs @@ -0,0 +1,36 @@ +using BankYouBankruptContracts.BusinessLogicsContracts; +using DocumentFormat.OpenXml.ExtendedProperties; +using Org.BouncyCastle.Asn1.Ocsp; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; + +namespace BankYouBankruptBusinessLogic.BusinessLogics +{ + public class ReportLoad : IReportLoad + { + public byte[] LoadFile(bool whoRequested) + { + string pdfFilePath; + + //если пришло true, то загружаем отчёт клиента + if (whoRequested) + { + pdfFilePath = "C:\\Users\\Programmist73\\Desktop\\Практика\\2-й курс\\4-й семестр\\CourseWork_BankYouBankrupt\\BankYouBankrupt\\BankYouBankruptRestAPI\\Отчёт_по_картам.pdf"; + } + else + { + pdfFilePath = "C:\\Users\\Programmist73\\Desktop\\Практика\\2-й курс\\4-й семестр\\CourseWork_BankYouBankrupt\\BankYouBankrupt\\BankYouBankruptRestAPI\\Отчёт_по_счетам.pdf"; + } + + byte[] bytes = System.IO.File.ReadAllBytes(pdfFilePath); + + System.IO.File.WriteAllBytes(pdfFilePath, bytes); + + return bytes; + } + } +} diff --git a/BankYouBankrupt/BankYouBankruptClientApp/APIClient.cs b/BankYouBankrupt/BankYouBankruptClientApp/APIClient.cs index 797b01a..bf69f31 100644 --- a/BankYouBankrupt/BankYouBankruptClientApp/APIClient.cs +++ b/BankYouBankrupt/BankYouBankruptClientApp/APIClient.cs @@ -22,7 +22,8 @@ namespace BankYouBankruptСlientApp public static T? GetRequest(string requestUrl) { var response = _client.GetAsync(requestUrl); - var result = response.Result.Content.ReadAsStringAsync().Result; + + var result = response.Result.Content.ReadAsStringAsync().Result; if (response.Result.IsSuccessStatusCode) { diff --git a/BankYouBankrupt/BankYouBankruptClientApp/BankYouBankruptClientApp.csproj b/BankYouBankrupt/BankYouBankruptClientApp/BankYouBankruptClientApp.csproj index 5bf8ee5..ba4cf68 100644 --- a/BankYouBankrupt/BankYouBankruptClientApp/BankYouBankruptClientApp.csproj +++ b/BankYouBankrupt/BankYouBankruptClientApp/BankYouBankruptClientApp.csproj @@ -8,6 +8,8 @@ + + diff --git a/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs b/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs index 37b0d90..504fba4 100644 --- a/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs +++ b/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs @@ -7,8 +7,14 @@ using BankYouBankruptContracts.ViewModels.Client.Diagram; using BankYouBankruptDataModels.Enums; using BankYouBankruptСlientApp; using Microsoft.AspNetCore.Mvc; +using PdfSharp.Drawing; +using PdfSharp.Pdf; +using PdfSharp.Pdf.IO; +using System.Collections; using System.Diagnostics; +using System.Reflection; using System.Xml.Linq; +using static System.Net.Mime.MediaTypeNames; namespace BankYouBankruptClientApp.Controllers { @@ -217,6 +223,7 @@ namespace BankYouBankruptClientApp.Controllers Id = x.Id, FullName = x.ClientSurname + " " + x.Number.ToString() }).ToList(); + return View(); } @@ -255,7 +262,7 @@ namespace BankYouBankruptClientApp.Controllers } [HttpPost] - public void CreateReport(DateTime dateFrom, DateTime dateTo) + public IActionResult CreateReport(DateTime dateFrom, DateTime dateTo) { if (APIClient.Client == null) { @@ -268,10 +275,68 @@ namespace BankYouBankruptClientApp.Controllers DateTo = dateTo }); - Response.Redirect("CreditingList"); + PdfLoadViewModel support = APIClient.GetRequest($"api/Report/LoadReport?whoRequested={bool.TrueString}"); + + + + /*PdfDocument doc = new PdfDocument(); + + doc.LoadFromBytes(support.bytes); + + support.document = doc;*/ + + + + //return File(support.bytes, "application/pdf", "support.pdf") + + /*/Create PDF Document + PdfDocument document = new PdfDocument(); + + //You will have to add Page in PDF Document + PdfPage page = document.AddPage(); + + //For drawing in PDF Page you will nedd XGraphics Object + XGraphics gfx = XGraphics.FromPdfPage(page); + + //For Test you will have to define font to be used + XFont font = new XFont("Verdana", 20, XFontStyle.Bold); + + //Finally use XGraphics & font object to draw text in PDF Page + gfx.DrawString("My First PDF Document", font, XBrushes.Black, + + new XRect(0, 0, page.Width, page.Height), XStringFormats.Center); + + //Specify file name of the PDF file + string filename = "FirstPDFDocument.pdf"; + + //Save PDF File + document.Save(filename); + + //Load PDF File for viewing + Process.Start(filename);*/ + + + + /*MemoryStream stream = new MemoryStream(support.bytes); + PdfDocument document = PdfReader.Open(stream, PdfDocumentOpenMode.Import); + support.document = document.Save(stream, false);*/ + + //ViewBag.ReportFile = File(support.bytes, "application/pdf"); + + ViewBag.ReportFile = new FileContentResult(support.bytes, "application/pdf"); + + //ViewBag.ReportFile = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location).ToString(); + + return View(); } - #endregion + /*[HttpPost] + public object LoadReport() + { + return null; + }*/ + + #endregion #region Получение отчета по картам [HttpGet] diff --git a/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/CreateReport.cshtml b/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/CreateReport.cshtml index 27f97d1..138a83b 100644 --- a/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/CreateReport.cshtml +++ b/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/CreateReport.cshtml @@ -21,7 +21,29 @@
- +
- \ No newline at end of file +
+

Просмотр отчёта в формате pdf

+ +

Unable to display PDF file. Download instead.

+
+
+ + + + \ No newline at end of file diff --git a/BankYouBankrupt/BankYouBankruptContracts/BankYouBankruptContracts.csproj b/BankYouBankrupt/BankYouBankruptContracts/BankYouBankruptContracts.csproj index e2e4bae..a3afc7b 100644 --- a/BankYouBankrupt/BankYouBankruptContracts/BankYouBankruptContracts.csproj +++ b/BankYouBankrupt/BankYouBankruptContracts/BankYouBankruptContracts.csproj @@ -6,6 +6,11 @@ enable + + + + + diff --git a/BankYouBankrupt/BankYouBankruptContracts/BusinessLogicsContracts/IReportLoad.cs b/BankYouBankrupt/BankYouBankruptContracts/BusinessLogicsContracts/IReportLoad.cs new file mode 100644 index 0000000..4c25831 --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptContracts/BusinessLogicsContracts/IReportLoad.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankYouBankruptContracts.BusinessLogicsContracts +{ + public interface IReportLoad + { + byte[] LoadFile(bool whoRequested); + } +} diff --git a/BankYouBankrupt/BankYouBankruptContracts/ViewModels/PdfLoadViewModel.cs b/BankYouBankrupt/BankYouBankruptContracts/ViewModels/PdfLoadViewModel.cs new file mode 100644 index 0000000..2f8ca6b --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptContracts/ViewModels/PdfLoadViewModel.cs @@ -0,0 +1,17 @@ +using PdfSharp.Pdf; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankYouBankruptContracts.ViewModels +{ + //для возврата pdf на страницу + public class PdfLoadViewModel + { + public byte[] bytes { get; set; } + + public PdfDocument document { get; set; } + } +} diff --git a/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/ReportController.cs b/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/ReportController.cs index bc04c92..51d4361 100644 --- a/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/ReportController.cs +++ b/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/ReportController.cs @@ -4,7 +4,10 @@ using BankYouBankruptContracts.BusinessLogicsContracts; using BankYouBankruptContracts.SearchModels; using BankYouBankruptContracts.ViewModels; using BankYouBankruptRestApi.Controllers; +using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing; using Microsoft.AspNetCore.Mvc; +using Org.BouncyCastle.Utilities; +using System.Net; namespace BankYouBankruptRestAPI.Controllers { @@ -21,12 +24,14 @@ namespace BankYouBankruptRestAPI.Controllers private readonly IReportCashierLogic _reportCashierLogic; + private readonly IReportLoad _reportLoad; - public ReportController(ILogger logger, IReportClientLogic reportClientLogic, IReportCashierLogic reportCashierLogic) + public ReportController(ILogger logger, IReportClientLogic reportClientLogic, IReportCashierLogic reportCashierLogic, IReportLoad reportLoad) { _logger = logger; _reportClientLogic = reportClientLogic; _reportCashierLogic = reportCashierLogic; + _reportLoad = reportLoad; } //метод генерации отчёта за период по картам клиента @@ -37,7 +42,7 @@ namespace BankYouBankruptRestAPI.Controllers { _reportClientLogic.SaveClientReportToPdfFile(new ReportBindingModel { - FileName = "Отчёт по картам за " + DateTime.Now.ToShortDateString() + ".pdf", + FileName = "Отчёт_по_картам.pdf", DateFrom = model.DateFrom, DateTo = model.DateTo }); @@ -57,7 +62,7 @@ namespace BankYouBankruptRestAPI.Controllers { _reportCashierLogic.SaveAccountsToPdfFile(new ReportBindingModel { - FileName = "Отчёт по счетам за " + DateTime.Now.ToShortDateString() + ".pdf", + FileName = "Отчёт_по_счетам.pdf", ClientId = model.ClientId, DateFrom = model.DateFrom, DateTo = model.DateTo @@ -69,5 +74,23 @@ namespace BankYouBankruptRestAPI.Controllers throw; } } + + [HttpGet] + public PdfLoadViewModel LoadReport(bool whoRequested) + { + try + { + PdfLoadViewModel model = new PdfLoadViewModel(); + + model.bytes = _reportLoad.LoadFile(whoRequested); + + return model; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка входа в систему"); + throw; + } + } } } diff --git a/BankYouBankrupt/BankYouBankruptRestAPI/Program.cs b/BankYouBankrupt/BankYouBankruptRestAPI/Program.cs index 415ce0f..ff42f0f 100644 --- a/BankYouBankrupt/BankYouBankruptRestAPI/Program.cs +++ b/BankYouBankrupt/BankYouBankruptRestAPI/Program.cs @@ -35,6 +35,10 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); + +//для отправки файла в приложение +builder.Services.AddTransient(); + builder.Services.AddTransient(); builder.Services.AddSingleton();