полный CRUD статей затрат

This commit is contained in:
Мк Игорь 2023-05-25 12:51:38 +04:00
parent d5bfb16824
commit 0381c343c6
7 changed files with 168 additions and 3 deletions

View File

@ -22,7 +22,7 @@ namespace CarServiceDatabase.Implements
{ {
using var context = new CarServiceDbContext(); using var context = new CarServiceDbContext();
return context.Items return context.Items
.Where(x => x.Id == model.Id) .Where(x => x.WorkerId == model.Id)
.Include(x => x.Worker) .Include(x => x.Worker)
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();

View File

@ -13,12 +13,14 @@ namespace CarServiceWebApp.Controllers
private readonly ILogger<HomeController> _logger; private readonly ILogger<HomeController> _logger;
private readonly IWorkLogic _workLogic; private readonly IWorkLogic _workLogic;
private readonly IWorkerLogic _workerLogic; private readonly IWorkerLogic _workerLogic;
private readonly IItemLogic _itemLogic;
public HomeController(ILogger<HomeController> logger, IWorkLogic workLogic, IWorkerLogic workerLogic) public HomeController(ILogger<HomeController> logger, IWorkLogic workLogic, IWorkerLogic workerLogic, IItemLogic itemLogic)
{ {
_logger = logger; _logger = logger;
_workLogic = workLogic; _workLogic = workLogic;
_workerLogic = workerLogic; _workerLogic = workerLogic;
_itemLogic = itemLogic;
} }
/// <summary> /// <summary>
/// Главная страница /// Главная страница
@ -170,6 +172,82 @@ namespace CarServiceWebApp.Controllers
return Redirect("~/Home/Works"); return Redirect("~/Home/Works");
} }
/// <summary> /// <summary>
/// Вывод формы со списком статей затрат
/// </summary>
/// <returns></returns>
public IActionResult Items()
{
if (CurrentUser.UserId < 1)
{
return Redirect("~/Home/Index");
}
var Items = _itemLogic.ReadList(new() { Id = CurrentUser.UserId });
ViewBag.Items = Items;
if (Items?.Count == 0)
{
ViewBag.Exception = "Пока нет запчастей";
}
return View();
}
/// <summary>
/// Отображение формы CRUD запчасти
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
[HttpGet]
public IActionResult Item(int Id)
{
ViewBag.Item = _itemLogic.ReadElement(new() { Id = Id });
return View();
}
/// <summary>
/// Обновление запчасти
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public IActionResult UpdateItem(ItemBindingModel model)
{
_itemLogic.Update(model);
return Redirect("~/Home/Items");
}
/// <summary>
/// Удаление запчасти
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public IActionResult DeleteItem(ItemBindingModel model)
{
_itemLogic.Delete(model);
return Redirect("~/Home/Items");
}
/// <summary>
/// Отображение формы добавления запчасти
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreateItem() => View();
/// <summary>
/// Добавление запчасти
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public IActionResult CreateItem(ItemBindingModel model)
{
try
{
_itemLogic.Create(model);
}
catch (Exception ex)
{
ViewBag.Exception = ex.Message;
return View();
}
return Redirect("~/Home/Items");
}
/// <summary>
/// Вывод ошибок /// Вывод ошибок
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>

View File

@ -13,6 +13,8 @@ builder.Services.AddTransient<IWorkLogic, WorkLogic>();
builder.Services.AddTransient<IWorkStorage, WorkStorage>(); builder.Services.AddTransient<IWorkStorage, WorkStorage>();
builder.Services.AddTransient<IWorkerLogic, WorkerLogic>(); builder.Services.AddTransient<IWorkerLogic, WorkerLogic>();
builder.Services.AddTransient<IWorkerStorage, WorkerStorage>(); builder.Services.AddTransient<IWorkerStorage, WorkerStorage>();
builder.Services.AddTransient<IItemLogic, ItemLogic>();
builder.Services.AddTransient<IItemStorage, ItemStorage>();
var app = builder.Build(); var app = builder.Build();

View File

@ -0,0 +1,23 @@
@{
ViewData["Title"] = "Создание запчасти";
}
<form method="post">
<div hidden><input type="text" name="Id" /></div>
<div hidden><input type="text" name="WorkerId" value="@CurrentUser.UserId" /></div>
<div class="row">
<div class="col-4">Название:</div>
<div class="col-8"><input type="text" name="Name" /></div>
</div>
<div class="row">
<div class="col-4">Цена:</div>
<div class="col-8"><input type="text" name="Price" /></div>
</div>
<div class="row">
<div class="col-4">Количество:</div>
<div class="col-8"><input type="text" name="Count" /></div>
</div>
<div class="row">
<div><button asp-controller="Home" asp-action="CreateItem" class="btn btn-primary">Добавить</button></div>
</div>
</form>
<div>@ViewBag.Exception</div>

View File

@ -0,0 +1,28 @@
@{
ViewData["Title"] = "Редактирование запчасти";
}
<form method="post">
<div hidden><input type="text" name="Id" value="@ViewBag.Item.Id" /></div>
<div hidden><input type="text" name="WorkerId" value="@ViewBag.Item.WorkerId" /></div>
<div class="row">
<div class="col-4">Название:</div>
<div class="col-8"><input type="text" name="Name" value="@ViewBag.Item.Name" /></div>
</div>
<div class="row">
<div class="col-4">Цена:</div>
<div class="col-8"><input type="text" name="Price" value="@ViewBag.Item.Price" /></div>
</div>
<div class="row">
<div class="col-4">Количество:</div>
<div class="col-8"><input type="text" name="Count" value="@ViewBag.Item.Count" /></div>
</div>
<div class="row">
<div><button asp-controller="Home" asp-action="UpdateItem" class="btn btn-primary">Изменить</button></div>
</div>
<div class="row">
<div><button asp-controller="Home" asp-action="DeleteItem" class="btn btn-primary">Удалить</button></div>
</div>
<div class="row">
<div><button asp-controller="Home" asp-action="Items" type="submit" class="btn btn-primary">Отмена</button></div>
</div>
</form>

View File

@ -0,0 +1,34 @@
@{
ViewData["Title"] = "Запчасти";
}
<div class="text-center">
<h1 class="display-4">Запчасти</h1>
@if (ViewBag.Items.Count != 0)
{
<center>
<table>
<thead>
<tr>
<th>Название</th>
<th>Цена (в рублях)</th>
<th>Количество на складе</th>
</tr>
</thead>
<tbody>
@foreach (var item in ViewBag.Items)
{
<tr>
<td>@item.Name</td>
<td>@item.Price</td>
<td>@item.Count</td>
<td><a href="/Home/Item/@item.Id">Изменить</a></td>
</tr>
}
</tbody>
</table>
</center>
}
<div><center><a asp-controller="Home" asp-action="CreateItem" class="btn btn-primary">Добавить</a></center></div>
<div>@ViewBag.Exception</div>
</div>

View File

@ -26,7 +26,7 @@
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Заявки</a> <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Заявки</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Статьи затрат</a> <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Items">Запчасти</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Отчет</a> <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Отчет</a>