From 3385f124d0bf1623ee4ce2696c68f346bdeeb2ed Mon Sep 17 00:00:00 2001 From: Yuee Shiness Date: Sun, 14 May 2023 02:14:53 +0400 Subject: [PATCH] Product views + api/logic/storage fixes. --- .../Implements/ProductStorage.cs | 10 +-- ComputerStoreEmployeeApp/APIClient.cs | 4 ++ .../Controllers/HomeController.cs | 63 +++++++++++++---- .../Views/Home/ComponentCheck.cshtml | 8 +-- .../Views/Home/ProductAdd.cshtml | 68 +++++++++++++++++-- .../Views/Home/ProductMenu.cshtml | 4 +- .../Controllers/MainController.cs | 6 +- 7 files changed, 129 insertions(+), 34 deletions(-) diff --git a/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs index c78a017..55ec65d 100644 --- a/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs @@ -19,7 +19,7 @@ namespace ComputerStoreDatabaseImplement.Implements if(string.IsNullOrEmpty(model.Name) && !model.ID.HasValue) { return null; } using var context = new ComputerStoreDatabase(); - return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(x.Name)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; + return context.Products.Include(x => x.Employee).Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(x.Name)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; } public List GetFilteredList(ProductSearchModel model) @@ -32,19 +32,19 @@ namespace ComputerStoreDatabaseImplement.Implements using var context = new ComputerStoreDatabase(); if(model.EmployeeID.HasValue) { - return context.Products.Include(x => x.Employee).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList(); + return context.Products.Include(x => x.Employee).Include(x => x.Employee).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList(); } if(model.ComponentID.HasValue) { - return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).Where(p => context.ProductComponents.Where(pc => pc.ComponentID == model.ComponentID).Select(pc => pc.ProductID).Contains(p.ID)).Select(x => x.GetViewModel).ToList(); + return context.Products.Include(x => x.Employee).Include(x => x.Components).ThenInclude(x => x.Component).Where(p => context.ProductComponents.Where(pc => pc.ComponentID == model.ComponentID).Select(pc => pc.ProductID).Contains(p.ID)).Select(x => x.GetViewModel).ToList(); } - return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).Where(p => context.ConsignmentProducts.Where(cp => context.Consignments.Where(c => context.Orders.Where(o => o.DateCreate >= model.DateFrom && o.DateCreate <= model.DateTo).Select(o => o.ID).Contains(c.OrderID)).Select(c => c.ID).Contains(cp.ConsignmentID)).Select(cp => cp.ProductID).Contains(p.ID)).ToList().Select(x => x.GetViewModel).ToList(); + return context.Products.Include(x => x.Employee).Include(x => x.Components).ThenInclude(x => x.Component).Where(p => context.ConsignmentProducts.Where(cp => context.Consignments.Where(c => context.Orders.Where(o => o.DateCreate >= model.DateFrom && o.DateCreate <= model.DateTo).Select(o => o.ID).Contains(c.OrderID)).Select(c => c.ID).Contains(cp.ConsignmentID)).Select(cp => cp.ProductID).Contains(p.ID)).ToList().Select(x => x.GetViewModel).ToList(); } public List GetFullList() { using var context = new ComputerStoreDatabase(); - return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); + return context.Products.Include(x => x.Employee).Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); } public ProductViewModel? Insert(ProductBindingModel model) diff --git a/ComputerStoreEmployeeApp/APIClient.cs b/ComputerStoreEmployeeApp/APIClient.cs index cbc8058..c8bb38e 100644 --- a/ComputerStoreEmployeeApp/APIClient.cs +++ b/ComputerStoreEmployeeApp/APIClient.cs @@ -1,4 +1,5 @@ using ComputerStoreContracts.ViewModels; +using ComputerStoreDataModels.Models; using Newtonsoft.Json; using System.Net.Http.Headers; using System.Text; @@ -10,6 +11,9 @@ 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 void Connect(IConfiguration configuration) { _client.BaseAddress = new Uri(configuration["IPAddress"]); diff --git a/ComputerStoreEmployeeApp/Controllers/HomeController.cs b/ComputerStoreEmployeeApp/Controllers/HomeController.cs index 023b07b..269fa58 100644 --- a/ComputerStoreEmployeeApp/Controllers/HomeController.cs +++ b/ComputerStoreEmployeeApp/Controllers/HomeController.cs @@ -11,15 +11,11 @@ namespace ComputerStoreEmployeeApp.Controllers { public class HomeController : Controller { - private readonly ILogger _logger; - private Dictionary _productComponents; - private Dictionary _pcComponents; + private readonly ILogger _logger; public HomeController(ILogger logger) - { - _logger = logger; - _productComponents = new Dictionary(); - _pcComponents = new Dictionary(); + { + _logger = logger; } public IActionResult Index() @@ -154,24 +150,65 @@ namespace ComputerStoreEmployeeApp.Controllers [HttpGet] public IActionResult ProductMenu() { + APIClient.productComponents = new Dictionary(); return View(); } [HttpGet] public IActionResult ProductAdd() - { - return View(); + { + ViewBag.Components = Task.Run(() => APIClient.GetRequest>("api/main/getcomponentslist")).Result; + return View(APIClient.productComponents); } [HttpPost] - public void ProductAdd(string productname, double productprice) + public IActionResult ProductAdd(string productname, double productprice) { + try + { + if (string.IsNullOrEmpty(productname) || string.IsNullOrEmpty(productprice.ToString())) + { + throw new Exception("Enter product's name or product doesn't have any components."); + } + if (!Task.Run(() => APIClient.PostRequest("api/main/insertproduct", new ProductBindingModel + { + Name = productname, + Price = productprice, + EmployeeID = APIClient.Employee.ID, + ProductComponents = APIClient.productComponents + })).Result) + { + throw new InvalidOperationException("Product with such name already exists"); + } + } + catch (Exception ex) + { + APIClient.productComponents.Clear(); + ViewBag.Message = new string(ex.Message.ToString()); + ViewBag.Components = Task.Run(() => APIClient.GetRequest>("api/main/getcomponentslist")).Result; + return View(APIClient.productComponents); + } + + APIClient.productComponents.Clear(); + + return Redirect("ProductMenu"); } + - public double ProductComponents(int id) - { - var component = Task.Run(() => APIClient.GetRequest("api/main/getcomponent")).Result; + public double ProductComponents(int id, int componentquantity) + { + var component = Task.Run(() => APIClient.GetRequest($"api/main/getcomponent?id={id}")).Result; + if(APIClient.productComponents.ContainsKey(component.ID)) + { + APIClient.productComponents[component.ID] = (component, componentquantity); + } + else + { + APIClient.productComponents.Add(component.ID, (component, componentquantity)); + } + + return APIClient.productComponents.Sum(x => x.Value.Item1.Price * x.Value.Item2); } [HttpGet] public IActionResult ProductUpdate() diff --git a/ComputerStoreEmployeeApp/Views/Home/ComponentCheck.cshtml b/ComputerStoreEmployeeApp/Views/Home/ComponentCheck.cshtml index 606ff47..dbcc47e 100644 --- a/ComputerStoreEmployeeApp/Views/Home/ComponentCheck.cshtml +++ b/ComputerStoreEmployeeApp/Views/Home/ComponentCheck.cshtml @@ -12,9 +12,6 @@ - @@ -26,10 +23,7 @@ @foreach (var item in Model) { - - + diff --git a/ComputerStoreEmployeeApp/Views/Home/ProductAdd.cshtml b/ComputerStoreEmployeeApp/Views/Home/ProductAdd.cshtml index dd6ac21..a3ecbbb 100644 --- a/ComputerStoreEmployeeApp/Views/Home/ProductAdd.cshtml +++ b/ComputerStoreEmployeeApp/Views/Home/ProductAdd.cshtml @@ -1,4 +1,6 @@ -@{ +@using ComputerStoreDataModels.Models; +@model Dictionary +@{ ViewData["Title"] = "Add a product"; }
@@ -6,16 +8,74 @@
-
+
+

Component:

+ +
Quantity:
+ + +
+
+
- ID - Name
- @Html.DisplayFor(modelItem => item.ID) -
@Html.DisplayFor(modelItem => item.Name)
+ + + + + + + + + @foreach (var item in Model) + { + + + + + + } + +
+ ID + + Name + + Count +
+ @Html.DisplayFor(modelItem => item.Key) + + @Html.DisplayFor(modelItem => item.Value.Item1.Name) + + @Html.DisplayFor(modelItem => item.Value.Item2) +
+ +
+

Product:

Name:
Price:
-
+ @if (!string.IsNullOrEmpty(ViewBag.Message)) { } - \ No newline at end of file + + \ No newline at end of file diff --git a/ComputerStoreEmployeeApp/Views/Home/ProductMenu.cshtml b/ComputerStoreEmployeeApp/Views/Home/ProductMenu.cshtml index 8bebd34..b2af62b 100644 --- a/ComputerStoreEmployeeApp/Views/Home/ProductMenu.cshtml +++ b/ComputerStoreEmployeeApp/Views/Home/ProductMenu.cshtml @@ -2,7 +2,7 @@ ViewData["Title"] = "Products"; }
-
+

What do you want to do with products?

@@ -28,5 +28,5 @@
- + diff --git a/ComputerStoreRestAPI/Controllers/MainController.cs b/ComputerStoreRestAPI/Controllers/MainController.cs index 7241537..adbbc26 100644 --- a/ComputerStoreRestAPI/Controllers/MainController.cs +++ b/ComputerStoreRestAPI/Controllers/MainController.cs @@ -42,7 +42,7 @@ namespace ComputerStoreRestAPI.Controllers [HttpGet] public ComponentViewModel? GetComponent(int id) { - try + try { return _componentLogic.ReadElement(new ComponentSearchModel { ID = id } ); } @@ -180,7 +180,7 @@ namespace ComputerStoreRestAPI.Controllers } [HttpPatch] - public bool UpdateProduct(PCBindingModel pc) + public bool UpdatePC(PCBindingModel pc) { try { @@ -194,7 +194,7 @@ namespace ComputerStoreRestAPI.Controllers } [HttpPost] - public bool InsertProduct(PCBindingModel pc) + public bool InsertPC(PCBindingModel pc) { try {