A damn bunch of fixes + a couple of views for product.

This commit is contained in:
Yuee Shiness 2023-05-14 22:11:06 +04:00
parent 3385f124d0
commit eeda90cb78
17 changed files with 181 additions and 55 deletions

View File

@ -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<int, (ComponentViewModel Component, int Quantity)> ProductComponents { get; set; } = new();
}
}

View File

@ -1,9 +1,5 @@
using ComputerStoreDataModels.Models; using ComputerStoreDataModels.Models;
using System; using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreContracts.BindingModels namespace ComputerStoreContracts.BindingModels
{ {
@ -16,6 +12,13 @@ namespace ComputerStoreContracts.BindingModels
public int EmployeeID { get; set; } public int EmployeeID { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents { get; set; } = new(); public Dictionary<int, (IComponentModel Component, int Quantity)> ProductComponents { get; set; } = new();
[JsonConstructor]
public ProductBindingModel(Dictionary<int, (ComponentBindingModel Component, int Quantity)> ProductComponents)
{
this.ProductComponents = ProductComponents.ToDictionary(x => x.Key, x => (x.Value.Component as IComponentModel, x.Value.Quantity));
}
public ProductBindingModel() { }
} }
} }

View File

@ -6,6 +6,10 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ComputerStoreDataModels\ComputerStoreDataModels.csproj" /> <ProjectReference Include="..\ComputerStoreDataModels\ComputerStoreDataModels.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -1,8 +1,11 @@
using ComputerStoreDataModels.Models; using ComputerStoreContracts.BindingModels;
using ComputerStoreDataModels.Models;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -23,8 +26,13 @@ namespace ComputerStoreContracts.ViewModels
[DisplayName("Employee's username")] [DisplayName("Employee's username")]
public string EmployeeUsername { get; set; } = string.Empty; public string EmployeeUsername { get; set; } = string.Empty;
public Dictionary<int, (IComponentModel, int)> ProductComponents { get; set; } = new(); public Dictionary<int, (IComponentModel Component, int Quantity)> ProductComponents { get; set; } = new();
[JsonConstructor]
public ProductViewModel(Dictionary<int, (ComponentBindingModel Component, int Quantity)> ProductComponents)
{
this.ProductComponents = ProductComponents.ToDictionary(x => x.Key, x => (x.Value.Component as IComponentModel, x.Value.Quantity));
}
public ProductViewModel() { }
} }
} }

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ComputerStoreDataModels.Models namespace ComputerStoreDataModels.Models

View File

@ -11,6 +11,6 @@ namespace ComputerStoreDataModels.Models
public string Name { get; } public string Name { get; }
public double Price { get; } public double Price { get; }
public int EmployeeID { get; } public int EmployeeID { get; }
Dictionary<int,(IComponentModel,int)> ProductComponents { get; } Dictionary<int,(IComponentModel Component, int Quantity)> ProductComponents { get; }
} }
} }

View File

@ -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.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<PCViewModel> GetFullList() public List<PCViewModel> GetFullList()
{ {
using var context = new ComputerStoreDatabase(); 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) public PCViewModel? Insert(PCBindingModel model)

View File

@ -32,7 +32,7 @@ namespace ComputerStoreDatabaseImplement.Implements
using var context = new ComputerStoreDatabase(); using var context = new ComputerStoreDatabase();
if(model.EmployeeID.HasValue) 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) if(model.ComponentID.HasValue)
{ {

View File

@ -56,6 +56,7 @@ namespace ComputerStoreDatabaseImplement.Models
Name = model.Name, Name = model.Name,
Price = model.Price, Price = model.Price,
EmployeeID = model.EmployeeID, EmployeeID = model.EmployeeID,
Employee = context.Employees.First(x => x.ID == model.EmployeeID),
RequestID = model.RequestID, RequestID = model.RequestID,
Components = model.PCComponents.Select(x => new RequestComponent Components = model.PCComponents.Select(x => new RequestComponent
{ {

View File

@ -1,6 +1,7 @@
using ComputerStoreContracts.BindingModels; using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.ViewModels; using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Models; using ComputerStoreDataModels.Models;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
@ -26,7 +27,7 @@ namespace ComputerStoreDatabaseImplement.Models
public int EmployeeID { get; private set; } public int EmployeeID { get; private set; }
private Dictionary<int, (IComponentModel, int)>? _productComponents = null; private Dictionary<int, (IComponentModel Component, int Quantity)>? _productComponents = null;
[NotMapped] [NotMapped]
public Dictionary<int, (IComponentModel, int)> ProductComponents public Dictionary<int, (IComponentModel, int)> ProductComponents
@ -56,6 +57,7 @@ namespace ComputerStoreDatabaseImplement.Models
Name = model.Name, Name = model.Name,
Price = model.Price, Price = model.Price,
EmployeeID = model.EmployeeID, EmployeeID = model.EmployeeID,
Employee = context.Employees.First(x => x.ID == model.EmployeeID),
Components = model.ProductComponents.Select(x => new ProductComponent Components = model.ProductComponents.Select(x => new ProductComponent
{ {
Component = context.Components.First(y => y.ID == x.Key), Component = context.Components.First(y => y.ID == x.Key),

View File

@ -1,4 +1,5 @@
using ComputerStoreContracts.ViewModels; using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Models; using ComputerStoreDataModels.Models;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Net.Http.Headers; using System.Net.Http.Headers;
@ -11,9 +12,8 @@ namespace ComputerStoreEmployeeApp
private static readonly HttpClient _client = new(); private static readonly HttpClient _client = new();
public static EmployeeViewModel? Employee { get; set; } = null; public static EmployeeViewModel? Employee { get; set; } = null;
public static Dictionary<int, (IComponentModel, int)>? productComponents; public static Dictionary<int, (IComponentModel Component, int Quantity)>? productComponents;
public static Dictionary<int, (IComponentModel, int)>? pcComponents; public static Dictionary<int, (IComponentModel Component, int Quantity)>? pcComponents;
public static void Connect(IConfiguration configuration) public static void Connect(IConfiguration configuration)
{ {
_client.BaseAddress = new Uri(configuration["IPAddress"]); _client.BaseAddress = new Uri(configuration["IPAddress"]);
@ -27,7 +27,10 @@ namespace ComputerStoreEmployeeApp
var result = response.Content.ReadAsStringAsync().Result; var result = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode)
{ {
return JsonConvert.DeserializeObject<T>(result); return JsonConvert.DeserializeObject<T>(result, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
} }
else else
{ {
@ -38,6 +41,7 @@ namespace ComputerStoreEmployeeApp
public static async Task<bool> PostRequest<T>(string requestUrl, T model) public static async Task<bool> PostRequest<T>(string requestUrl, T model)
{ {
var json = JsonConvert.SerializeObject(model); var json = JsonConvert.SerializeObject(model);
var data = new StringContent(json, Encoding.UTF8, "application/json"); var data = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _client.PostAsync(requestUrl, data); var response = await _client.PostAsync(requestUrl, data);
@ -47,9 +51,8 @@ namespace ComputerStoreEmployeeApp
{ {
throw new Exception(result); throw new Exception(result);
} }
return Convert.ToBoolean(result); return true;
} }
public static async Task<bool> PatchRequest<T>(string requestUrl, T model) public static async Task<bool> PatchRequest<T>(string requestUrl, T model)
{ {
var json = JsonConvert.SerializeObject(model); var json = JsonConvert.SerializeObject(model);

View File

@ -1,4 +1,5 @@
using ComputerStoreContracts.BindingModels; using ComputerStoreContracts.APIModels;
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.ViewModels; using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Models; using ComputerStoreDataModels.Models;
using ComputerStoreEmployeeApp.Models; using ComputerStoreEmployeeApp.Models;
@ -66,7 +67,7 @@ namespace ComputerStoreEmployeeApp.Controllers
[HttpGet] [HttpGet]
public IActionResult ComponentUpdate() public IActionResult ComponentUpdate()
{ {
ViewBag.Components = Task.Run(() => APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist")).Result; ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
return View(); return View();
} }
@ -121,7 +122,7 @@ namespace ComputerStoreEmployeeApp.Controllers
[HttpGet] [HttpGet]
public IActionResult ComponentDelete() public IActionResult ComponentDelete()
{ {
ViewBag.Components = Task.Run(() => APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist")).Result; ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
return View(); return View();
} }
@ -144,20 +145,20 @@ namespace ComputerStoreEmployeeApp.Controllers
[HttpGet] [HttpGet]
public IActionResult ComponentCheck() public IActionResult ComponentCheck()
{ {
return View(Task.Run(() => APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist")).Result); return View( APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result);
} }
[HttpGet] [HttpGet]
public IActionResult ProductMenu() public IActionResult ProductMenu()
{ {
APIClient.productComponents = new Dictionary<int, (IComponentModel, int)>(); APIClient.productComponents = new Dictionary<int, (IComponentModel Component, int Quantity)>();
return View(); return View();
} }
[HttpGet] [HttpGet]
public IActionResult ProductAdd() public IActionResult ProductAdd()
{ {
ViewBag.Components = Task.Run(() => APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist")).Result; ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
return View(APIClient.productComponents); 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."); throw new Exception("Enter product's name or product doesn't have any components.");
} }
if (!Task.Run(() => APIClient.PostRequest<ProductBindingModel>("api/main/insertproduct", new ProductBindingModel if (!Task.Run(() => APIClient.PostRequest("api/main/insertproduct", new ProductBindingModel
{ {
Name = productname, Name = productname,
Price = productprice, Price = productprice,
@ -186,7 +187,7 @@ namespace ComputerStoreEmployeeApp.Controllers
{ {
APIClient.productComponents.Clear(); APIClient.productComponents.Clear();
ViewBag.Message = new string(ex.Message.ToString()); ViewBag.Message = new string(ex.Message.ToString());
ViewBag.Components = Task.Run(() => APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist")).Result; ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
return View(APIClient.productComponents); return View(APIClient.productComponents);
} }
@ -196,9 +197,10 @@ namespace ComputerStoreEmployeeApp.Controllers
} }
[HttpPost]
public double ProductComponents(int id, int componentquantity) public double ProductComponents(int id, int componentquantity)
{ {
var component = Task.Run(() => APIClient.GetRequest<ComponentViewModel>($"api/main/getcomponent?id={id}")).Result; var component = APIClient.GetRequest<ComponentViewModel>($"api/main/getcomponent?id={id}").Result;
if(APIClient.productComponents.ContainsKey(component.ID)) if(APIClient.productComponents.ContainsKey(component.ID))
{ {
APIClient.productComponents[component.ID] = (component, componentquantity); APIClient.productComponents[component.ID] = (component, componentquantity);
@ -207,9 +209,11 @@ namespace ComputerStoreEmployeeApp.Controllers
{ {
APIClient.productComponents.Add(component.ID, (component, componentquantity)); APIClient.productComponents.Add(component.ID, (component, componentquantity));
} }
return APIClient.productComponents.Sum(x => x.Value.Component.Price * x.Value.Quantity);
return APIClient.productComponents.Sum(x => x.Value.Item1.Price * x.Value.Item2);
} }
[HttpGet] [HttpGet]
public IActionResult ProductUpdate() public IActionResult ProductUpdate()
{ {
@ -234,6 +238,12 @@ namespace ComputerStoreEmployeeApp.Controllers
} }
[HttpGet]
public IActionResult ProductCheck()
{
return View(APIClient.GetRequest<List<ProductViewModel>>("api/main/getproductslist").Result);
}
[HttpGet] [HttpGet]
public IActionResult Register() public IActionResult Register()
{ {

View File

@ -19,9 +19,7 @@
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th>
ID
</th>
<th> <th>
Name Name
</th> </th>
@ -34,9 +32,7 @@
@foreach (var item in Model) @foreach (var item in Model)
{ {
<tr> <tr>
<td>
@Html.DisplayFor(modelItem => item.Key)
</td>
<td> <td>
@Html.DisplayFor(modelItem => item.Value.Item1.Name) @Html.DisplayFor(modelItem => item.Value.Item1.Name)
</td> </td>
@ -53,7 +49,7 @@
<div class="productname" style="text-align: left; font-size: 15px;">Name:</div> <div class="productname" style="text-align: left; font-size: 15px;">Name:</div>
<input type="text" id="productname" name="productname" /> <input type="text" id="productname" name="productname" />
<div class="productprice" style="text-align: left; font-size: 15px;">Price:</div> <div class="productprice" style="text-align: left; font-size: 15px;">Price:</div>
<input type="text" id="productprice" name="productprice" /> <input type="text" id="productprice" name="productprice" readonly />
<input type="submit" id="productbtn" value="ADD" class="btn btn-primary" /> <input type="submit" id="productbtn" value="ADD" class="btn btn-primary" />
</div> </div>
</div> </div>
@ -63,19 +59,33 @@
} }
</form> </form>
<script> <script>
window.onsubmit = function(){
localStorage.clear();
}
window.onload = function(){
$('#productprice').val(localStorage.getItem("price"));
}
$('#componentbtn').on("click", function(){ $('#componentbtn').on("click", function(){
var count = $('#componentquantity').val(); var count = $('#componentquantity').val();
var id = $('#component').val(); var id = $('#component').val();
$.ajax({
$.when(ajax1(id, count)).done(function (a1) {
localStorage.setItem("price", $('#productprice').val());
window.location.href = '/Home/ProductAdd'
})
});
function ajax1(id, count) {
return $.ajax({
method: "POST", method: "POST",
url: "/Home/ProductComponents", url: "/Home/ProductComponents",
data: { id: id, componentquantity: count }, data: { id: id, componentquantity: count },
success: function(result) success: function (result) {
{
$('#productprice').val(result); $('#productprice').val(result);
} }
}); });
}); }
</script> </script>

View File

@ -0,0 +1,56 @@
@using ComputerStoreContracts.ViewModels
@model List<ProductViewModel>
@{
ViewData["Title"] = "Storage";
}
<div class="text-center">
<h1 class="display-4">Storage</h1>
</div>
<div class="text-center">
@{
<table class="table">
<thead>
<tr>
<th>
Name
</th>
<th>
Price
</th>
<th>
Employee username
</th>
<th>
Components
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmployeeUsername)
</td>
<td>
<table>
@foreach (var component in item.ProductComponents)
{
<tr><td>@component.Value.Component.Name</td></tr>
}
</table>
</td>
</tr>
}
</tbody>
</table>
}
</div>

View File

@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="6.1.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="6.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup> </ItemGroup>

View File

@ -1,4 +1,5 @@
using ComputerStoreContracts.BindingModels; using ComputerStoreContracts.APIModels;
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts; using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels; using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.ViewModels; using ComputerStoreContracts.ViewModels;
@ -101,6 +102,7 @@ namespace ComputerStoreRestAPI.Controllers
try try
{ {
return _productLogic.ReadList(null); return _productLogic.ReadList(null);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -142,6 +144,7 @@ namespace ComputerStoreRestAPI.Controllers
{ {
try try
{ {
return _productLogic.Create(product); return _productLogic.Create(product);
} }
catch (Exception ex) catch (Exception ex)

View File

@ -24,7 +24,8 @@ builder.Services.AddTransient<IProductLogic, ProductLogic>();
builder.Services.AddTransient<IConsignmentLogic,ConsignmentLogic>(); builder.Services.AddTransient<IConsignmentLogic,ConsignmentLogic>();
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 // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();