Product views + api/logic/storage fixes.

This commit is contained in:
Yuee Shiness 2023-05-14 02:14:53 +04:00
parent 2fec7dfdf0
commit 3385f124d0
7 changed files with 129 additions and 34 deletions

View File

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

View File

@ -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<int, (IComponentModel, int)>? productComponents;
public static Dictionary<int, (IComponentModel, int)>? pcComponents;
public static void Connect(IConfiguration configuration)
{
_client.BaseAddress = new Uri(configuration["IPAddress"]);

View File

@ -11,15 +11,11 @@ namespace ComputerStoreEmployeeApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private Dictionary<int, (IComponentModel, int)> _productComponents;
private Dictionary<int, (IComponentModel, int)> _pcComponents;
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
_productComponents = new Dictionary<int, (IComponentModel, int)>();
_pcComponents = new Dictionary<int, (IComponentModel, int)>();
{
_logger = logger;
}
public IActionResult Index()
@ -154,24 +150,65 @@ namespace ComputerStoreEmployeeApp.Controllers
[HttpGet]
public IActionResult ProductMenu()
{
APIClient.productComponents = new Dictionary<int, (IComponentModel, int)>();
return View();
}
[HttpGet]
public IActionResult ProductAdd()
{
return View();
{
ViewBag.Components = Task.Run(() => APIClient.GetRequest<List<ComponentViewModel>>("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<ProductBindingModel>("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<List<ComponentViewModel>>("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<ComponentViewModel>("api/main/getcomponent")).Result;
public double ProductComponents(int id, int componentquantity)
{
var component = Task.Run(() => APIClient.GetRequest<ComponentViewModel>($"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()

View File

@ -12,9 +12,6 @@
<table class="table">
<thead>
<tr>
<th>
ID
</th>
<th>
Name
</th>
@ -26,10 +23,7 @@
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ID)
</td>
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>

View File

@ -1,4 +1,6 @@
@{
@using ComputerStoreDataModels.Models;
@model Dictionary<int, (IComponentModel, int)>
@{
ViewData["Title"] = "Add a product";
}
<div class="text-center">
@ -6,16 +8,74 @@
</div>
<form method="post">
<div class="flex-container" style="flex-direction: row; justify-content: space-between; display: flex; height: 100vh;">
<div class="flex-containerB1" style="flex-direction: column; flex: 4; gap: 20px; display: flex; align-items: center; justify-content: center;">
<div class="flex-containerB1" style="flex-direction: column; flex: 4; gap: 20px; display: flex; align-items: center; justify-content: center;">
<h1>Component:</h1>
<select id="component" name="component" class="form-control" asp-items="@(new SelectList(@ViewBag.Components,"ID","Name"))"></select>
<div class="productprice" style="text-align: left; font-size: 15px;">Quantity:</div>
<input type="text" id="componentquantity" name="componentquantity" />
<input type="button" id="componentbtn" value="ADD" class="btn btn-primary" />
</div>
<div class="flex-containerB2" style="flex-direction: column; flex: 4; gap: 20px; display: flex; align-items: center; justify-content: center;">
<table class="table">
<thead>
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th>
Count
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Key)
</td>
<td>
@Html.DisplayFor(modelItem => item.Value.Item1.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Value.Item2)
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="flex-containerB3" style="flex-direction: column; flex: 4; gap: 20px; display: flex; align-items: center; justify-content: center;">
<h1>Product:</h1>
<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="submit" id="productbtn" value="ADD" class="btn btn-primary" />
</div>
</div>
</div>
@if (!string.IsNullOrEmpty(ViewBag.Message))
{
<script>alert("@ViewBag.Message");</script>
}
</form>
</form>
<script>
$('#componentbtn').on("click", function(){
var count = $('#componentquantity').val();
var id = $('#component').val();
$.ajax({
method: "POST",
url: "/Home/ProductComponents",
data: { id: id, componentquantity : count},
success: function(result)
{
$('#productprice').val(result);
}
});
});
</script>

View File

@ -2,7 +2,7 @@
ViewData["Title"] = "Products";
}
<div class="text-center">
<form method="post">
<div class="flex-container" style="flex-direction: row; justify-content: center; display: flex; height: 100vh;">
<div class="flex-containerA1" style="flex-direction: column; align-items: center; display: flex; gap: 35px">
<h1>What do you want to do with products?</h1>
@ -28,5 +28,5 @@
</div>
</div>
</div>
</form>
</div>

View File

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