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; } if(string.IsNullOrEmpty(model.Name) && !model.ID.HasValue) { return null; }
using var context = new ComputerStoreDatabase(); 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) public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
@ -32,19 +32,19 @@ 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).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) 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() public List<ProductViewModel> GetFullList()
{ {
using var context = new ComputerStoreDatabase(); 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) public ProductViewModel? Insert(ProductBindingModel model)

View File

@ -1,4 +1,5 @@
using ComputerStoreContracts.ViewModels; using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Models;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text; using System.Text;
@ -10,6 +11,9 @@ 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, int)>? 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"]);

View File

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

View File

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

View File

@ -1,4 +1,6 @@
@{ @using ComputerStoreDataModels.Models;
@model Dictionary<int, (IComponentModel, int)>
@{
ViewData["Title"] = "Add a product"; ViewData["Title"] = "Add a product";
} }
<div class="text-center"> <div class="text-center">
@ -7,6 +9,47 @@
<form method="post"> <form method="post">
<div class="flex-container" style="flex-direction: row; justify-content: space-between; display: flex; height: 100vh;"> <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> <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>
@ -19,3 +62,20 @@
<script>alert("@ViewBag.Message");</script> <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"; ViewData["Title"] = "Products";
} }
<div class="text-center"> <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-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"> <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> <h1>What do you want to do with products?</h1>
@ -28,5 +28,5 @@
</div> </div>
</div> </div>
</div> </div>
</form>
</div> </div>

View File

@ -180,7 +180,7 @@ namespace ComputerStoreRestAPI.Controllers
} }
[HttpPatch] [HttpPatch]
public bool UpdateProduct(PCBindingModel pc) public bool UpdatePC(PCBindingModel pc)
{ {
try try
{ {
@ -194,7 +194,7 @@ namespace ComputerStoreRestAPI.Controllers
} }
[HttpPost] [HttpPost]
public bool InsertProduct(PCBindingModel pc) public bool InsertPC(PCBindingModel pc)
{ {
try try
{ {