From 5333817ea812505b93179ae6e7b5f863039b4998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Thu, 18 May 2023 14:40:36 +0400 Subject: [PATCH] =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Worker/WorkerReportLogic.cs | 2 +- .../Controllers/ReportController.cs | 12 +++--- .../HardwareShopWorkerApp/APIClient.cs | 20 +++++++++- .../Controllers/HomeController.cs | 12 +++--- .../Views/Home/listComponents.cshtml | 39 +++++++++++++------ 5 files changed, 61 insertions(+), 24 deletions(-) diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs index bf245c2..0cab5f4 100644 --- a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs @@ -115,7 +115,7 @@ namespace HardwareShopContracts.BusinessLogicsContracts }); byte[] file = File.ReadAllBytes(model.FileName); - //File.Delete(model.FileName); + File.Delete(model.FileName); return file; } diff --git a/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs b/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs index 9d95c6d..2f19715 100644 --- a/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs +++ b/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs @@ -47,21 +47,23 @@ namespace HardwareShopRestApi.Controllers [HttpPost] - public void BuildPurchaseReport(PurchaseBindingModel model, string format, string filename) + public byte[] BuildPurchaseReport(PurchaseBindingModel model, string format) { try { + byte[] file; switch (format) { - case "doc": - _reportWorkerLogic.SavePurchasesToWordFile(new ReportBindingModel { FileName = filename }, model.Purchases); + case "docx": + file = _reportWorkerLogic.SavePurchasesToWordFile(new ReportBindingModel { FileName = "temp.docx" }, model.Purchases); break; - case "excel": - _reportWorkerLogic.SavePurchasesToExcelFile(new ReportBindingModel { FileName = filename }, model.Purchases); + case "xlsx": + file = _reportWorkerLogic.SavePurchasesToExcelFile(new ReportBindingModel { FileName = "temp.xlsx" }, model.Purchases); break; default: throw new FormatException("Неправильный формат файла"); } + return file; } catch (Exception ex) { diff --git a/HardwareShop/HardwareShopWorkerApp/APIClient.cs b/HardwareShop/HardwareShopWorkerApp/APIClient.cs index 99eb654..f84e17f 100644 --- a/HardwareShop/HardwareShopWorkerApp/APIClient.cs +++ b/HardwareShop/HardwareShopWorkerApp/APIClient.cs @@ -46,5 +46,23 @@ namespace HardwareShopWorkerApp throw new Exception(result); } } - } + + public static R? PostRequestWithResult(string requestUrl, T model) + { + var json = JsonConvert.SerializeObject(model); + var data = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = _client.PostAsync(requestUrl, data); + + var result = response.Result.Content.ReadAsStringAsync().Result; + if (response.Result.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject(result); + } + else + { + return default; + } + } + } } \ No newline at end of file diff --git a/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs b/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs index db2efef..6a73699 100644 --- a/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs +++ b/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs @@ -332,7 +332,7 @@ namespace HardwareShopWorkerApp.Controllers } [HttpPost] - public void ListComponents([FromBody] PurchaseBindingModel goodModel, [FromQuery] string format, [FromQuery] string filename) + public int[]? ListComponents([FromBody] PurchaseBindingModel purchaseModel, [FromQuery] string format) { if (APIClient.User == null) { @@ -342,11 +342,11 @@ namespace HardwareShopWorkerApp.Controllers { throw new FormatException($"Неправильный формат файла: {format}"); } - if (string.IsNullOrEmpty(filename)) - { - throw new FormatException($"Неправильное название файла: {filename}"); - } - APIClient.PostRequest($"api/report/buildpurchasereport?format={format}&filename=${filename}", goodModel); + byte[]? file = APIClient.PostRequestWithResult($"api/report/buildpurchasereport?format={format}", purchaseModel); + var array = file!.Select(b => (int)b).ToArray(); + return array; + + } [HttpGet] diff --git a/HardwareShop/HardwareShopWorkerApp/Views/Home/listComponents.cshtml b/HardwareShop/HardwareShopWorkerApp/Views/Home/listComponents.cshtml index edad62f..ea4942c 100644 --- a/HardwareShop/HardwareShopWorkerApp/Views/Home/listComponents.cshtml +++ b/HardwareShop/HardwareShopWorkerApp/Views/Home/listComponents.cshtml @@ -23,10 +23,6 @@ -
- - -
@@ -59,7 +55,6 @@ const resultTable = document.getElementById("result"); const saveDocBtn = document.getElementById("savedoc"); const saveExcelBtn = document.getElementById("saveexcel"); - const filename = document.getElementById("filename"); submitPurchaseBtn.addEventListener("click", () => { console.log('try to add purchase') @@ -88,32 +83,53 @@ }) saveDocBtn.addEventListener("click", async () => { - send('doc') + send('docx') }) saveExcelBtn.addEventListener("click", async () => { - send('excel') + send('xlsx') }) function send(format) { console.log(`try to save in ${format} format`) - if (list.length == 0 || !filename.value || filename.value == '') { + if (list.length == 0) { alert('operation failed. purchases or filename are empty') return } $.ajax({ - url: `/Home/ListComponents?format=${format}&filename=${filename.value}`, + url: `/Home/ListComponents?format=${format}`, type: 'POST', contentType: 'application/json', data: JSON.stringify({ "Purchases" : list }) - }).done(() => { - //let byteArray = new Uint8Array(file); + }).done((file) => { + let byteArray = new Uint8Array(file); + saveFile(byteArray, format); }) .fail(function(xhr, textStatus, errorThrown) { alert(xhr.responseText); }) } + async function saveFile(bytes, format) { + if (window.showSaveFilePicker) { + const opts = { + suggestedName: `listcomponents.${format}`, + types: [{ + description: `${format} file`, + accept: + { + [`text/${format}`]: [`.${format}`] + }, + }], + }; + const handle = await showSaveFilePicker(opts); + const writable = await handle.createWritable(); + await writable.write(bytes); + writable.close(); + alert('done') + } + } + function reloadTable() { resultTable.innerHTML = '' let count = 0; @@ -132,5 +148,6 @@ list = list.filter(value => value.id != resultTable.rows[id].cells[0].innerText) reloadTable() } + } \ No newline at end of file