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 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<int, (IComponentModel Component, int Quantity)> ProductComponents { get; set; } = new();
public Dictionary<int, (IComponentModel, int)> 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>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ComputerStoreDataModels\ComputerStoreDataModels.csproj" />
</ItemGroup>

View File

@ -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<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.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace ComputerStoreDataModels.Models

View File

@ -11,6 +11,6 @@ namespace ComputerStoreDataModels.Models
public string Name { get; }
public double Price { 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.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()
{
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)

View File

@ -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)
{

View File

@ -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
{

View File

@ -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<int, (IComponentModel, int)>? _productComponents = null;
private Dictionary<int, (IComponentModel Component, int Quantity)>? _productComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> ProductComponents
@ -40,13 +41,13 @@ namespace ComputerStoreDatabaseImplement.Models
return _productComponents;
}
}
[ForeignKey("ProductID")]
public virtual List<ProductComponent> Components { get; set; } = new();
[ForeignKey("ProductID")]
public virtual List<Consignment> 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),

View File

@ -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<int, (IComponentModel, int)>? productComponents;
public static Dictionary<int, (IComponentModel, int)>? pcComponents;
public static Dictionary<int, (IComponentModel Component, int Quantity)>? productComponents;
public static Dictionary<int, (IComponentModel Component, int Quantity)>? 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<T>(result);
return JsonConvert.DeserializeObject<T>(result, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
}
else
{
@ -38,6 +41,7 @@ namespace ComputerStoreEmployeeApp
public static async Task<bool> PostRequest<T>(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<bool> PatchRequest<T>(string requestUrl, T 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 ComputerStoreDataModels.Models;
using ComputerStoreEmployeeApp.Models;
@ -66,7 +67,7 @@ namespace ComputerStoreEmployeeApp.Controllers
[HttpGet]
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();
}
@ -121,7 +122,7 @@ namespace ComputerStoreEmployeeApp.Controllers
[HttpGet]
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();
}
@ -144,20 +145,20 @@ namespace ComputerStoreEmployeeApp.Controllers
[HttpGet]
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]
public IActionResult ProductMenu()
{
APIClient.productComponents = new Dictionary<int, (IComponentModel, int)>();
APIClient.productComponents = new Dictionary<int, (IComponentModel Component, int Quantity)>();
return View();
}
[HttpGet]
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);
}
@ -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<ProductBindingModel>("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<List<ComponentViewModel>>("api/main/getcomponentslist")).Result;
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("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<ComponentViewModel>($"api/main/getcomponent?id={id}")).Result;
var component = APIClient.GetRequest<ComponentViewModel>($"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<List<ProductViewModel>>("api/main/getproductslist").Result);
}
[HttpGet]
public IActionResult Register()
{

View File

@ -19,9 +19,7 @@
<table class="table">
<thead>
<tr>
<th>
ID
</th>
<th>
Name
</th>
@ -34,9 +32,7 @@
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Key)
</td>
<td>
@Html.DisplayFor(modelItem => item.Value.Item1.Name)
</td>
@ -53,7 +49,7 @@
<div class="productname" style="text-align: left; font-size: 15px;">Name:</div>
<input type="text" id="productname" name="productname" />
<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" />
</div>
</div>
@ -63,19 +59,33 @@
}
</form>
<script>
window.onsubmit = function(){
localStorage.clear();
}
window.onload = function(){
$('#productprice').val(localStorage.getItem("price"));
}
$('#componentbtn').on("click", function(){
var count = $('#componentquantity').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",
url: "/Home/ProductComponents",
data: { id: id, componentquantity : count},
success: function(result)
{
data: { id: id, componentquantity: count },
success: function (result) {
$('#productprice').val(result);
}
});
});
}
</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>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="6.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

View File

@ -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)

View File

@ -24,7 +24,8 @@ builder.Services.AddTransient<IProductLogic, ProductLogic>();
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
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();