From d748583b955063402b70a098d7abab93d0862a74 Mon Sep 17 00:00:00 2001 From: Sergey Kozyrev Date: Sat, 25 May 2024 21:36:21 +0400 Subject: [PATCH] CRUD base --- .../Controllers/HomeController.cs | 156 ++++++++++++------ Course/ImplementerApp/ImplementerData.cs | 34 ++++ .../Views/Home/CreateProduct.cshtml | 96 +++++++++-- .../Views/Home/CreateProduction.cshtml | 89 ++++++++-- 4 files changed, 304 insertions(+), 71 deletions(-) diff --git a/Course/ImplementerApp/Controllers/HomeController.cs b/Course/ImplementerApp/Controllers/HomeController.cs index 7eb8774..e8630a4 100644 --- a/Course/ImplementerApp/Controllers/HomeController.cs +++ b/Course/ImplementerApp/Controllers/HomeController.cs @@ -18,22 +18,23 @@ namespace ImplementerApp.Controllers _logger = logger; _data = data; } + private bool IsLoggedIn { get {return UserImplementer.user != null; } } public IActionResult IndexNonReg() { - if (UserImplementer.user == null) + if (!IsLoggedIn) return View(); return RedirectToAction("Index"); } public IActionResult Index() { - if (UserImplementer.user == null) + if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); return View(); } [HttpGet] public IActionResult Enter() { - if (UserImplementer.user == null) + if (!IsLoggedIn) return View(); return RedirectToAction("Index"); } @@ -112,30 +113,60 @@ namespace ImplementerApp.Controllers [HttpGet] public IActionResult IndexProduct() { - if (UserImplementer.user != null) { - var products = _data.GetProducts(UserImplementer.user.Id); + if (IsLoggedIn) { + var products = _data.GetProducts(UserImplementer.user!.Id); return View(products); } return RedirectToAction("IndexNonReg"); } - public IActionResult CreateProduct() + [HttpPost] + public IActionResult IndexProduct(int id) + { + _data.DeleteProduct(id); + return RedirectToAction("IndexProduct"); + } + [HttpGet] + public IActionResult CreateProduct(int id) { - var details = new List(); - details.Add(new DetailViewModel - { - Id = 1, - Name = "Test", - Cost = 55.5, - UserId = 1 - }); - details.Add(new DetailViewModel - { - Id = 2, - Name = "Test1", - Cost = 32, - UserId = 2 - }); - return View(details); + var details = _data.GetDetails(UserImplementer.user!.Id); + ViewBag.AllDetails = details; + if (id != 0) + { + var value = _data.GetProduct(id); + if (value != null) + return View(value); + } + return View(new ProductViewModel()); + } + [HttpPost] + public IActionResult CreateProduct(int id, string title, int[] detailIds, int[] counts) + { + ProductBindingModel model = new ProductBindingModel(); + model.Id = id; + model.Name = title; + model.UserId = UserImplementer.user!.Id; + var details = _data.GetDetails(UserImplementer.user!.Id); + double sum = 0; + for (int i = 0; i < detailIds.Length; i++) + { + var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!; + if (counts[i] <= 0) + continue; + model.ProductDetails[detailIds[i]] = (detail, counts[i]); + sum += detail.Cost * counts[i]; + } + if (model.ProductDetails.Count == 0) + return RedirectToAction("IndexProduct"); + model.Cost = sum; + if (id != 0) + { + _data.UpdateProduct(model); + } + else + { + _data.CreateProduct(model); + } + return RedirectToAction("IndexProduct"); } [HttpGet] public IActionResult IndexProduction() @@ -147,37 +178,66 @@ namespace ImplementerApp.Controllers } return RedirectToAction("IndexNonReg"); } - public IActionResult CreateProduction() + [HttpPost] + public IActionResult IndexProduction(int id) + { + _data.DeleteProduction(id); + return RedirectToAction("IndexProduction"); + } + [HttpGet] + public IActionResult CreateProduction(int id) { - var details = new List(); - details.Add(new DetailViewModel + var details = _data.GetDetails(UserImplementer.user!.Id); + ViewBag.AllDetails = details; + if (id != 0) + { + var value = _data.GetProduction(id); + if (value != null) + return View(value); + } + return View(new ProductionViewModel()); + } + [HttpPost] + public IActionResult CreateProduction(int id, string title, int[] detailIds) + { + ProductionBindingModel model = new ProductionBindingModel(); + model.Id = id; + model.Name = title; + model.UserId = UserImplementer.user!.Id; + var details = _data.GetDetails(UserImplementer.user!.Id); + double sum = 0; + for (int i = 0; i < detailIds.Length; i++) { - Id = 1, - Name = "Test", - Cost = 55.5, - UserId = 1 - }); - details.Add(new DetailViewModel - { - Id = 2, - Name = "Test1", - Cost = 32, - UserId = 2 - }); - return View(details); + var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!; + model.ProductionDetails[detailIds[i]] = detail; + sum += detail.Cost; + } + model.Cost = sum; + bool changed = false; + if (model.ProductionDetails.Count > 0) + { + if (id != 0) + { + changed = _data.UpdateProduction(model); + } + else + { + changed = _data.CreateProduction(model); + } + } + if (changed) + return RedirectToAction("IndexProduction"); + else + { + ViewBag.AllDetails = details; + return View(model); + } } public IActionResult Privacy() { - ImplementerViewModel user = new() - { - Email = "mail@mail.ru", - Login = "Login", - Password = "password", - Name = "User" - - }; - - return View(user); + if (IsLoggedIn) + return View(UserImplementer.user); + return RedirectToAction("IndexNonReg"); } public IActionResult DetailTimeReport() { diff --git a/Course/ImplementerApp/ImplementerData.cs b/Course/ImplementerApp/ImplementerData.cs index 9a5b14c..a27ec5f 100644 --- a/Course/ImplementerApp/ImplementerData.cs +++ b/Course/ImplementerApp/ImplementerData.cs @@ -69,9 +69,43 @@ namespace ImplementerApp return _productLogic.ReadList(new ProductSearchModel() { UserId = userId }); } + public ProductViewModel? GetProduct(int id) + { + return _productLogic.ReadElement(new() { Id = id }); + } + + public bool UpdateProduct(ProductBindingModel model) + { + return _productLogic.Update(model); + } + public bool DeleteProduct(int productId) + { + return _productLogic.Delete(new() { Id = productId }); + } + public bool CreateProduct(ProductBindingModel model) + { + return _productLogic.Create(model); + } + public List? GetProductions(int userId) { return _productionLogic.ReadList(new() { UserId = userId }); } + public ProductionViewModel? GetProduction(int id) + { + return _productionLogic.ReadElement(new() { Id = id }); + } + public bool CreateProduction(ProductionBindingModel model) + { + return _productionLogic.Create(model); + } + public bool UpdateProduction(ProductionBindingModel model) + { + return _productionLogic.Update(model); + } + public bool DeleteProduction(int productionId) + { + return _productionLogic.Delete(new() { Id = productionId}); + } } } diff --git a/Course/ImplementerApp/Views/Home/CreateProduct.cshtml b/Course/ImplementerApp/Views/Home/CreateProduct.cshtml index aa11933..985149c 100644 --- a/Course/ImplementerApp/Views/Home/CreateProduct.cshtml +++ b/Course/ImplementerApp/Views/Home/CreateProduct.cshtml @@ -1,18 +1,19 @@ @using Contracts.ViewModels -@model List +@model ProductViewModel; @{ - ViewData["Title"] = "CreateProduct"; + ViewData["Title"] = "CreateProduct"; + ViewBag.Details = Model.DetailProducts; }
-

Создание изделия

+

Создание изделия

-
-
Название:
-
-
+
+
Название:
+
+
Детали
@@ -21,24 +22,38 @@ Название Количество + Стоимость Удалить - @foreach (var detail in Model) + @foreach (var detail in ViewBag.Details) { - - @detail.Name + - + + @detail.Value.Item1.Name + + + @detail.Value.Item1.Cost + + }
+ +
Сумма:
@@ -54,6 +69,61 @@ \ No newline at end of file + diff --git a/Course/ImplementerApp/Views/Home/CreateProduction.cshtml b/Course/ImplementerApp/Views/Home/CreateProduction.cshtml index de8b108..01e5847 100644 --- a/Course/ImplementerApp/Views/Home/CreateProduction.cshtml +++ b/Course/ImplementerApp/Views/Home/CreateProduction.cshtml @@ -1,9 +1,10 @@ @using Contracts.ViewModels -@model List +@model ProductionViewModel @{ ViewData["Title"] = "CreateProduction"; + ViewBag.Details = Model.DetailProductions; }

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

@@ -11,31 +12,42 @@
Название:
-
+
-
Details
+
Детали
- + + - @foreach (var detail in Model) + @foreach (var detail in ViewBag.Details) { - + - + + }
Выбор НазваниеСтоимостьУдалить
- + + @detail.Value.Name @detail.Name@detail.Value.Cost
+ +
Сумма:
@@ -51,6 +63,63 @@ \ No newline at end of file +