From eeda90cb7878e54e97313fcb4bdb87f50f59eff7 Mon Sep 17 00:00:00 2001 From: Yuee Shiness Date: Sun, 14 May 2023 22:11:06 +0400 Subject: [PATCH] A damn bunch of fixes + a couple of views for product. --- .../APIModels/APIProductModel.cs | 23 ++++++++ .../BindingModels/ProductBindingModel.cs | 15 +++-- .../ComputerStoreContracts.csproj | 4 ++ .../ViewModels/ProductViewModel.cs | 14 ++++- .../Models/IComponentModel.cs | 1 + .../Models/IProductModel.cs | 2 +- .../Implements/PCStorage.cs | 4 +- .../Implements/ProductStorage.cs | 2 +- ComputerStoreDatabaseImplement/Models/PC.cs | 1 + .../Models/Product.cs | 10 ++-- ComputerStoreEmployeeApp/APIClient.cs | 19 ++++--- .../Controllers/HomeController.cs | 36 +++++++----- .../Views/Home/ProductAdd.cshtml | 38 ++++++++----- .../Views/Home/ProductCheck.cshtml | 56 +++++++++++++++++++ .../ComputerStoreRestAPI.csproj | 1 + .../Controllers/MainController.cs | 7 ++- ComputerStoreRestAPI/Program.cs | 3 +- 17 files changed, 181 insertions(+), 55 deletions(-) create mode 100644 ComputerStoreContracts/APIModels/APIProductModel.cs create mode 100644 ComputerStoreEmployeeApp/Views/Home/ProductCheck.cshtml diff --git a/ComputerStoreContracts/APIModels/APIProductModel.cs b/ComputerStoreContracts/APIModels/APIProductModel.cs new file mode 100644 index 0000000..6355c26 --- /dev/null +++ b/ComputerStoreContracts/APIModels/APIProductModel.cs @@ -0,0 +1,23 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.ViewModels; +using ComputerStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreContracts.APIModels +{ + public class APIProductModel + { + public int ID { get; set; } + public string Name { get; set; } = string.Empty; + + public double Price { get; set; } + + public int EmployeeID { get; set; } + + public Dictionary ProductComponents { get; set; } = new(); + } +} diff --git a/ComputerStoreContracts/BindingModels/ProductBindingModel.cs b/ComputerStoreContracts/BindingModels/ProductBindingModel.cs index aab0fd9..e75e820 100644 --- a/ComputerStoreContracts/BindingModels/ProductBindingModel.cs +++ b/ComputerStoreContracts/BindingModels/ProductBindingModel.cs @@ -1,9 +1,5 @@ using ComputerStoreDataModels.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Newtonsoft.Json; namespace ComputerStoreContracts.BindingModels { @@ -15,7 +11,14 @@ namespace ComputerStoreContracts.BindingModels public double Price { get; set; } public int EmployeeID { get; set; } + + public Dictionary ProductComponents { get; set; } = new(); - public Dictionary ProductComponents { get; set; } = new(); + [JsonConstructor] + public ProductBindingModel(Dictionary ProductComponents) + { + this.ProductComponents = ProductComponents.ToDictionary(x => x.Key, x => (x.Value.Component as IComponentModel, x.Value.Quantity)); + } + public ProductBindingModel() { } } } diff --git a/ComputerStoreContracts/ComputerStoreContracts.csproj b/ComputerStoreContracts/ComputerStoreContracts.csproj index 0eeba33..7899907 100644 --- a/ComputerStoreContracts/ComputerStoreContracts.csproj +++ b/ComputerStoreContracts/ComputerStoreContracts.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/ComputerStoreContracts/ViewModels/ProductViewModel.cs b/ComputerStoreContracts/ViewModels/ProductViewModel.cs index 81c6286..2ed056a 100644 --- a/ComputerStoreContracts/ViewModels/ProductViewModel.cs +++ b/ComputerStoreContracts/ViewModels/ProductViewModel.cs @@ -1,8 +1,11 @@ -using ComputerStoreDataModels.Models; +using ComputerStoreContracts.BindingModels; +using ComputerStoreDataModels.Models; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; +using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; @@ -23,8 +26,13 @@ namespace ComputerStoreContracts.ViewModels [DisplayName("Employee's username")] public string EmployeeUsername { get; set; } = string.Empty; - public Dictionary ProductComponents { get; set; } = new(); + public Dictionary ProductComponents { get; set; } = new(); - + [JsonConstructor] + public ProductViewModel(Dictionary ProductComponents) + { + this.ProductComponents = ProductComponents.ToDictionary(x => x.Key, x => (x.Value.Component as IComponentModel, x.Value.Quantity)); + } + public ProductViewModel() { } } } diff --git a/ComputerStoreDataModels/Models/IComponentModel.cs b/ComputerStoreDataModels/Models/IComponentModel.cs index 6a60baf..09f3d89 100644 --- a/ComputerStoreDataModels/Models/IComponentModel.cs +++ b/ComputerStoreDataModels/Models/IComponentModel.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace ComputerStoreDataModels.Models diff --git a/ComputerStoreDataModels/Models/IProductModel.cs b/ComputerStoreDataModels/Models/IProductModel.cs index 5399350..f23b49c 100644 --- a/ComputerStoreDataModels/Models/IProductModel.cs +++ b/ComputerStoreDataModels/Models/IProductModel.cs @@ -11,6 +11,6 @@ namespace ComputerStoreDataModels.Models public string Name { get; } public double Price { get; } public int EmployeeID { get; } - Dictionary ProductComponents { get; } + Dictionary ProductComponents { get; } } } diff --git a/ComputerStoreDatabaseImplement/Implements/PCStorage.cs b/ComputerStoreDatabaseImplement/Implements/PCStorage.cs index 16c1ec9..e8d545c 100644 --- a/ComputerStoreDatabaseImplement/Implements/PCStorage.cs +++ b/ComputerStoreDatabaseImplement/Implements/PCStorage.cs @@ -36,13 +36,13 @@ namespace ComputerStoreDatabaseImplement.Implements { return context.PCs.Include(x => x.Employee).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList(); } - return context.PCs.Include(x => x.Components).ThenInclude(x => x.Component).Where(p => context.Requests.Where(r => context.Orders.Where(o => o.DateCreate >= model.DateFrom && o.DateCreate <= model.DateTo).Select(o => o.ID).Contains(r.OrderID)).Select(r => r.ID).Contains(p.RequestID)).ToList().Select(x => x.GetViewModel).ToList(); + return context.PCs.Include(x => x.Employee).Include(x => x.Components).ThenInclude(x => x.Component).Where(p => context.Requests.Where(r => context.Orders.Where(o => o.DateCreate >= model.DateFrom && o.DateCreate <= model.DateTo).Select(o => o.ID).Contains(r.OrderID)).Select(r => r.ID).Contains(p.RequestID)).ToList().Select(x => x.GetViewModel).ToList(); } public List GetFullList() { using var context = new ComputerStoreDatabase(); - return context.PCs.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); + return context.PCs.Include(x => x.Employee).Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); } public PCViewModel? Insert(PCBindingModel model) diff --git a/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs index 55ec65d..a58ea31 100644 --- a/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs @@ -32,7 +32,7 @@ namespace ComputerStoreDatabaseImplement.Implements using var context = new ComputerStoreDatabase(); if(model.EmployeeID.HasValue) { - return context.Products.Include(x => x.Employee).Include(x => x.Employee).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList(); + return context.Products.Include(x => x.Employee).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList(); } if(model.ComponentID.HasValue) { diff --git a/ComputerStoreDatabaseImplement/Models/PC.cs b/ComputerStoreDatabaseImplement/Models/PC.cs index 56d6eea..550bdaf 100644 --- a/ComputerStoreDatabaseImplement/Models/PC.cs +++ b/ComputerStoreDatabaseImplement/Models/PC.cs @@ -56,6 +56,7 @@ namespace ComputerStoreDatabaseImplement.Models Name = model.Name, Price = model.Price, EmployeeID = model.EmployeeID, + Employee = context.Employees.First(x => x.ID == model.EmployeeID), RequestID = model.RequestID, Components = model.PCComponents.Select(x => new RequestComponent { diff --git a/ComputerStoreDatabaseImplement/Models/Product.cs b/ComputerStoreDatabaseImplement/Models/Product.cs index a59a758..0ed36f8 100644 --- a/ComputerStoreDatabaseImplement/Models/Product.cs +++ b/ComputerStoreDatabaseImplement/Models/Product.cs @@ -1,6 +1,7 @@ using ComputerStoreContracts.BindingModels; using ComputerStoreContracts.ViewModels; using ComputerStoreDataModels.Models; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -26,7 +27,7 @@ namespace ComputerStoreDatabaseImplement.Models public int EmployeeID { get; private set; } - private Dictionary? _productComponents = null; + private Dictionary? _productComponents = null; [NotMapped] public Dictionary ProductComponents @@ -40,13 +41,13 @@ namespace ComputerStoreDatabaseImplement.Models return _productComponents; } } - + [ForeignKey("ProductID")] public virtual List Components { get; set; } = new(); - + [ForeignKey("ProductID")] public virtual List Consignments { get; set; } = new(); - + public virtual Employee Employee { get; set; } public static Product Create(ComputerStoreDatabase context, ProductBindingModel model) { @@ -56,6 +57,7 @@ namespace ComputerStoreDatabaseImplement.Models Name = model.Name, Price = model.Price, EmployeeID = model.EmployeeID, + Employee = context.Employees.First(x => x.ID == model.EmployeeID), Components = model.ProductComponents.Select(x => new ProductComponent { Component = context.Components.First(y => y.ID == x.Key), diff --git a/ComputerStoreEmployeeApp/APIClient.cs b/ComputerStoreEmployeeApp/APIClient.cs index c8bb38e..b9e1f2a 100644 --- a/ComputerStoreEmployeeApp/APIClient.cs +++ b/ComputerStoreEmployeeApp/APIClient.cs @@ -1,4 +1,5 @@ -using ComputerStoreContracts.ViewModels; +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.ViewModels; using ComputerStoreDataModels.Models; using Newtonsoft.Json; using System.Net.Http.Headers; @@ -11,9 +12,8 @@ namespace ComputerStoreEmployeeApp private static readonly HttpClient _client = new(); public static EmployeeViewModel? Employee { get; set; } = null; - public static Dictionary? productComponents; - public static Dictionary? pcComponents; - + public static Dictionary? productComponents; + public static Dictionary? pcComponents; public static void Connect(IConfiguration configuration) { _client.BaseAddress = new Uri(configuration["IPAddress"]); @@ -27,7 +27,10 @@ namespace ComputerStoreEmployeeApp var result = response.Content.ReadAsStringAsync().Result; if (response.IsSuccessStatusCode) { - return JsonConvert.DeserializeObject(result); + return JsonConvert.DeserializeObject(result, new JsonSerializerSettings() + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore + }); } else { @@ -38,6 +41,7 @@ namespace ComputerStoreEmployeeApp public static async Task PostRequest(string requestUrl, T model) { var json = JsonConvert.SerializeObject(model); + var data = new StringContent(json, Encoding.UTF8, "application/json"); var response = await _client.PostAsync(requestUrl, data); @@ -47,9 +51,8 @@ namespace ComputerStoreEmployeeApp { throw new Exception(result); } - return Convert.ToBoolean(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 269fa58..f07eda1 100644 --- a/ComputerStoreEmployeeApp/Controllers/HomeController.cs +++ b/ComputerStoreEmployeeApp/Controllers/HomeController.cs @@ -1,4 +1,5 @@ -using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.APIModels; +using ComputerStoreContracts.BindingModels; using ComputerStoreContracts.ViewModels; using ComputerStoreDataModels.Models; using ComputerStoreEmployeeApp.Models; @@ -66,7 +67,7 @@ namespace ComputerStoreEmployeeApp.Controllers [HttpGet] public IActionResult ComponentUpdate() { - ViewBag.Components = Task.Run(() => APIClient.GetRequest>("api/main/getcomponentslist")).Result; + ViewBag.Components = APIClient.GetRequest>("api/main/getcomponentslist").Result; return View(); } @@ -121,7 +122,7 @@ namespace ComputerStoreEmployeeApp.Controllers [HttpGet] public IActionResult ComponentDelete() { - ViewBag.Components = Task.Run(() => APIClient.GetRequest>("api/main/getcomponentslist")).Result; + ViewBag.Components = APIClient.GetRequest>("api/main/getcomponentslist").Result; return View(); } @@ -144,20 +145,20 @@ namespace ComputerStoreEmployeeApp.Controllers [HttpGet] public IActionResult ComponentCheck() { - return View(Task.Run(() => APIClient.GetRequest>("api/main/getcomponentslist")).Result); + return View( APIClient.GetRequest>("api/main/getcomponentslist").Result); } [HttpGet] public IActionResult ProductMenu() { - APIClient.productComponents = new Dictionary(); + APIClient.productComponents = new Dictionary(); return View(); } [HttpGet] public IActionResult ProductAdd() { - ViewBag.Components = Task.Run(() => APIClient.GetRequest>("api/main/getcomponentslist")).Result; + ViewBag.Components = APIClient.GetRequest>("api/main/getcomponentslist").Result; return View(APIClient.productComponents); } @@ -171,7 +172,7 @@ namespace ComputerStoreEmployeeApp.Controllers throw new Exception("Enter product's name or product doesn't have any components."); } - if (!Task.Run(() => APIClient.PostRequest("api/main/insertproduct", new ProductBindingModel + if (!Task.Run(() => APIClient.PostRequest("api/main/insertproduct", new ProductBindingModel { Name = productname, Price = productprice, @@ -186,7 +187,7 @@ namespace ComputerStoreEmployeeApp.Controllers { APIClient.productComponents.Clear(); ViewBag.Message = new string(ex.Message.ToString()); - ViewBag.Components = Task.Run(() => APIClient.GetRequest>("api/main/getcomponentslist")).Result; + ViewBag.Components = APIClient.GetRequest>("api/main/getcomponentslist").Result; return View(APIClient.productComponents); } @@ -194,11 +195,12 @@ namespace ComputerStoreEmployeeApp.Controllers return Redirect("ProductMenu"); } - + + [HttpPost] public double ProductComponents(int id, int componentquantity) { - var component = Task.Run(() => APIClient.GetRequest($"api/main/getcomponent?id={id}")).Result; + var component = APIClient.GetRequest($"api/main/getcomponent?id={id}").Result; if(APIClient.productComponents.ContainsKey(component.ID)) { APIClient.productComponents[component.ID] = (component, componentquantity); @@ -206,10 +208,12 @@ namespace ComputerStoreEmployeeApp.Controllers else { APIClient.productComponents.Add(component.ID, (component, componentquantity)); - } - - return APIClient.productComponents.Sum(x => x.Value.Item1.Price * x.Value.Item2); + } + return APIClient.productComponents.Sum(x => x.Value.Component.Price * x.Value.Quantity); } + + + [HttpGet] public IActionResult ProductUpdate() { @@ -234,6 +238,12 @@ namespace ComputerStoreEmployeeApp.Controllers } + [HttpGet] + public IActionResult ProductCheck() + { + return View(APIClient.GetRequest>("api/main/getproductslist").Result); + } + [HttpGet] public IActionResult Register() { diff --git a/ComputerStoreEmployeeApp/Views/Home/ProductAdd.cshtml b/ComputerStoreEmployeeApp/Views/Home/ProductAdd.cshtml index a3ecbbb..4abec1e 100644 --- a/ComputerStoreEmployeeApp/Views/Home/ProductAdd.cshtml +++ b/ComputerStoreEmployeeApp/Views/Home/ProductAdd.cshtml @@ -19,9 +19,7 @@ - + @@ -34,9 +32,7 @@ @foreach (var item in Model) { - + @@ -53,7 +49,7 @@
Name:
Price:
- + @@ -63,19 +59,33 @@ } \ No newline at end of file diff --git a/ComputerStoreEmployeeApp/Views/Home/ProductCheck.cshtml b/ComputerStoreEmployeeApp/Views/Home/ProductCheck.cshtml new file mode 100644 index 0000000..30dfb2c --- /dev/null +++ b/ComputerStoreEmployeeApp/Views/Home/ProductCheck.cshtml @@ -0,0 +1,56 @@ +@using ComputerStoreContracts.ViewModels +@model List +@{ + ViewData["Title"] = "Storage"; +} +
+

Storage

+
+ +
+ @{ +
- ID - Name
- @Html.DisplayFor(modelItem => item.Key) - @Html.DisplayFor(modelItem => item.Value.Item1.Name)
+ + + + + + + + + + @foreach (var item in Model) + { + + + + + + + } + +
+ Name + + Price + + Employee username + + Components +
+ @Html.DisplayFor(modelItem => item.Name) + + @Html.DisplayFor(modelItem => item.Price) + + @Html.DisplayFor(modelItem => item.EmployeeUsername) + + + @foreach (var component in item.ProductComponents) + { + + + } +
@component.Value.Component.Name
+
+ } + \ No newline at end of file diff --git a/ComputerStoreRestAPI/ComputerStoreRestAPI.csproj b/ComputerStoreRestAPI/ComputerStoreRestAPI.csproj index 70c6935..13bf231 100644 --- a/ComputerStoreRestAPI/ComputerStoreRestAPI.csproj +++ b/ComputerStoreRestAPI/ComputerStoreRestAPI.csproj @@ -7,6 +7,7 @@ + diff --git a/ComputerStoreRestAPI/Controllers/MainController.cs b/ComputerStoreRestAPI/Controllers/MainController.cs index adbbc26..05e40c9 100644 --- a/ComputerStoreRestAPI/Controllers/MainController.cs +++ b/ComputerStoreRestAPI/Controllers/MainController.cs @@ -1,4 +1,5 @@ -using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.APIModels; +using ComputerStoreContracts.BindingModels; using ComputerStoreContracts.BusinessLogicContracts; using ComputerStoreContracts.SearchModels; using ComputerStoreContracts.ViewModels; @@ -101,6 +102,7 @@ namespace ComputerStoreRestAPI.Controllers try { return _productLogic.ReadList(null); + } catch (Exception ex) { @@ -141,7 +143,8 @@ namespace ComputerStoreRestAPI.Controllers public bool InsertProduct(ProductBindingModel product) { try - { + { + return _productLogic.Create(product); } catch (Exception ex) diff --git a/ComputerStoreRestAPI/Program.cs b/ComputerStoreRestAPI/Program.cs index b7ae8bd..bd95bb2 100644 --- a/ComputerStoreRestAPI/Program.cs +++ b/ComputerStoreRestAPI/Program.cs @@ -24,7 +24,8 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); -builder.Services.AddControllers(); +builder.Services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen();