Frontend component views + api logic fixes.

This commit is contained in:
Yuee Shiness 2023-05-13 21:14:47 +04:00
parent e5eec7f4ca
commit aee700f1fb
8 changed files with 180 additions and 43 deletions

View File

@ -31,7 +31,7 @@ namespace ComputerStoreEmployeeApp
}
}
public static async Task PostRequest<T>(string requestUrl, T model)
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");
@ -43,9 +43,10 @@ namespace ComputerStoreEmployeeApp
{
throw new Exception(result);
}
return Convert.ToBoolean(result);
}
public static async Task PatchRequest<T>(string requestUrl, T model)
public static async Task<bool> PatchRequest<T>(string requestUrl, T model)
{
var json = JsonConvert.SerializeObject(model);
var data = new StringContent(json, Encoding.UTF8, "application/json-patch+json");
@ -57,6 +58,18 @@ namespace ComputerStoreEmployeeApp
{
throw new Exception(result);
}
return Convert.ToBoolean(result);
}
public static async Task<bool> DeleteRequest<T>(string requestUrl)
{
var response = await _client.DeleteAsync(requestUrl);
var result = response.Content.ReadAsStringAsync().Result;
if (!response.IsSuccessStatusCode)
{
throw new Exception(result);
}
return Convert.ToBoolean(result);
}
}
}

View File

@ -1,6 +1,7 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.ViewModels;
using ComputerStoreEmployeeApp.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using System.Data;
using System.Diagnostics;
@ -22,13 +23,12 @@ namespace ComputerStoreEmployeeApp.Controllers
return View();
}
[HttpGet]
public IActionResult ComponentMenu()
{
return View();
}
[HttpGet]
public IActionResult ComponentAdd()
{
return View();
@ -44,11 +44,14 @@ namespace ComputerStoreEmployeeApp.Controllers
throw new Exception("Enter component's name and price.");
}
Task.Run(() => APIClient.PostRequest("api/main/insertcomponent", new ComponentBindingModel
if(!Task.Run(() => APIClient.PostRequest("api/main/insertcomponent", new ComponentBindingModel
{
Name = componentname,
Price = componentprice
}));
})).Result)
{
throw new InvalidOperationException("Component with such name already exists");
}
}
catch (Exception ex)
{
@ -62,25 +65,85 @@ namespace ComputerStoreEmployeeApp.Controllers
[HttpGet]
public IActionResult ComponentUpdate()
{
ViewBag.Components = Task.Run(() => APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist")).Result;
return View();
}
[HttpPost]
public void ComponentUpdate(string val)
public IActionResult ComponentUpdate(int component, string? componentname, double? componentprice)
{
try
{
if (string.IsNullOrEmpty(componentname) && !componentprice.HasValue)
{
throw new Exception("Enter at least one field.");
}
if(string.IsNullOrEmpty(componentname))
{
Task.Run(() => APIClient.PatchRequest("api/main/updatecomponent", new ComponentBindingModel
{
ID = component,
Price = componentprice.Value
}));
}
else if (!componentprice.HasValue)
{
Task.Run(() => APIClient.PatchRequest("api/main/updatecomponent", new ComponentBindingModel
{
ID = component,
Name = componentname
}));
}
else
{
Task.Run(() => APIClient.PatchRequest("api/main/updatecomponent", new ComponentBindingModel
{
ID = component,
Name = componentname,
Price = componentprice.Value
}));
}
}
catch (Exception ex)
{
ViewBag.Message = new string(ex.Message.ToString());
return View();
}
return Redirect("ComponentMenu");
}
[HttpGet]
public IActionResult ComponentDelete()
{
ViewBag.Components = Task.Run(() => APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist")).Result;
return View();
}
[HttpPost]
public void ComponentDelete(string val)
public IActionResult ComponentDelete(int component)
{
try
{
Task.Run(() => APIClient.DeleteRequest<string>($"api/main/deletecomponent/{component}"));
}
catch (Exception ex)
{
ViewBag.Message = new string(ex.Message.ToString());
return View();
}
return Redirect("ComponentMenu");
}
[HttpGet]
public IActionResult ComponentCheck()
{
return View(Task.Run(() => APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist")).Result);
}
[HttpGet]

View File

@ -6,8 +6,7 @@
</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="componenttitle" style="text-align: center; font-size: 25px;">ADD A COMPONENT</div>
<div class="flex-containerB1" style="flex-direction: column; flex: 4; gap: 20px; display: flex; align-items: center; justify-content: center;">
<div class="componentname" style="text-align: left; font-size: 15px;">Name:</div>
<input type="text" id="componentname" name="componentname" />
<div class="componentprice" style="text-align: left; font-size: 15px;">Price:</div>

View File

@ -0,0 +1,44 @@
@using ComputerStoreContracts.ViewModels
@model List<ComponentViewModel>
@{
ViewData["Title"] = "Storage";
}
<div class="text-center">
<h1 class="display-4">Storage</h1>
</div>
<div class="text-center">
@{
<table class="table">
<thead>
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th>
Price
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
</tr>
}
</tbody>
</table>
}
</div>

View File

@ -0,0 +1,14 @@
@{
ViewData["Title"] = "Delete a component";
}
<div class="text-center">
<h2 class="display-4">Delete a component</h2>
</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;">
<select id="component" name="component" class="form-control" asp-items="@(new SelectList(@ViewBag.Components,"ID","Name"))"></select>
<input type="submit" id="componentbtn" value="DELETE" class="btn btn-primary" />
</div>
</div>
</form>

View File

@ -1,44 +1,30 @@
@{
ViewData["Title"] = "Components";
}
<div class="text-center">
<form method="post">
<div class="text-center">
<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 components?</h1>
<div class="flex-containerB1" style="display: flex; align-items: center; margin-top: 120px;">
<input type="submit" id="compadd" value="ADD" class="btn btn-primary" style="font-size: 20pt; width: 130px;" />
<form method="get" asp-controller="Home" asp-action="ComponentAdd">
<input type="submit" id="compadd" value="ADD" class="btn btn-primary" style="font-size: 20pt; width: 130px;" />
</form>
</div>
<div class="flex-containerB2" style="display: flex; align-items: center;">
<input type="submit" id="compupdate" value="UPDATE" class="btn btn-primary" style="font-size: 20pt; width: 130px;" />
<form method="get" asp-controller="Home" asp-action="ComponentUpdate">
<input type="submit" id="compupdate" value="UPDATE" class="btn btn-primary" style="font-size: 20pt; width: 130px;" />
</form>
</div>
<div class="flex-containerB3" style="display: flex; align-items: center;">
<input type="submit" id="compdel" value="DELETE" class="btn btn-primary" style="font-size: 20pt; width: 130px;" />
<form method="get" asp-controller="Home" asp-action="ComponentDelete">
<input type="submit" id="compdel" value="DELETE" class="btn btn-primary" style="font-size: 20pt; width: 130px;" />
</form>
</div>
<div class="flex-containerB3" style="display: flex; align-items: center;">
<form method="get" asp-controller="Home" asp-action="ComponentCheck">
<input type="submit" id="compcheck" value="STORAGE" class="btn btn-primary" style="font-size: 20pt; width: 130px;" />
</form>
</div>
</div>
</div>
</form>
<script>
$('#compadd').on('click', function () {
evt.preventDefault();
document.location.href='@Url.Action("ComponentAdd","Home")';
});
$('#compupdate').on('click', function () {
evt.preventDefault();
document.location.href='@Url.Action("ComponentUpdate","Home")';
});
$('#compdel').on('click', function () {
evt.preventDefault();
document.location.href='@Url.Action("ComponentDelete","Home")';
});
$('#compcheck').on('click', function () {
evt.preventDefault();
document.location.href = '@Url.Action("ComponentStorage","Home")';
});
</script>
</div>
</div>

View File

@ -0,0 +1,18 @@
@{
ViewData["Title"] = "Update a component";
}
<div class="text-center">
<h2 class="display-4">Update a component</h2>
</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;">
<select id="component" name="component" class="form-control" asp-items="@(new SelectList(@ViewBag.Components,"ID","Name"))"></select>
<div class="componentname" style="text-align: left; font-size: 15px;">Name:</div>
<input type="text" id="componentname" name="componentname" />
<div class="componentprice" style="text-align: left; font-size: 15px;">Price:</div>
<input type="text" id="componentprice" name="componentprice" />
<input type="submit" id="componentbtn" value="CHANGE" class="btn btn-primary" />
</div>
</div>
</form>

View File

@ -31,19 +31,19 @@ namespace ComputerStoreRestAPI.Controllers
{
return _componentLogic.ReadList(null);
}
catch(Exception ex)
catch (Exception ex)
{
_logger.LogError(ex, "Receiving list of components error.");
throw;
}
}
[HttpDelete]
public bool DeleteComponent(ComponentBindingModel component)
[HttpDelete("{id}")]
public bool DeleteComponent(int id)
{
try
{
return _componentLogic.Delete(component);
return _componentLogic.Delete(new ComponentBindingModel { ID = id});
}
catch(Exception ex)
{