This commit is contained in:
Николай 2023-05-16 17:54:45 +04:00
parent 4684d4665b
commit 0f35bf9e19
10 changed files with 402 additions and 91 deletions

View File

@ -1,5 +1,5 @@
using HardwareShopContracts.BindingModels; using HardwareShopContracts.BindingModels;
using HardwareShopContracts.BuisnessLogicsContracts; using HardwareShopContracts.BusinessLogicsContracts;
using HardwareShopContracts.SearchModels; using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts; using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels; using HardwareShopContracts.ViewModels;

View File

@ -119,6 +119,7 @@
resultTable.innerHTML = '' resultTable.innerHTML = ''
let price = 0; let price = 0;
list.forEach((elem) => { list.forEach((elem) => {
console.log(elem);
resultTable.innerHTML += `<tr><td>${elem.component.componentName}</td><td>${elem.count}</td><td>${Math.round(elem.component.cost * elem.count * 100) / 100}</td></tr>` resultTable.innerHTML += `<tr><td>${elem.component.componentName}</td><td>${elem.count}</td><td>${Math.round(elem.component.cost * elem.count * 100) / 100}</td></tr>`
price += elem.component.cost * elem.count price += elem.component.cost * elem.count
}) })

View File

@ -1,4 +1,5 @@
using HardwareShopDataModels.Enums; using HardwareShopContracts.ViewModels;
using HardwareShopDataModels.Enums;
using HardwareShopDataModels.Models; using HardwareShopDataModels.Models;
namespace HardwareShopContracts.BindingModels namespace HardwareShopContracts.BindingModels
@ -18,5 +19,17 @@ namespace HardwareShopContracts.BindingModels
public Dictionary<int, (IGoodModel, int)> PurchaseGoods { get; set; } = new(); public Dictionary<int, (IGoodModel, int)> PurchaseGoods { get; set; } = new();
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; set; } = new(); public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; set; } = new();
public List<GoodViewModel> ListPurchaseGoods
{
get;
set;
} = new();
public List<int> PurchaseGoodsCounts
{
get;
set;
} = new();
} }
} }

View File

@ -2,7 +2,7 @@
using HardwareShopContracts.SearchModels; using HardwareShopContracts.SearchModels;
using HardwareShopContracts.ViewModels; using HardwareShopContracts.ViewModels;
namespace HardwareShopContracts.BuisnessLogicsContracts namespace HardwareShopContracts.BusinessLogicsContracts
{ {
public interface IGoodLogic public interface IGoodLogic
{ {

View File

@ -1,5 +1,5 @@
using HardwareShopContracts.BindingModels; using HardwareShopContracts.BindingModels;
using HardwareShopContracts.BuisnessLogicsContracts; using HardwareShopContracts.BusinessLogicsContracts;
using HardwareShopContracts.ViewModels; using HardwareShopContracts.ViewModels;
using HardwareShopDataModels.Models; using HardwareShopDataModels.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -20,6 +20,20 @@ namespace HardwareShopRestApi.Controllers
_good = good; _good = good;
} }
[HttpGet]
public List<GoodViewModel>? GetAllGoods()
{
try
{
return _good.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка всех товаров");
throw;
}
}
[HttpGet] [HttpGet]
public List<GoodViewModel>? GetGoods(int userId) public List<GoodViewModel>? GetGoods(int userId)
{ {

View File

@ -2,6 +2,8 @@
using HardwareShopContracts.BusinessLogicsContracts; using HardwareShopContracts.BusinessLogicsContracts;
using HardwareShopContracts.SearchModels; using HardwareShopContracts.SearchModels;
using HardwareShopContracts.ViewModels; using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models.Storekeeper;
using HardwareShopDataModels.Models;
using HardwareShopRestApi.Controllers; using HardwareShopRestApi.Controllers;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Xml.Linq; using System.Xml.Linq;
@ -16,10 +18,88 @@ namespace HardwareShopRestApi.Controllers
private readonly IPurchaseLogic _purchaseLogic; private readonly IPurchaseLogic _purchaseLogic;
public PurchaseController(IPurchaseLogic purchaseLogic, ILogger<UserController> logger) private readonly IGoodLogic _goodLogic;
public PurchaseController(IPurchaseLogic purchaseLogic, IGoodLogic goodLogic, ILogger<UserController> logger)
{ {
_logger = logger; _logger = logger;
_purchaseLogic = purchaseLogic; _purchaseLogic = purchaseLogic;
_goodLogic = goodLogic;
}
[HttpGet]
public List<PurchaseViewModel>? GetPurchases(int userId)
{
try
{
return _purchaseLogic.ReadList(new() { UserId = userId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка товаров");
throw;
}
}
[HttpGet]
public PurchaseViewModel? GetPurchase(int id)
{
try
{
return _purchaseLogic.ReadElement(new() { Id = id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения товара");
throw;
}
}
[HttpPost]
public void CreatePurchase(PurchaseBindingModel model)
{
try
{
for (int i = 0; i < model.PurchaseGoodsCounts.Count; i++)
{
model.PurchaseGoods.Add(model.ListPurchaseGoods[i].Id,
(model.ListPurchaseGoods[i] as IGoodModel, model.PurchaseGoodsCounts[i]));
}
_purchaseLogic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания товара");
throw;
}
}
[HttpPost]
public void UpdatePurchase(PurchaseBindingModel model)
{
try
{
_purchaseLogic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных товара");
throw;
}
}
[HttpPost]
public void DeletePurchase(PurchaseBindingModel model)
{
try
{
_purchaseLogic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления товара");
throw;
}
} }
} }
} }

View File

@ -270,22 +270,61 @@ namespace HardwareShopWorkerApp.Controllers
[HttpGet] [HttpGet]
public IActionResult Purchases() public IActionResult Purchases()
{ {
return View(); if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<PurchaseViewModel>>($"api/purchase/getpurchases?userId={APIClient.User.Id}"));
} }
[HttpPost] [HttpPost]
public void Purchases(int id) public void Purchases(int id)
{ {
Response.Redirect("Purchase"); Response.Redirect("CreatePurchase");
} }
[HttpGet]
public GoodViewModel? GetGood(int Id)
{
if (APIClient.User == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
var result = APIClient.GetRequest<GoodViewModel?>($"api/good/getgood?id={Id}");
if (result == null)
{
return default;
}
return result;
}
[HttpGet] [HttpGet]
public IActionResult Purchase() public IActionResult CreatePurchase()
{ {
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Goods = APIClient.GetRequest<List<GoodViewModel>>($"api/good/GetAllGoods");
return View(); return View();
} }
[HttpPost]
public void CreatePurchase([FromBody] PurchaseBindingModel purchaseModel)
{
if (APIClient.User == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (purchaseModel.Sum <= 0)
{
throw new Exception("Цена должна быть больше 0");
}
purchaseModel.UserId = APIClient.User.Id;
APIClient.PostRequest("api/purchase/createpurchase", purchaseModel);
}
[HttpGet]
public IActionResult WorkerReport() public IActionResult WorkerReport()
{ {
return View(); return View();

View File

@ -0,0 +1,193 @@
@using HardwareShopContracts.ViewModels
@{
ViewData["Title"] = "CreatePurchase";
Layout = "~/Views/Shared/_LayoutWorker.cshtml";
}
<div class="text-center">
<h2 class="display-4">Создание покупки</h2>
</div>
<div class="d-flex flex-column align-items-center">
<div class="col-sm-3">
<label class="form-label">Цена</label>
<input type="number" step="0.01" class="form-control" name="sum" id="sum" readonly min="0.01" value="0" required>
</div>
<h1 class="display-6">Товары</h1>
<div class="d-flex justify-content-center">
<button type="button" class="btn btn-primary mx-2 mt-3" data-bs-toggle="modal" data-bs-target="#exampleModal">Добавить</button>
<button type="button" class="btn btn-primary mx-2 mt-3">Удалить</button>
</div>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Товар</th>
<th scope="col">Количество</th>
<th scope="col">Цена</th>
</tr>
</thead>
<tbody id="result">
</tbody>
</table>
<div class="col-sm-2 d-flex justify-content-evenly align-items-baseline">
<button type="button" class="btn btn-primary mt-3 px-4" id="createpurchase">Сохранить</button>
</div>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Товар</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
</div>
<div class="modal-body">
<label class="form-label">Товар</label>
<select id="good" name="good" class="form-control" asp-items="@(new SelectList(@ViewBag.Goods, "Id", "GoodName"))"></select>
<label class="form-label">Количество</label>
<input type="number" class="form-control" name="count" id="count" min="1" value="1" required>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
<button type="button" class="btn btn-primary" data-bs-dismiss="modal" id="savegood">Сохранить</button>
</div>
</div>
</div>
</div>
@section Scripts
{
<script>
let list = [];
const submitGoodBtn = document.getElementById("savegood");
//const editBtn = document.getElementById("editcomponent");
const saveBtn = document.getElementById("createpurchase");
const countElem = document.getElementById("count");
const resultTable = document.getElementById("result");
const totalSum = document.getElementById("sum");
submitGoodBtn.addEventListener("click", () => {
console.log('try to add good')
var count = $('#count').val();
var good = $('#good').val();
$.ajax({
method: "GET",
url: `/Home/GetGood`,
data: { Id: good },
success: function (result) {
let flag = false
if (list.length > 0) {
list.forEach((elem) => {
if (elem.good.id === parseInt(result.id)) {
console.log('good already added')
flag = true
}
})
}
if (!flag) list.push({ good: result, count: count })
reloadTable()
countElem.value = '1'
}
});
})
saveBtn.addEventListener("click", () => {
console.log('try to add purchase')
let goods = []
let counts = []
list.forEach((x) => {
goods.push(x.good);
counts.push(parseInt(x.count))
})
$.ajax(
{
url: `/Home/CreatePurchase`,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
"Sum": parseFloat(totalSum.value),
"ListPurchaseGoods": goods
})
}
).done(() => window.location.href = '/Home/Purchases')
})
function reloadTable() {
resultTable.innerHTML = ''
let price = 0;
list.forEach((elem) => {
console.log(elem);
resultTable.innerHTML += `<tr><td>${elem.good.goodName}</td><td>${elem.count}</td><td>${Math.round(elem.good.price * elem.count * 100) / 100}</td></tr>`
price += elem.good.price * elem.count
})
totalSum.value = Math.round(price * 100) / 100
}
</script>
}
@*<form class="d-flex justify-content-evenly">
<div class=" col-sm-8">
<div class="text-center">
<h2 class="display-4">Товары</h2>
</div>
<div class="text-center" name="id">
<table class="table">
<thead>
<tr>
<th>
Номер
</th>
<th>
Название товара
</th>
<th>
Цена
</th>
<th>
Количество
</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<div class="text-center d-flex flex-column mt-5">
<button type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#exampleModal">Добавить</button>
<button type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#exampleModal">Изменить</button>
<button type="button" class="btn btn-primary btn-lg mb-5">Удалить</button>
<button type="button" class="btn btn-primary btn-lg mb-5">Обновить</button>
<button type="button" class="btn btn-primary btn-lg mb-5">Сохранить</button>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Товар</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
</div>
<div class="modal-body">
<div>
<label class="form-label">Товар</label>
<select class="form-select">
<option value="1">Товар 1</option>
<option value="2">Товар 2</option>
</select>
</div>
<div>
<label class="form-label">Количество</label>
<input type="number" class="form-control">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
<button type="button" class="btn btn-primary">Сохранить</button>
</div>
</div>
</div>
</div>
</form>
*@

View File

@ -1,68 +0,0 @@
@{
ViewData["Title"] = "Purchase";
Layout = "~/Views/Shared/_LayoutWorker.cshtml";
}
<form class="d-flex justify-content-evenly">
<div class=" col-sm-8">
<div class="text-center">
<h2 class="display-4">Товары</h2>
</div>
<div class="text-center" name="id">
<table class="table">
<thead>
<tr>
<th>
Номер
</th>
<th>
Название товара
</th>
<th>
Цена
</th>
<th>
Количество
</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<div class="text-center d-flex flex-column mt-5">
<button type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#exampleModal">Добавить</button>
<button type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#exampleModal">Изменить</button>
<button type="button" class="btn btn-primary btn-lg mb-5">Удалить</button>
<button type="button" class="btn btn-primary btn-lg mb-5">Обновить</button>
<button type="button" class="btn btn-primary btn-lg mb-5">Сохранить</button>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Товар</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
</div>
<div class="modal-body">
<div>
<label class="form-label">Товар</label>
<select class="form-select">
<option value="1">Товар 1</option>
<option value="2">Товар 2</option>
</select>
</div>
<div>
<label class="form-label">Количество</label>
<input type="number" class="form-control">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
<button type="button" class="btn btn-primary">Сохранить</button>
</div>
</div>
</div>
</div>
</form>

View File

@ -1,12 +1,27 @@
@{ @using HardwareShopContracts.ViewModels
@model List<PurchaseViewModel>
@{
ViewData["Title"] = "Purchases"; ViewData["Title"] = "Purchases";
Layout = "~/Views/Shared/_LayoutWorker.cshtml"; Layout = "~/Views/Shared/_LayoutWorker.cshtml";
} }
<form method="post" class="d-flex justify-content-evenly"> <div method="post" class="d-flex justify-content-evenly">
@{
if (Model == null)
{
<h3 class="display-4">Введите пароль</h3>
return;
}
<div class="col-sm-8"> <div class="col-sm-8">
<div class="text-center"> <div class="text-center">
<h2 class="display-4">Покупки</h2> <h2 class="display-4">Покупки</h2>
<p>
<a asp-controller="Home" asp-action="CreatePurchase" class="btn btn-primary btn-lg mb-5">Добавить</a>
<button type="button" class="btn btn-primary mx-2" id="delete">Удалить заказ</button>
<button type="button" class="btn btn-primary mx-2" id="inwork">Выполняется</button>
<button type="button" class="btn btn-primary mx-2" id="ready">Готов</button>
<button type="button" class="btn btn-primary mx-2" id="done">Выдан</button>
</p>
</div> </div>
<div class="text-center" name="id"> <div class="text-center" name="id">
<table class="table"> <table class="table">
@ -27,14 +42,38 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.DatePurchase)
</td>
<td>
@Html.DisplayFor(modelItem => item.Sum)
</td>
<td>
@Html.DisplayFor(modelItem => item.PurchaseStatus)
</td>
@*<td>
<div>
<button onclick="getBuild(@item.Id)" type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#updateModal">Изменить</button>
<button onclick="getBuild(@item.Id)" type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#deleteModal">Удалить</button>
</div>
</td>*@
</tr>
}
</tbody> </tbody>
</table> </table>
</div> </div>
</div> </div>
<div class="text-center d-flex flex-column mt-5"> <div class="text-center d-flex flex-column mt-5">
<button type="submit" class="btn btn-primary btn-lg mb-5">Добавить</button>
<button type="submit" class="btn btn-primary btn-lg mb-5">Изменить</button> @*<button type="submit" class="btn btn-primary btn-lg mb-5">Изменить</button>
<button type="button" class="btn btn-primary btn-lg mb-5">Удалить</button> <button type="button" class="btn btn-primary btn-lg mb-5">Удалить</button>
<button type="button" class="btn btn-primary btn-lg mb-5">Обновить</button> <button type="button" class="btn btn-primary btn-lg mb-5">Обновить</button>*@
</div>
}
</div> </div>
</form>