From e376e9d8f2d773076d2ba2a01446a020d83b9206 Mon Sep 17 00:00:00 2001 From: dasha Date: Wed, 17 May 2023 22:40:46 +0400 Subject: [PATCH 1/2] files download --- .../Storekeeper/ReportStorekeeperLogic.cs | 31 +++++++------ .../OfficePackage/AbstractSaveToExcel.cs | 6 +-- .../HardwareShopClientApp/APIClient.cs | 18 ++++++++ .../Controllers/StorekeeperController.cs | 11 ++--- .../Views/Storekeeper/ListBuilds.cshtml | 41 +++++++++++++----- HardwareShop/HardwareShopRestApi/$q.doc | Bin 1501 -> 0 bytes .../Controllers/ReportController.cs | 12 ++--- HardwareShop/HardwareShopRestApi/qwe.xlsx | Bin 0 -> 2992 bytes 8 files changed, 77 insertions(+), 42 deletions(-) delete mode 100644 HardwareShop/HardwareShopRestApi/$q.doc create mode 100644 HardwareShop/HardwareShopRestApi/qwe.xlsx diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ReportStorekeeperLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ReportStorekeeperLogic.cs index 3d95e07..15942a6 100644 --- a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ReportStorekeeperLogic.cs +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ReportStorekeeperLogic.cs @@ -11,22 +11,26 @@ namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper { private readonly IComponentStorage _componentStorage; + private readonly IGoodStorage _goodStorage; + private readonly AbstractSaveToExcel _saveToExcel; private readonly AbstractSaveToWord _saveToWord; - public ReportStorekeeperLogic(IComponentStorage componentStorage, AbstractSaveToExcel abstractSaveToExcel, AbstractSaveToWord abstractSaveToWord) + public ReportStorekeeperLogic(IComponentStorage componentStorage, AbstractSaveToExcel abstractSaveToExcel, AbstractSaveToWord abstractSaveToWord, IGoodStorage goodStorage) { _componentStorage = componentStorage; _saveToExcel = abstractSaveToExcel; _saveToWord = abstractSaveToWord; + _goodStorage = goodStorage; } public List GetBuildGood(List goods) { var result = new List(); - foreach (var good in goods) + foreach (var g in goods) { + var good = _goodStorage.GetElement(new() { Id = g.Id })!; var builds = good.GoodComponents //получили сборки и количество компонентов .Select(x => _componentStorage.GetComponentBuilds(new() { Id = x.Key }) @@ -78,16 +82,6 @@ namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper return result; } - public void SaveBuildGoodToExcelFile(ReportBindingModel model, List goods) - { - _saveToExcel.CreateBuildGoodReport(new ExcelInfo - { - FileName = model.FileName, - Title = "Cписок сборок по выбранным товарам", - BuildGood = GetBuildGood(goods) - }); - } - public byte[] SaveBuildGoodToWordFile(ReportBindingModel model, List goods) { _saveToWord.CreateBuildGoodReport(new WordInfo @@ -102,9 +96,18 @@ namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper return file; } - byte[] IReportStorekeeperLogic.SaveBuildGoodToExcelFile(ReportBindingModel model, List goods) + public byte[] SaveBuildGoodToExcelFile(ReportBindingModel model, List goods) { - throw new NotImplementedException(); + _saveToExcel.CreateBuildGoodReport(new ExcelInfo + { + FileName = model.FileName, + Title = "Cписок сборок по выбранным товарам", + BuildGood = GetBuildGood(goods) + }); + + byte[] file = File.ReadAllBytes(model.FileName); + File.Delete(model.FileName); + return file; } } } diff --git a/HardwareShop/HardwareShopBusinessLogic/OfficePackage/AbstractSaveToExcel.cs b/HardwareShop/HardwareShopBusinessLogic/OfficePackage/AbstractSaveToExcel.cs index 038a23d..2a675cd 100644 --- a/HardwareShop/HardwareShopBusinessLogic/OfficePackage/AbstractSaveToExcel.cs +++ b/HardwareShop/HardwareShopBusinessLogic/OfficePackage/AbstractSaveToExcel.cs @@ -24,7 +24,7 @@ namespace HardwareShopBusinessLogic.OfficePackage MergeCells(new ExcelMergeParameters { CellFromName = "A1", - CellToName = "C1" + CellToName = "B1" }); uint rowIndex = 2; @@ -35,7 +35,7 @@ namespace HardwareShopBusinessLogic.OfficePackage ColumnName = "A", RowIndex = rowIndex, Text = bg.GoodName, - StyleInfo = ExcelStyleInfoType.Text + StyleInfo = ExcelStyleInfoType.TextWithBroder }); rowIndex++; @@ -51,8 +51,6 @@ namespace HardwareShopBusinessLogic.OfficePackage rowIndex++; } - - rowIndex++; } SaveExcel(info); diff --git a/HardwareShop/HardwareShopClientApp/APIClient.cs b/HardwareShop/HardwareShopClientApp/APIClient.cs index f83e062..810244c 100644 --- a/HardwareShop/HardwareShopClientApp/APIClient.cs +++ b/HardwareShop/HardwareShopClientApp/APIClient.cs @@ -46,5 +46,23 @@ namespace HardwareShopStorekeeperApp 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/HardwareShopClientApp/Controllers/StorekeeperController.cs b/HardwareShop/HardwareShopClientApp/Controllers/StorekeeperController.cs index 39ef02f..c7f00d5 100644 --- a/HardwareShop/HardwareShopClientApp/Controllers/StorekeeperController.cs +++ b/HardwareShop/HardwareShopClientApp/Controllers/StorekeeperController.cs @@ -396,7 +396,7 @@ namespace HardwareShopStorekeeperApp.Controllers } [HttpPost] - public void ListBuilds([FromBody] GoodBindingModel goodModel, [FromQuery] string format, [FromQuery] string filename) + public int[]? ListBuilds([FromBody] GoodBindingModel goodModel, [FromQuery] string format) { if (APIClient.User == null) { @@ -404,13 +404,10 @@ namespace HardwareShopStorekeeperApp.Controllers } if (string.IsNullOrEmpty(format)) { - throw new FormatException($"Неправильный формат файла: {format}"); + throw new FormatException("Неправильный формат файла"); } - if (string.IsNullOrEmpty(filename)) - { - throw new FormatException($"Неправильное название файла: {filename}"); - } - APIClient.PostRequest($"api/report/buildgoodreport?format={format}&filename=${filename}", goodModel); + byte[]? file = APIClient.PostRequestWithResult($"api/report/buildgoodreport?format={format}", goodModel); + return file!.Select(b => (int)b).ToArray(); } public IActionResult Report() diff --git a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/ListBuilds.cshtml b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/ListBuilds.cshtml index c9fac2e..c1882be 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/ListBuilds.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/ListBuilds.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"); submitGoodBtn.addEventListener("click", () => { console.log('try to add good') @@ -88,32 +83,54 @@ }) 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 == '') { - alert('operation failed. goods or filename are empty') + if (list.length == 0) { + alert('operation failed. goods are empty') return } $.ajax({ - url: `/Storekeeper/ListBuilds?format=${format}&filename=${filename.value}`, + url: `/Storekeeper/ListBuilds?format=${format}`, type: 'POST', contentType: 'application/json', data: JSON.stringify({ "Goods" : 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: `listbuilds.${format}`, + types: [{ + description: `${format} file`, + accept: + { + "text/docx": [".docx"], + "text/xlsx": [".xlsx"] + }, + }], + }; + 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; diff --git a/HardwareShop/HardwareShopRestApi/$q.doc b/HardwareShop/HardwareShopRestApi/$q.doc deleted file mode 100644 index d0726ba386d07651986c19072deca4cfa36ae15f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1501 zcmWIWW@Zs#VBp|jm^E`_n1%b7{4CVPnDf%h-$)&lec_n%kxjApc zPG{XQ5cw;*uy*mu=AbhbPQo6$HI^<}8hd)vvBe7BZj+ABTyrb$v*=rKmB`i!K6Q@& z7RUWGG`TWUQ^ae*-5*}x6#<^b6sG}9t*)4+MKFgg_4fXDRxyil9 zUNSszRpiX#rveYA_O6mRKSx{1NoV%QIk$9U3s|?VG_x$-U@EzCizoMD$?amdo=$qx z<96QDul3Nv)J2zFJ6u0kq&rz8uIPFea%={pq`Abz3ENmxFEC{VY1GAd$t6cw-JRO% zb-Oil_l?c_PM?coR*daj({aYi;>@?tE48xL-SuHS()w`PY|G=5o$KGPf308lGGCy@ zd;c1HbLkWM6Zfw=|5r6d@6X)*&0lp_?RWZlD&FMeZHEU@v&y!#X-$lMu|oJ~n_kJw z1h?%P4fR@nmAB4vUS$rDycfUuvTOb11v{QdZ2P8j?C(~U`MbVyC~vyS=CNq*<)2kO z0bxu2e0|?ww~l#Y{JI$@{IylX#1!~ht5aR1rb!-g)@9_cyUY5$^vL1LPY>2)&)%s1 zFrawJ)XfDgQEDzd_8oJgzf22%5%D~P^=z8H6(q{eA6@kJtN8i#PpDU%TUC^UZJf%i_$h1&gKbIqCb>eO>0cnc3B%H-9@U>19Y< zFx%{0z{9FzavZ0>sY{Ffz2A6}KLDCu7#L;))64mul{>W<85lsA8<<|=i&Arn_4PpH z``!C(?13ppp)9qiI5R)bR>@G$KuIArFF8LYGcVm%sk9_b*FwqO&L$`|C$R*mqBtY7 zpjZK@C9l|4DWjyMz)D}gI5{IVH?deRzaTXaD3g|7l$%%ru&}6B9FIB?Wyu8+{ZX+o1;OeQBofC}41curM%4qn-2fN`Sd99^{MS zSWqS;K4`&u$PChCh`qWcAbWv9>XMq4SejF!;93E64kSi_Zc$Qzx(^b_i3J5YnaSX& z&@anNL5nwCNch1E6i}!Y6y+zU78e7PL~f2ATqZX$Gfx|AJTSbl*bGWxq*)D3E}*oD zNGWhD^uZwoj0#335eD4lAuv9W07j%Ev}4a62)zctLKZ2rpld`=1PG1I%vh5|fHx}} PND(s-?gY|&tRNl$lW95O diff --git a/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs b/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs index d00a628..a356090 100644 --- a/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs +++ b/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs @@ -20,21 +20,23 @@ namespace HardwareShopRestApi.Controllers } [HttpPost] - public void BuildGoodReport(GoodBindingModel model, string format, string filename) + public byte[] BuildGoodReport(GoodBindingModel model, string format) { try { + byte[] file; switch (format) { - case "doc": - _reportStorekeeperLogic.SaveBuildGoodToWordFile(new ReportBindingModel { FileName = filename }, model.Goods); + case "docx": + file = _reportStorekeeperLogic.SaveBuildGoodToWordFile(new ReportBindingModel { FileName = "temp.docx" }, model.Goods); break; - case "excel": - _reportStorekeeperLogic.SaveBuildGoodToExcelFile(new ReportBindingModel { FileName = filename }, model.Goods); + case "xlsx": + file = _reportStorekeeperLogic.SaveBuildGoodToExcelFile(new ReportBindingModel { FileName = "temp.xlsx" }, model.Goods); break; default: throw new FormatException("Неправильный формат файла"); } + return file; } catch (Exception ex) { diff --git a/HardwareShop/HardwareShopRestApi/qwe.xlsx b/HardwareShop/HardwareShopRestApi/qwe.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..6340319c1c3d6186052dab9da611a03d7ea21d83 GIT binary patch literal 2992 zcma)83pi7a8y`x;H(_oG&0KQJM!CiGA0_e4t>l)qH8YECY{L-gLVOvLD4)xiT#}Zg zh^bskA>BktB9~kWHBrR>sOS59{QI8gf6jAm&pGe!ocDKr@9%w`9Yog1f*Ktvdg9&m_83ou|&0@=x!q#@i2T37m!ObZ6Ypx*Ey8@%Wp+Mc^*EUM}7)$G|_?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022AM`h!3_1;I5_w(`{yWBcieCA;Pw;CT4IL!pah{B44Y&2>sb@TO!IdW&k1&0L@3~ zr}%zy`p=&GgWt+jv||HOY8UTNTaH;m);2Vz2H$W)APglb@5YQbhvuO+N+dPPA;EcT z%5l#+nm>OiGJR2dyN)uGLH2DQn8#s6JX3|$)hVXu>y9y;!{o-px<%NE{Se-%mld;( zv!YD!XVftEaOUI9p;}NVvFD_ks!uy9ZS#1-FxiWK)Avs1gQU(23pUs7A*rgDEuDPp zH{m`uglMcEkH1p%7<4&52)qGcNx2Tb+j-+ub&`P@4 zoWe|TKzn@vT~L57ZyOqw3H-Z*OTV1qvs9*KshB06Vu1nW^r0esDeFDAv;&323~=UW z?_F9PxR53Y)HjoH$7SY&)e6K*^jdPlCn5=RpTBdlPH*HQoBav?jrRNVV{nH^4L79K zrw7YCZcdM>3CX`!x+*Q=a%-=68wkry6ccv;cAl^GW~j{m)wn?Zd5Bw&HdF-7NljAC zFPq5gxQGo0{KqibRJD@az%BGAw0@!be#2O(kEB_scr$qwM}AImC5uMS>4 zAuUHv1E))!;_rEBb?EB0vK%@GSnjWy%xZ+y7QKuxwdU^$^?wX|8RggJwTz-7y28qq u1Akql%YoE&KVJVYCA%7DwR)FvvcbR9va`cl;LAXub--E=$i7Q#>FU2`o6ukY literal 0 HcmV?d00001 From 4997506a44052cc927a79f1cf29492dc8f63ed0c Mon Sep 17 00:00:00 2001 From: dasha Date: Thu, 18 May 2023 12:58:44 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HardwareShopClientApp/Views/Storekeeper/ListBuilds.cshtml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/ListBuilds.cshtml b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/ListBuilds.cshtml index c1882be..cfc8c7b 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/ListBuilds.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/ListBuilds.cshtml @@ -118,8 +118,7 @@ description: `${format} file`, accept: { - "text/docx": [".docx"], - "text/xlsx": [".xlsx"] + [`text/${format}`]: [`.${format}`] }, }], };