From 8ef56e41f008300331a24337d23c719e78c443da Mon Sep 17 00:00:00 2001 From: Yuee Shiness Date: Sat, 20 May 2023 02:22:02 +0400 Subject: [PATCH] Views for excel/word. --- ComputerStoreEmployeeApp/APIClient.cs | 30 +++++++++-- .../Controllers/HomeController.cs | 6 ++- .../Controllers/ReportController.cs | 53 +++++++++++++------ .../Views/Home/PCCheck.cshtml | 6 ++- .../Views/Home/PCDelete.cshtml | 4 +- .../Views/Report/ConsignmentsReport.cshtml | 49 +++++++---------- 6 files changed, 94 insertions(+), 54 deletions(-) diff --git a/ComputerStoreEmployeeApp/APIClient.cs b/ComputerStoreEmployeeApp/APIClient.cs index b9e1f2a..3b4d8d8 100644 --- a/ComputerStoreEmployeeApp/APIClient.cs +++ b/ComputerStoreEmployeeApp/APIClient.cs @@ -1,7 +1,9 @@ using ComputerStoreContracts.BindingModels; using ComputerStoreContracts.ViewModels; using ComputerStoreDataModels.Models; +using Microsoft.VisualBasic; using Newtonsoft.Json; +using System.Net; using System.Net.Http.Headers; using System.Text; @@ -27,10 +29,7 @@ namespace ComputerStoreEmployeeApp var result = response.Content.ReadAsStringAsync().Result; if (response.IsSuccessStatusCode) { - return JsonConvert.DeserializeObject(result, new JsonSerializerSettings() - { - ReferenceLoopHandling = ReferenceLoopHandling.Ignore - }); + return JsonConvert.DeserializeObject(result); } else { @@ -38,6 +37,24 @@ namespace ComputerStoreEmployeeApp } } + public static Stream GetRequestFile(string requestUrl, ReportComponentsBindingModel model) + { + var json = JsonConvert.SerializeObject(model); + var data = new StringContent(json, Encoding.UTF8, "application/json"); + var response = _client.PostAsync(requestUrl, data).Result; + + if (response.StatusCode == HttpStatusCode.OK) + { + response.Content.Headers.TryGetValues("content-type", out var type); + + return response.Content.ReadAsStreamAsync().Result; + } + else + { + throw new Exception("Something went wrong!"); + } + } + public static async Task PostRequest(string requestUrl, T model) { var json = JsonConvert.SerializeObject(model); @@ -52,7 +69,10 @@ namespace ComputerStoreEmployeeApp throw new Exception(result); } return true; - } + } + + + public static async Task PatchRequest(string requestUrl, T model) { var json = JsonConvert.SerializeObject(model); diff --git a/ComputerStoreEmployeeApp/Controllers/HomeController.cs b/ComputerStoreEmployeeApp/Controllers/HomeController.cs index 54818da..11a57e7 100644 --- a/ComputerStoreEmployeeApp/Controllers/HomeController.cs +++ b/ComputerStoreEmployeeApp/Controllers/HomeController.cs @@ -319,7 +319,11 @@ namespace ComputerStoreEmployeeApp.Controllers public IActionResult PCAdd(int? request) { ViewBag.Components = APIClient.GetRequest>("api/main/getcomponentslist").Result; - ViewBag.Requests = APIClient.GetRequest>($"api/main/getrequestlist?id={null}").Result; + if(!APIClient.GetRequest>($"api/main/getrequestcomponentlist?id={null}").Result.Any()) + { + return Redirect("PCMenu"); + } + ViewBag.Requests = APIClient.GetRequest>($"api/main/getrequestcomponentlist?id={null}").Result.Select(x => new RequestViewModel { ID = x.RequestID}).ToList(); ViewBag.PCComponents = APIClient.pcComponents; if(request == null) { diff --git a/ComputerStoreEmployeeApp/Controllers/ReportController.cs b/ComputerStoreEmployeeApp/Controllers/ReportController.cs index 4af86a4..b47ea39 100644 --- a/ComputerStoreEmployeeApp/Controllers/ReportController.cs +++ b/ComputerStoreEmployeeApp/Controllers/ReportController.cs @@ -1,7 +1,10 @@ using ComputerStoreContracts.BindingModels; using ComputerStoreContracts.SearchModels; using ComputerStoreContracts.ViewModels; +using ComputerStoreDataModels.Models; using Microsoft.AspNetCore.Mvc; +using System.Linq; +using System.Text; namespace ComputerStoreEmployeeApp.Controllers { @@ -30,8 +33,15 @@ namespace ComputerStoreEmployeeApp.Controllers [HttpPost] public IActionResult SaveExcel() { - var model = new ReportComponentsBindingModel { FileName = "ReportConsignments", Components = APIClient.productComponents.Select(x => new ComponentSearchModel { ID = x.Key, Name = x.Value.Component.Name }).ToList() }; - return File(APIClient.GetRequest($"api/main/savereportconsignmentstoexcel?model={model}").Result, + + var model = new ReportComponentsBindingModel + { + FileName = "ReportConsignments.xlsx" , + Components = APIClient.productComponents.Select(x => new ComponentSearchModel { ID = x.Key, Name =x.Value.Component.Name}).ToList() + }; + APIClient.productComponents.Clear(); + var mem = APIClient.GetRequestFile($"api/main/savereportconsignmentstoexcel",model); + return File(mem, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "ReportConsignments.xlsx"); @@ -40,31 +50,29 @@ namespace ComputerStoreEmployeeApp.Controllers [HttpPost] public IActionResult SaveWord() { - var model = new ReportComponentsBindingModel { FileName = "ReportConsignments", Components = APIClient.productComponents.Select(x => new ComponentSearchModel { ID = x.Key, Name = x.Value.Component.Name }).ToList() }; - return File(APIClient.GetRequest($"api/main/savereportconsignmentstoword?model={model}").Result, + var model = new ReportComponentsBindingModel + { + FileName = "ReportConsignments.xlsx", + Components = APIClient.productComponents.Select(x => new ComponentSearchModel { ID = x.Key, Name = x.Value.Component.Name }).ToList() + }; + APIClient.productComponents.Clear(); + var mem = APIClient.GetRequestFile($"api/main/savereportconsignmentstoword", model); + return File(mem, "application/vnd.openxmlformats-officedocument.spreadsheetml.document", "ReportConsignments.doc"); } [HttpPost] - public void ConsignmentComponents(int id, int componentquantity) + public void ConsignmentComponents(int id) { var component = APIClient.GetRequest($"api/main/getcomponent?id={id}").Result; if (APIClient.productComponents.ContainsKey(component.ID)) { - if(componentquantity == 0) - { - APIClient.productComponents.Remove(component.ID); - } - else - { - APIClient.productComponents[component.ID] = (component, componentquantity); - } - + APIClient.productComponents[component.ID] = (component, 1); } else { - APIClient.productComponents.Add(component.ID, (component, componentquantity)); + APIClient.productComponents.Add(component.ID, (component, 1)); } } @@ -74,7 +82,9 @@ namespace ComputerStoreEmployeeApp.Controllers { if(APIClient.productComponents.Any()) { - ViewBag.Consignments = APIClient.GetRequest>($"api/main/getreportconsignmentslist?model={APIClient.productComponents.Select(x => new ComponentSearchModel { ID = x.Key, Name = x.Value.Component.Name })}").Result; + + var str = ModelToStr(); + ViewBag.Consignments = APIClient.GetRequest>($"api/main/GetReportConsignments?str={str}").Result; return View("ConsignmentsReport", APIClient.productComponents); } @@ -82,6 +92,17 @@ namespace ComputerStoreEmployeeApp.Controllers ViewBag.Components = APIClient.GetRequest>("api/main/getcomponentslist").Result; return View("ConsignmentsReport", APIClient.productComponents); } + + public string ModelToStr() + { + StringBuilder stringBuilder = new StringBuilder(); + foreach (var component in APIClient.productComponents) + { + stringBuilder.Append($"{component.Key}|{component.Value.Component.Name};"); + } + var str = stringBuilder.ToString(); + return str; + } } } diff --git a/ComputerStoreEmployeeApp/Views/Home/PCCheck.cshtml b/ComputerStoreEmployeeApp/Views/Home/PCCheck.cshtml index 787686f..c7b878f 100644 --- a/ComputerStoreEmployeeApp/Views/Home/PCCheck.cshtml +++ b/ComputerStoreEmployeeApp/Views/Home/PCCheck.cshtml @@ -43,7 +43,11 @@ @foreach (var component in item.PCComponents) { - + + + + + }
@component.Value.Component.Name
@component.Value.Component.Name@component.Value.Quantity
diff --git a/ComputerStoreEmployeeApp/Views/Home/PCDelete.cshtml b/ComputerStoreEmployeeApp/Views/Home/PCDelete.cshtml index 17a26a8..41d42a1 100644 --- a/ComputerStoreEmployeeApp/Views/Home/PCDelete.cshtml +++ b/ComputerStoreEmployeeApp/Views/Home/PCDelete.cshtml @@ -7,8 +7,8 @@
- - + +
@if (!string.IsNullOrEmpty(ViewBag.Message)) diff --git a/ComputerStoreEmployeeApp/Views/Report/ConsignmentsReport.cshtml b/ComputerStoreEmployeeApp/Views/Report/ConsignmentsReport.cshtml index e881891..4332704 100644 --- a/ComputerStoreEmployeeApp/Views/Report/ConsignmentsReport.cshtml +++ b/ComputerStoreEmployeeApp/Views/Report/ConsignmentsReport.cshtml @@ -7,15 +7,15 @@

Enter components

- +
+ + @if(ViewBag.Components != null) {

Component:

- -
Quantity:
- +
@@ -25,10 +25,7 @@ Name - - - Count - + @@ -39,9 +36,6 @@ @Html.DisplayFor(modelItem => item.Value.Item1.Name) - - @Html.DisplayFor(modelItem => item.Value.Item2) - } @@ -80,8 +74,7 @@ @foreach (var product in item.Products) { - @product.product - @product.count + @product.product } @@ -91,27 +84,26 @@ } -
- - - -
- -
-
+
+
+ +
+
+
+ +
} @if (!string.IsNullOrEmpty(ViewBag.Message)) { } - +
\ No newline at end of file