Trying to implement assembly crud

This commit is contained in:
the 2023-04-09 11:30:54 +04:00
parent 29dd3e6dd9
commit 63f4dbe428
10 changed files with 248 additions and 7 deletions

View File

@ -32,6 +32,14 @@ namespace ComputerShopClientApp.Controllers
}
return View(APIClient.GetRequest<List<ComponentViewModel>>($"api/component/getcomponentlist"));
}
public IActionResult Assembly()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<AssemblyViewModel>>($"api/assembly/getassemblylist"));
}
[HttpGet]
public IActionResult Privacy()
@ -171,7 +179,37 @@ namespace ComputerShopClientApp.Controllers
Response.Redirect("Index");
}
[HttpGet]
public IActionResult CreateAssembly()
{
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/component/getcomponentlist");
ViewBag.CurrentComponents = new List<ComponentViewModel>();
return View();
}
[HttpPost]
public void CreateAssembly(string name, int sum)
{
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (sum <= 0)
{
throw new Exception("Сумма должна быть больше 0");
}
//APIClient.PostRequest("api/assembly/createassembly", new AssemblyBindingModel
//{
// ClientId = APIClient.Client.Id,
// AssemblyName = name,
// Price = 0,
// AssemblyComponents = new()
//});
System.Diagnostics.Debug.WriteLine("it might work");
Response.Redirect("Index");
}
[HttpPost]
public double Calc(int count, int component)

View File

@ -0,0 +1,57 @@
@using ComputerShopContracts.ViewModels
@model List<AssemblyViewModel>
@{
ViewData["Title"] = "Assembly";
}
<div class="text-center">
<h1 class="display-4">Сборки</h1>
</div>
<div class="text-center">
@{
if (Model == null)
{
<h3 class="display-4">Авторизируйтесь</h3>
return;
}
<p>
<a asp-action="CreateAssembly">Создать сборку</a>
</p>
<table class="table">
<thead>
<tr>
<th>
Номер
</th>
<th>
Сборка
</th>
<th>
Цена
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.AssemblyName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
</tr>
}
</tbody>
</table>
}
</div>

View File

@ -0,0 +1,71 @@
@using ComputerShopContracts.ViewModels
@{
ViewData["Title"] = "CreateAssembly";
}
<div class="text-center">
<h2 class="display-4">Создание сборки</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Компонент:</div>
<div class="col-8">
<select id="component" name="component" class="form-control" asp-items="@(new SelectList(@ViewBag.Components,"Id", "ComponentName"))"></select>
</div>
</div>
<div class="row">
<div class="col-4">Количество:</div>
<div class="col-8"><input type="text" name="count" id="count" /></div>
</div>
<div class="row">
<div class="col-4">Сумма:</div>
<div class="col-8"><input type="text" id="sum" name="sum" readonly /></div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Добавить компонент" class="btn btn-primary" /></div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Создать сборку" class="btn btn-primary" /></div>
</div>
</form>
<div class="text-center">
<h1 class="display-4">Список компонентов</h1>
</div>
<div class="text-center">
@{
<table class="table">
<thead>
<tr>
<th>
Номер
</th>
<th>
Компонент
</th>
<th>
Цена
</th>
</tr>
</thead>
<tbody>
@foreach (var item in (ViewBag.CurrentComponents as IEnumerable<ComponentViewModel>))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.ComponentName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cost)
</td>
</tr>
}
</tbody>
</table>
}
</div>

View File

@ -25,6 +25,9 @@
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Component">Компоненты</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Assembly">Сборки</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Личные данные</a>
</li>

View File

@ -12,6 +12,7 @@ namespace ComputerShopContracts.BindingModels
public int Id { get; set; }
public string AssemblyName { get; set; } = string.Empty;
public double Price { get; set; }
public int ClientId { get; set; }
public Dictionary<int, (IComponentModel, int)> AssemblyComponents
{
get;

View File

@ -20,5 +20,9 @@ namespace ComputerShopContracts.ViewModels
get;
set;
} = new();
public int ClientId { get; set; }
[DisplayName("ФИО клиента")]
public string ClientFIO { get; set; } = string.Empty;
}
}

View File

@ -19,8 +19,9 @@ namespace ComputerShopDatabaseImplement.Models
public string AssemblyName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _assemblyComponents =
null;
[Required]
public int ClientId { get; private set; }
private Dictionary<int, (IComponentModel, int)>? _assemblyComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> AssemblyComponents
{
@ -39,6 +40,7 @@ namespace ComputerShopDatabaseImplement.Models
public virtual List<AssemblyComponent> Components { get; set; } = new();
[ForeignKey("AssemblyId")]
public virtual List<AssemblyOrder> Orders { get; set; } = new();
public virtual Client Client { get; set; }
public static Assembly Create(ComputerShopDatabase context, AssemblyBindingModel model)
{
return new Assembly()
@ -46,6 +48,7 @@ namespace ComputerShopDatabaseImplement.Models
Id = model.Id,
AssemblyName = model.AssemblyName,
Price = model.Price,
ClientId = model.ClientId,
Components = model.AssemblyComponents.Select(x => new
AssemblyComponent
{
@ -64,12 +67,12 @@ namespace ComputerShopDatabaseImplement.Models
Id = Id,
AssemblyName = AssemblyName,
Price = Price,
AssemblyComponents = AssemblyComponents
AssemblyComponents = AssemblyComponents,
ClientId = ClientId
};
public void UpdateComponents(ComputerShopDatabase context, AssemblyBindingModel model)
{
var assemblyComponents = context.AssemblyComponents.Where(rec =>
rec.Id == model.Id).ToList();
var assemblyComponents = context.AssemblyComponents.Where(rec => rec.Id == model.Id).ToList();
if (assemblyComponents != null && assemblyComponents.Count > 0)
{ // удалили те, которых нет в модели
context.AssemblyComponents.RemoveRange(assemblyComponents.Where(rec

View File

@ -0,0 +1,61 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace ComputerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class AssemblyController : Controller
{
private readonly ILogger _logger;
private readonly IAssemblyLogic _assembly;
private readonly IComponentLogic _component;
public AssemblyController(ILogger<AssemblyController> logger, IAssemblyLogic assembly, IComponentLogic component)
{
_logger = logger;
_assembly = assembly;
_component = component;
}
[HttpGet]
public List<AssemblyViewModel>? GetAssemblyList()
{
try
{
return _assembly.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка сборок");
throw;
}
}
[HttpGet]
public List<ComponentViewModel>? GetComponentList()
{
try
{
return _component.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка компонентов");
throw;
}
}
[HttpPost]
public void CreateAssembly(AssemblyBindingModel model)
{
try
{
_assembly.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания сборки");
throw;
}
}
}
}

View File

@ -12,7 +12,7 @@ namespace ComputerShopRestApi.Controllers
{
private readonly ILogger _logger;
private readonly IComponentLogic _component;
public ComponentController(ILogger<MainController> logger, IComponentLogic component)
public ComponentController(ILogger<ComponentController> logger, IComponentLogic component)
{
_logger = logger;
_component = component;

View File

@ -14,9 +14,12 @@ builder.Logging.AddLog4Net("log4net.config");
builder.Services.AddTransient<IClientStorage, ClientStorage>();
builder.Services.AddTransient<IPurchaseStorage, PurchaseStorage>();
builder.Services.AddTransient<IComponentStorage, ComponentStorage>();
builder.Services.AddTransient<IAssemblyStorage, AssemblyStorage>();
builder.Services.AddTransient<IPurchaseLogic, PurchaseLogic>();
builder.Services.AddTransient<IClientLogic, ClientLogic>();
builder.Services.AddTransient<IComponentLogic, ComponentLogic>();
builder.Services.AddTransient<IAssemblyLogic, AssemblyLogic>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle