From e035f95f283d1e58f74781d1bf65463edf9dedc8 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Thu, 18 May 2023 21:57:14 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B5=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ReportLoad.cs | 36 +++++++++ .../BankYouBankruptClientApp/APIClient.cs | 3 +- .../BankYouBankruptClientApp.csproj | 2 + .../Controllers/HomeController.cs | 76 +++++++++++++++++-- .../Views/Home/CreateReport.cshtml | 26 ++++++- .../BankYouBankruptContracts.csproj | 5 ++ .../BusinessLogicsContracts/IReportLoad.cs | 13 ++++ .../ViewModels/PdfLoadViewModel.cs | 17 +++++ .../Controllers/ReportController.cs | 29 ++++++- .../BankYouBankruptRestAPI/Program.cs | 4 + 10 files changed, 199 insertions(+), 12 deletions(-) create mode 100644 BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/ReportLoad.cs create mode 100644 BankYouBankrupt/BankYouBankruptContracts/BusinessLogicsContracts/IReportLoad.cs create mode 100644 BankYouBankrupt/BankYouBankruptContracts/ViewModels/PdfLoadViewModel.cs 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 44a90a5..860db89 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 d94550d..de9a87e 100644 --- a/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs +++ b/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs @@ -1,12 +1,17 @@ - -using BankYouBankruptClientApp.Models; +using BankYouBankruptClientApp.Models; using BankYouBankruptContracts.BindingModels; using BankYouBankruptContracts.ViewModels; 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 { @@ -214,6 +219,7 @@ namespace BankYouBankruptClientApp.Controllers Id = x.Id, FullName = x.ClientSurname + " " + x.Number.ToString() }).ToList(); + return View(); } @@ -253,7 +259,7 @@ namespace BankYouBankruptClientApp.Controllers } [HttpPost] - public void CreateReport(DateTime dateFrom, DateTime dateTo) + public IActionResult CreateReport(DateTime dateFrom, DateTime dateTo) { if (APIClient.Client == null) { @@ -266,12 +272,70 @@ 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; + }*/ - [HttpGet] + #endregion + + [HttpGet] public IActionResult ReportWithCards() { if (APIClient.Client == null) 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();