From b984fdfb052f2868f08c82856fb7de2005e4709e Mon Sep 17 00:00:00 2001 From: dasha Date: Tue, 16 May 2023 15:41:04 +0400 Subject: [PATCH] table select index && create --- .../Controllers/HomeController.cs | 12 +- .../Controllers/StorekeeperController.cs | 193 +++++++++++++++++- HardwareShop/HardwareShopClientApp/Program.cs | 3 +- .../Views/Shared/_LayoutStorekeeper.cshtml | 1 + .../Views/Storekeeper/Components.cshtml | 65 +++--- .../Views/Storekeeper/CreateComponent.cshtml | 2 +- .../Views/Storekeeper/CreateGood.cshtml | 97 +++++++-- .../Views/Storekeeper/CreateOrder.cshtml | 34 ++- .../Views/Storekeeper/Goods.cshtml | 14 ++ .../Views/Storekeeper/Orders.cshtml | 125 +++++++++++- .../HardwareShopClientApp/appsettings.json | 2 +- .../BindingModels/GoodBindingModel.cs | 15 +- .../Controllers/ComponentController.cs | 93 +++++++++ .../Controllers/GoodController.cs | 98 +++++++++ .../Controllers/OrderController.cs | 96 +++++++++ .../Controllers/UserController.cs | 4 +- 16 files changed, 790 insertions(+), 64 deletions(-) create mode 100644 HardwareShop/HardwareShopRestApi/Controllers/ComponentController.cs create mode 100644 HardwareShop/HardwareShopRestApi/Controllers/GoodController.cs create mode 100644 HardwareShop/HardwareShopRestApi/Controllers/OrderController.cs diff --git a/HardwareShop/HardwareShopClientApp/Controllers/HomeController.cs b/HardwareShop/HardwareShopClientApp/Controllers/HomeController.cs index e68e31c..85718b3 100644 --- a/HardwareShop/HardwareShopClientApp/Controllers/HomeController.cs +++ b/HardwareShop/HardwareShopClientApp/Controllers/HomeController.cs @@ -22,18 +22,18 @@ namespace HardwareShopStorekeeperApp.Controllers } [HttpPost] - public void Register(string login, string email, string password, int role) + public void Register(string login, string email, string password) { - if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password) || role <= 0) + if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { - throw new Exception("Введите логин, email, пароль и роль"); + throw new Exception("Введите логин, email, пароль"); } - APIClient.PostRequest("api/client/register", new UserBindingModel + APIClient.PostRequest("api/user/register", new UserBindingModel { Login = login, Email = email, Password = password, - Role = (UserRole)role + Role = UserRole.Кладовщик }); Response.Redirect("Enter"); return; @@ -67,7 +67,7 @@ namespace HardwareShopStorekeeperApp.Controllers { throw new Exception("Введите почту и пароль"); } - APIClient.User = APIClient.GetRequest($"api/client/login?email={email}&password={password}"); + APIClient.User = APIClient.GetRequest($"api/user/login?email={email}&password={password}"); if (APIClient.User == null) { throw new Exception("Неверные почта и/или пароль"); diff --git a/HardwareShop/HardwareShopClientApp/Controllers/StorekeeperController.cs b/HardwareShop/HardwareShopClientApp/Controllers/StorekeeperController.cs index f4d5314..0df64b1 100644 --- a/HardwareShop/HardwareShopClientApp/Controllers/StorekeeperController.cs +++ b/HardwareShop/HardwareShopClientApp/Controllers/StorekeeperController.cs @@ -1,4 +1,7 @@ -using Microsoft.AspNetCore.Mvc; +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.ViewModels; +using HardwareShopDataModels.Enums; +using Microsoft.AspNetCore.Mvc; namespace HardwareShopStorekeeperApp.Controllers { @@ -12,51 +15,231 @@ namespace HardwareShopStorekeeperApp.Controllers } public IActionResult CreateOrder() { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Goods = APIClient.GetRequest>($"api/good/getgoods?userId={APIClient.User.Id}"); return View(); } + [HttpPost] + public void CreateOrder(int good, int count, string sum) + { + if (APIClient.User == null) + { + throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); + } + if (good <= 0) + { + throw new Exception("Некорректный идентификатор товара"); + } + if (count <= 0) + { + throw new Exception("Количество должно быть больше 0"); + } + if (Convert.ToDouble(sum.Replace('.', ',')) <= 0) + { + throw new Exception("Цена должна быть больше 0"); + } + APIClient.PostRequest("api/order/createorder", new OrderBindingModel + { + UserId = APIClient.User.Id, + GoodId = good, + Count = count, + Sum = Convert.ToDouble(sum.Replace('.', ',')) + }); + Response.Redirect("Orders"); + } + + + [HttpPost] + public void DeleteOrder(int Id) + { + if (APIClient.User == null) + { + throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); + } + if (Id <= 0) + { + throw new Exception("Некорректный идентификатор"); + } + APIClient.PostRequest("api/order/deleteorder", new OrderBindingModel + { + Id = Id + }); + Response.Redirect("Orders"); + } + + [HttpPost] + public void UpdateOrder(int id, int status) + { + if (APIClient.User == null) + { + throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); + } + if (id <= 0) + { + throw new Exception("Некорректный идентификатор"); + } + if (status <= 0) + { + throw new Exception("Некорректный статус"); + } + APIClient.PostRequest("api/order/updatedata", new OrderBindingModel + { + Id = id, + Status = (OrderStatus)status + }); + Response.Redirect("Orders"); + } + + [HttpPost] + public double Calc(int count, int good) + { + var prod = APIClient.GetRequest($"api/good/getgood?id={good}"); + double result = Math.Round(count * (prod?.Price ?? 1), 2); + return result; + } + public IActionResult CreateGood() { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Components = APIClient.GetRequest>($"api/component/getcomponents?userId={APIClient.User.Id}"); return View(); } - + + [HttpPost] + public void CreateGood([FromBody]GoodBindingModel goodModel) + { + if (APIClient.User == null) + { + throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); + } + if (string.IsNullOrEmpty(goodModel.GoodName)) + { + throw new Exception("Название не должно быть пустым"); + } + if (goodModel.Price <= 0) + { + throw new Exception("Цена должна быть больше 0"); + } + goodModel.UserId = APIClient.User.Id; + APIClient.PostRequest("api/good/creategood", goodModel); + } + public IActionResult LinkBuilds() { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } return View(); } public IActionResult CreateComponent() { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } return View(); } + [HttpPost] + public void CreateComponent(string name, string cost, DateTime date) + { + if (APIClient.User == null) + { + throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); + } + if (string.IsNullOrEmpty(name)) + { + throw new Exception("Название не должно быть пустым"); + } + if (string.IsNullOrEmpty(cost) || Convert.ToDouble(cost.Replace('.', ',')) <= 0) + { + throw new Exception("Цена должна быть больше 0"); + } + APIClient.PostRequest("api/component/createcomponent", new ComponentBindingModel + { + UserId = APIClient.User.Id, + ComponentName = name, + Cost = Convert.ToDouble(cost.Replace('.', ',')), + DateCreate = date + }); + Response.Redirect("Components"); + } + + [HttpGet] + public ComponentViewModel? GetComponent(int Id) + { + if (APIClient.User == null) + { + throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); + } + var result = APIClient.GetRequest($"api/component/getcomponent?id={Id}"); + if (result == null) + { + return default; + } + return result; + } + public IActionResult MainStorekeeper() { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } return View(); } public IActionResult Components() { - return View(); + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.GetRequest>($"api/component/getcomponents?userId={APIClient.User.Id}")); } public IActionResult Goods() { - return View(); + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.GetRequest>($"api/good/getgoods?userId={APIClient.User.Id}")); } public IActionResult Orders() { - return View(); + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.GetRequest>($"api/order/getorders?userId={APIClient.User.Id}")); } public IActionResult ListBuilds() { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } return View(); } public IActionResult Report() { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } return View(); } } diff --git a/HardwareShop/HardwareShopClientApp/Program.cs b/HardwareShop/HardwareShopClientApp/Program.cs index dde8734..48f88a0 100644 --- a/HardwareShop/HardwareShopClientApp/Program.cs +++ b/HardwareShop/HardwareShopClientApp/Program.cs @@ -7,6 +7,7 @@ builder.Services.AddControllersWithViews(); var app = builder.Build(); APIClient.Connect(builder.Configuration); + // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { @@ -26,4 +27,4 @@ app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/HardwareShop/HardwareShopClientApp/Views/Shared/_LayoutStorekeeper.cshtml b/HardwareShop/HardwareShopClientApp/Views/Shared/_LayoutStorekeeper.cshtml index a39a75e..61bc7a2 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Shared/_LayoutStorekeeper.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Shared/_LayoutStorekeeper.cshtml @@ -7,6 +7,7 @@ + @await RenderSectionAsync("Styles", required: false)
diff --git a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/Components.cshtml b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/Components.cshtml index 7fc0af3..c15c31b 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/Components.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/Components.cshtml @@ -1,4 +1,7 @@ @using HardwareShopContracts.ViewModels + +@model List + @{ ViewData["Title"] = "Комплектующие"; Layout = "~/Views/Shared/_LayoutStorekeeper.cshtml"; @@ -10,29 +13,45 @@
-

- Создать комплектующее - - - Привязка сборок -

- - - - - - - - - - -
- Название - - Цена - - Дата приобретения -
+ @{ +

+ Создать комплектующее + + + Привязка сборок +

+ + + + + + + + + + @foreach (var item in Model) + { + + + + + + } + +
+ Название + + Цена + + Дата приобретения +
+ @Html.DisplayFor(modelItem => item.ComponentName) + + @Html.DisplayFor(modelItem => item.Cost) + + @Html.DisplayFor(modelItem => item.DateCreate) +
+ }
diff --git a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/CreateComponent.cshtml b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/CreateComponent.cshtml index af2339b..1bd6a7d 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/CreateComponent.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/CreateComponent.cshtml @@ -10,7 +10,7 @@
- +
diff --git a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/CreateGood.cshtml b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/CreateGood.cshtml index a52186f..7ac7938 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/CreateGood.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/CreateGood.cshtml @@ -1,4 +1,5 @@ -@{ +@using HardwareShopContracts.ViewModels +@{ ViewData["Title"] = "Создание товара"; Layout = "~/Views/Shared/_LayoutStorekeeper.cshtml"; } @@ -7,36 +8,35 @@

Создание товара

- +
- +
- +

Комплектующие

-
- - - + + + - +
КомплектующееКоличествоСуммаКомплектующееКоличествоСумма
- +
- +
+ +@section Scripts +{ + +} \ No newline at end of file diff --git a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/CreateOrder.cshtml b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/CreateOrder.cshtml index 32dc90c..b5de9d9 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/CreateOrder.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/CreateOrder.cshtml @@ -10,17 +10,45 @@
- +
- +
- +
+ +@section Scripts + { + +} diff --git a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/Goods.cshtml b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/Goods.cshtml index 1deeb96..83b8a94 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/Goods.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/Goods.cshtml @@ -1,4 +1,7 @@ @using HardwareShopContracts.ViewModels + +@model List + @{ ViewData["Title"] = "Товары"; Layout = "~/Views/Shared/_LayoutStorekeeper.cshtml"; @@ -26,6 +29,17 @@ + @foreach (var item in Model) + { + + + @Html.DisplayFor(modelItem => item.GoodName) + + + @Html.DisplayFor(modelItem => item.Price) + + + } \ No newline at end of file diff --git a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/Orders.cshtml b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/Orders.cshtml index b206d4a..ea3d008 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Storekeeper/Orders.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Storekeeper/Orders.cshtml @@ -1,4 +1,7 @@ @using HardwareShopContracts.ViewModels + +@model List + @{ ViewData["Title"] = "Заказы"; Layout = "~/Views/Shared/_LayoutStorekeeper.cshtml"; @@ -11,13 +14,12 @@

Создать заказ - - - - - + + + +

- +
+ @foreach (var item in Model) + { + + + + + + + + + + }
@@ -44,6 +46,115 @@
+ @Html.DisplayFor(modelItem => item.Id) + + @Html.DisplayFor(modelItem => item.GoodName) + + @Html.DisplayFor(modelItem => item.Count) + + @Html.DisplayFor(modelItem => item.Sum) + + @Html.DisplayFor(modelItem => item.Status) + + @Html.DisplayFor(modelItem => item.DateCreate) + + @Html.DisplayFor(modelItem => item.DateImplement) +
-
\ No newline at end of file + + +@section Styles +{ + +} + +@section Scripts +{ + +} \ No newline at end of file diff --git a/HardwareShop/HardwareShopClientApp/appsettings.json b/HardwareShop/HardwareShopClientApp/appsettings.json index a9607e1..8685b00 100644 --- a/HardwareShop/HardwareShopClientApp/appsettings.json +++ b/HardwareShop/HardwareShopClientApp/appsettings.json @@ -7,5 +7,5 @@ }, "AllowedHosts": "*", - "IPAddress": "http://localhost:5205/" + "IPAddress": "http://localhost:5254/" } diff --git a/HardwareShop/HardwareShopContracts/BindingModels/GoodBindingModel.cs b/HardwareShop/HardwareShopContracts/BindingModels/GoodBindingModel.cs index 553d3ee..a80ef36 100644 --- a/HardwareShop/HardwareShopContracts/BindingModels/GoodBindingModel.cs +++ b/HardwareShop/HardwareShopContracts/BindingModels/GoodBindingModel.cs @@ -1,4 +1,5 @@ -using HardwareShopDataModels.Models; +using HardwareShopContracts.ViewModels; +using HardwareShopDataModels.Models; namespace HardwareShopContracts.BindingModels { @@ -17,5 +18,17 @@ namespace HardwareShopContracts.BindingModels get; set; } = new(); + + public List GoodComponentsComponents + { + get; + set; + } = new(); + + public List GoodComponentsCounts + { + get; + set; + } = new(); } } diff --git a/HardwareShop/HardwareShopRestApi/Controllers/ComponentController.cs b/HardwareShop/HardwareShopRestApi/Controllers/ComponentController.cs new file mode 100644 index 0000000..c36961e --- /dev/null +++ b/HardwareShop/HardwareShopRestApi/Controllers/ComponentController.cs @@ -0,0 +1,93 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.BuisnessLogicsContracts; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace HardwareShopRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ComponentController : Controller + { + private readonly ILogger _logger; + + private readonly IComponentLogic _component; + + public ComponentController(ILogger logger, IComponentLogic component) + { + _logger = logger; + _component = component; + } + + [HttpGet] + public List? GetComponents(int userId) + { + try + { + return _component.ReadList(new() { UserId = userId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка комплектующих"); + throw; + } + } + + [HttpGet] + public ComponentViewModel? GetComponent(int id) + { + try + { + return _component.ReadElement(new() { Id = id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения комплектующего"); + throw; + } + } + + [HttpPost] + public void CreateComponent(ComponentBindingModel model) + { + try + { + _component.Create(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания комплектующего"); + throw; + } + } + + [HttpPost] + public void UpdateData(ComponentBindingModel model) + { + try + { + _component.Update(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка обновления данных комплектующего"); + throw; + } + } + + [HttpPost] + public void DeleteComponent(ComponentBindingModel model) + { + try + { + _component.Delete(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления комплектующего"); + throw; + } + } + } +} diff --git a/HardwareShop/HardwareShopRestApi/Controllers/GoodController.cs b/HardwareShop/HardwareShopRestApi/Controllers/GoodController.cs new file mode 100644 index 0000000..10dfa86 --- /dev/null +++ b/HardwareShop/HardwareShopRestApi/Controllers/GoodController.cs @@ -0,0 +1,98 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.BuisnessLogicsContracts; +using HardwareShopContracts.ViewModels; +using HardwareShopDataModels.Models; +using Microsoft.AspNetCore.Mvc; + +namespace HardwareShopRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class GoodController : Controller + { + private readonly ILogger _logger; + + private readonly IGoodLogic _good; + + public GoodController(ILogger logger, IGoodLogic good) + { + _logger = logger; + _good = good; + } + + [HttpGet] + public List? GetGoods(int userId) + { + try + { + return _good.ReadList(new() { UserId = userId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка товаров"); + throw; + } + } + + [HttpGet] + public GoodViewModel? GetGood(int id) + { + try + { + return _good.ReadElement(new() { Id = id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения товара"); + throw; + } + } + + [HttpPost] + public void CreateGood(GoodBindingModel model) + { + try + { + for (int i = 0; i < model.GoodComponentsCounts.Count; i++) + { + model.GoodComponents.Add(model.GoodComponentsComponents[i].Id, + (model.GoodComponentsComponents[i] as IComponentModel, model.GoodComponentsCounts[i])); + } + _good.Create(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания товара"); + throw; + } + } + + [HttpPost] + public void UpdateData(GoodBindingModel model) + { + try + { + _good.Update(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка обновления данных товара"); + throw; + } + } + + [HttpPost] + public void DeleteGood(GoodBindingModel model) + { + try + { + _good.Delete(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления товара"); + throw; + } + } + } +} diff --git a/HardwareShop/HardwareShopRestApi/Controllers/OrderController.cs b/HardwareShop/HardwareShopRestApi/Controllers/OrderController.cs new file mode 100644 index 0000000..ab38918 --- /dev/null +++ b/HardwareShop/HardwareShopRestApi/Controllers/OrderController.cs @@ -0,0 +1,96 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.BuisnessLogicsContracts; +using HardwareShopContracts.ViewModels; +using HardwareShopDatabaseImplement.Models.Storekeeper; +using HardwareShopDataModels.Enums; +using HardwareShopDataModels.Models; +using Microsoft.AspNetCore.Mvc; + +namespace HardwareShopRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class OrderController : Controller + { + private readonly ILogger _logger; + + private readonly IOrderLogic _order; + + public OrderController(ILogger logger, IOrderLogic order) + { + _logger = logger; + _order = order; + } + + [HttpGet] + public List? GetOrders(int userId) + { + try + { + return _order.ReadList(new() { UserId = userId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка заказов"); + throw; + } + } + + [HttpPost] + public void CreateOrder(OrderBindingModel model) + { + try + { + _order.CreateOrder(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания заказа"); + throw; + } + } + + [HttpPost] + public void UpdateData(OrderBindingModel model) + { + try + { + switch (model.Status) + { + case OrderStatus.Неизвестен: + break; + case OrderStatus.Принят: + break; + case OrderStatus.Выполняется: + _order.TakeOrderInWork(model); + break; + case OrderStatus.Готов: + _order.FinishOrder(model); + break; + case OrderStatus.Выдан: + _order.DeliveryOrder(model); + break; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка обновления данных заказа"); + throw; + } + } + + [HttpPost] + public void DeleteOrder(OrderBindingModel model) + { + try + { + _order.Delete(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления заказа"); + throw; + } + } + } +} diff --git a/HardwareShop/HardwareShopRestApi/Controllers/UserController.cs b/HardwareShop/HardwareShopRestApi/Controllers/UserController.cs index 0944eed..d430e4c 100644 --- a/HardwareShop/HardwareShopRestApi/Controllers/UserController.cs +++ b/HardwareShop/HardwareShopRestApi/Controllers/UserController.cs @@ -21,13 +21,13 @@ namespace HardwareShopRestApi.Controllers } [HttpGet] - public UserViewModel? Login(string login, string password) + public UserViewModel? Login(string email, string password) { try { return _logic.ReadElement(new UserSearchModel { - Email = login, + Email = email, Password = password }); }