From 4fd4b0351fc45f26762fe878586290cfaa017ff9 Mon Sep 17 00:00:00 2001 From: Sergey Kozyrev Date: Thu, 23 May 2024 20:02:34 +0400 Subject: [PATCH] AllIndex + SomeImplementerData --- .../SearchModels/ImplementerSearchModel.cs | 1 + .../DatabaseImplement.csproj | 1 + .../Implements/DetailStorage.cs | 4 +- .../Implements/ImplementerStorage.cs | 2 +- .../Implements/ProductStorage.cs | 2 +- Course/ImplemenerLogic/DataToLogic.cs | 13 ++ Course/ImplemenerLogic/ImplemenerLogic.csproj | 14 ++ .../Controllers/HomeController.cs | 160 ++++++++++-------- Course/ImplementerApp/ImplementerApp.csproj | 2 + Course/ImplementerApp/ImplementerData.cs | 77 +++++++++ Course/ImplementerApp/Program.cs | 18 ++ Course/ImplementerApp/UserImplementer.cs | 9 + .../Views/Home/CreateDetail.cshtml | 11 +- .../Views/Home/CreateProduct.cshtml | 7 +- Course/ImplementerApp/Views/Home/Index.cshtml | 4 +- .../Views/Home/IndexDetail.cshtml | 7 +- .../Views/Home/IndexNonReg.cshtml | 10 ++ .../Views/Home/IndexProduct.cshtml | 8 +- .../Views/Home/IndexProduction.cshtml | 7 +- .../ImplementerApp/Views/Home/Privacy.cshtml | 1 + .../Views/Shared/_Layout.cshtml | 9 +- Course/ImplementerApp/log4net.config | 16 ++ 22 files changed, 292 insertions(+), 91 deletions(-) create mode 100644 Course/ImplemenerLogic/DataToLogic.cs create mode 100644 Course/ImplemenerLogic/ImplemenerLogic.csproj create mode 100644 Course/ImplementerApp/ImplementerData.cs create mode 100644 Course/ImplementerApp/UserImplementer.cs create mode 100644 Course/ImplementerApp/Views/Home/IndexNonReg.cshtml create mode 100644 Course/ImplementerApp/log4net.config diff --git a/Course/Contracts/SearchModels/ImplementerSearchModel.cs b/Course/Contracts/SearchModels/ImplementerSearchModel.cs index 4fba103..deb4f46 100644 --- a/Course/Contracts/SearchModels/ImplementerSearchModel.cs +++ b/Course/Contracts/SearchModels/ImplementerSearchModel.cs @@ -10,5 +10,6 @@ namespace Contracts.SearchModels { public int? Id { get; set; } public string? Login { get; set; } + public string? Password { get; set; } } } diff --git a/Course/DatabaseImplement/DatabaseImplement.csproj b/Course/DatabaseImplement/DatabaseImplement.csproj index b4206ce..dfc5a94 100644 --- a/Course/DatabaseImplement/DatabaseImplement.csproj +++ b/Course/DatabaseImplement/DatabaseImplement.csproj @@ -7,6 +7,7 @@ + diff --git a/Course/DatabaseImplement/Implements/DetailStorage.cs b/Course/DatabaseImplement/Implements/DetailStorage.cs index 2260591..27fd95b 100644 --- a/Course/DatabaseImplement/Implements/DetailStorage.cs +++ b/Course/DatabaseImplement/Implements/DetailStorage.cs @@ -22,7 +22,7 @@ namespace DatabaseImplement.Implements public DetailViewModel? GetElement(DetailSearchModel model) { using var context = new FactoryGoWorkDatabase(); - return context.Details.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Contains(model.Name)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Details.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Equals(model.Name)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(DetailSearchModel model) @@ -35,7 +35,7 @@ namespace DatabaseImplement.Implements if (model.DateFrom.HasValue) return context.Details.Where(x => x.UserId == model.Id).Where(x => x.DateCreate < model.DateTo && x.DateCreate > model.DateFrom).Select(x => x.GetViewModel).ToList(); else - return context.Details.Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList(); + return context.Details.Where(x => x.UserId == model.UserId).Select(x => x.GetViewModel).ToList(); } diff --git a/Course/DatabaseImplement/Implements/ImplementerStorage.cs b/Course/DatabaseImplement/Implements/ImplementerStorage.cs index c96278d..05db08c 100644 --- a/Course/DatabaseImplement/Implements/ImplementerStorage.cs +++ b/Course/DatabaseImplement/Implements/ImplementerStorage.cs @@ -23,7 +23,7 @@ namespace DatabaseImplement.Implements { if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login)) { return null; } using var context = new FactoryGoWorkDatabase(); - return context.Implementers.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.Login) && x.Login.Equals(model.Login)))?.GetViewModel; ; + return context.Implementers.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.Login) && !string.IsNullOrEmpty(model.Password) && x.Login.Equals(model.Login) && x.Password.Equals(model.Password)))?.GetViewModel; ; } public List GetFilteredList(ImplementerSearchModel model) diff --git a/Course/DatabaseImplement/Implements/ProductStorage.cs b/Course/DatabaseImplement/Implements/ProductStorage.cs index 93ad8c6..31c1fd9 100644 --- a/Course/DatabaseImplement/Implements/ProductStorage.cs +++ b/Course/DatabaseImplement/Implements/ProductStorage.cs @@ -39,7 +39,7 @@ namespace DatabaseImplement.Implements else if (model.WorkerId.HasValue) return context.Products.Where(p => p.MachineId.HasValue).Include(p => p.Machine).ThenInclude(p => p.Workers).Where(x => x.Machine.Workers.FirstOrDefault(y => y.WorkerId == model.WorkerId) != null).Select(x => x.GetViewModel).ToList(); else - return context.Products.Include(p => p.Details).ThenInclude(p => p.Detail).Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList(); + return context.Products.Include(p => p.Details).ThenInclude(p => p.Detail).Where(x => x.UserId == model.UserId).Select(x => x.GetViewModel).ToList(); } public List GetFullList() diff --git a/Course/ImplemenerLogic/DataToLogic.cs b/Course/ImplemenerLogic/DataToLogic.cs new file mode 100644 index 0000000..19211af --- /dev/null +++ b/Course/ImplemenerLogic/DataToLogic.cs @@ -0,0 +1,13 @@ +using DatabaseImplement.Implements; +using BusinessLogic.BusinessLogic; + +namespace ImplemenerLogic +{ + public static class DataToLogic + { + public static DetailLogic detailLogic() + { + return + } + } +} diff --git a/Course/ImplemenerLogic/ImplemenerLogic.csproj b/Course/ImplemenerLogic/ImplemenerLogic.csproj new file mode 100644 index 0000000..b2c1bac --- /dev/null +++ b/Course/ImplemenerLogic/ImplemenerLogic.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/Course/ImplementerApp/Controllers/HomeController.cs b/Course/ImplementerApp/Controllers/HomeController.cs index 54266c8..7eb8774 100644 --- a/Course/ImplementerApp/Controllers/HomeController.cs +++ b/Course/ImplementerApp/Controllers/HomeController.cs @@ -5,82 +5,119 @@ using BusinessLogic.BusinessLogic; using Contracts.BusinessLogicsContracts; using Contracts.ViewModels; using DataModels.Models; +using Contracts.BindingModels; namespace ImplementerApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; - private readonly IDetailLogic _detailLogic; - private readonly IImplementerLogic _userLogic; - private readonly IProductLogic _productLogic; - private readonly IProductionLogic _productionLogic; - - - public HomeController(ILogger logger) + private readonly ImplementerData _data; + public HomeController(ILogger logger, ImplementerData data) { _logger = logger; + _data = data; + } + public IActionResult IndexNonReg() + { + if (UserImplementer.user == null) + return View(); + return RedirectToAction("Index"); } - public IActionResult Index() { + if (UserImplementer.user == null) + return RedirectToAction("IndexNonReg"); return View(); } + [HttpGet] public IActionResult Enter() { - return View(); + if (UserImplementer.user == null) + return View(); + return RedirectToAction("Index"); } + [HttpPost] + public void Enter(string login, string password) + { + var user = _data.Login(login, password); + if (user != null) + { + UserImplementer.user = user; + Response.Redirect("Index"); + } + } + [HttpGet] public IActionResult Register() { return View(); } + [HttpPost] + public void Register(string name, string login, string email, string password1, string password2) + { + if (password1 == password2 && _data.Register(new() { Email = email, Login = login, Name = name, Password = password1 })) + { + Response.Redirect("Index"); + } + } + [HttpGet] public IActionResult IndexDetail() { - var details = new List(); - details.Add(new DetailViewModel + if (UserImplementer.user != null) { - 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 list = _data.GetDetails(UserImplementer.user.Id); + if (list != null) + return View(list); + return View(new List()); + } + return RedirectToAction("IndexNonReg"); } - public IActionResult CreateDetail() - { - return View(); + [HttpPost] + public void IndexDetail(int id) + { + if (UserImplementer.user != null) + { + _data.DeleteDetail(id); + } + Response.Redirect("IndexDetail"); + } + [HttpGet] + public IActionResult CreateDetail(int id) + { + if (id != 0) + { + var value = _data.GetDetail(id); + if (value != null) + return View(value); + } + return View(new DetailViewModel()); } - + [HttpPost] + public IActionResult CreateDetail(DetailBindingModel model) + { + if (model.Id == 0) + { + model.DateCreate = DateTime.Now; + model.UserId = UserImplementer.user!.Id; + if (_data.CreateDetail(model)) + return RedirectToAction("IndexDetail"); + } + else + { + if (_data.UpdateDetail(model)) + return RedirectToAction("IndexDetail"); + } + return View(); + } + [HttpGet] public IActionResult IndexProduct() { - List products = new List - { - new ProductViewModel - { - Id = 1, - Name = "Изделие 1", - Cost = 10.99, - UserId = 1, - MachineId = 1 - }, - new ProductViewModel - { - Id = 2, - Name = "Изделие 2", - Cost = 19.99, - UserId = 2, - MachineId = 2 - } - }; - return View(products); - } + if (UserImplementer.user != null) { + var products = _data.GetProducts(UserImplementer.user.Id); + return View(products); + } + return RedirectToAction("IndexNonReg"); + } public IActionResult CreateProduct() { var details = new List(); @@ -100,27 +137,16 @@ namespace ImplementerApp.Controllers }); return View(details); } + [HttpGet] public IActionResult IndexProduction() { - List productionViewModels = new List + if (UserImplementer.user != null) { - new ProductionViewModel - { - Id = 1, - Name = "Производство А", - Cost = 1000.00, - UserId = 1 - }, - new ProductionViewModel - { - Id = 2, - Name = "Производство Б", - Cost = 1500.00, - UserId = 2 - } - }; - return View(productionViewModels); - } + var productions = _data.GetProductions(UserImplementer.user.Id); + return View(productions); + } + return RedirectToAction("IndexNonReg"); + } public IActionResult CreateProduction() { var details = new List(); diff --git a/Course/ImplementerApp/ImplementerApp.csproj b/Course/ImplementerApp/ImplementerApp.csproj index cbd6681..1ab9af5 100644 --- a/Course/ImplementerApp/ImplementerApp.csproj +++ b/Course/ImplementerApp/ImplementerApp.csproj @@ -8,11 +8,13 @@ + + diff --git a/Course/ImplementerApp/ImplementerData.cs b/Course/ImplementerApp/ImplementerData.cs new file mode 100644 index 0000000..9a5b14c --- /dev/null +++ b/Course/ImplementerApp/ImplementerData.cs @@ -0,0 +1,77 @@ +using Contracts.BusinessLogicsContracts; +using Contracts.ViewModels; +using Contracts.BindingModels; +using Contracts.StoragesContracts; +using Contracts.SearchModels; + +namespace ImplementerApp +{ + public class ImplementerData + { + private readonly ILogger _logger; + private readonly IImplementerLogic _implementerLogic; + private readonly IDetailLogic _detailLogic; + private readonly IProductionLogic _productionLogic; + private readonly IProductLogic _productLogic; + + public ImplementerData(ILogger logger, IImplementerLogic implementerLogic, IDetailLogic detailLogic, IProductionLogic productionLogic, IProductLogic productLogic) + { + _logger = logger; + _implementerLogic = implementerLogic; + _detailLogic = detailLogic; + _productionLogic = productionLogic; + _productLogic = productLogic; + } + + public ImplementerViewModel? Login(string login, string password) + { + return _implementerLogic.ReadElement(new() + { + Login = login, + Password = password + }); + } + + public bool Register(ImplementerBindingModel model) + { + return _implementerLogic.Create(model); + } + + public bool UpdateUser(ImplementerBindingModel model) + { + return _implementerLogic.Update(model); + } + + public List? GetDetails(int userId) + { + return _detailLogic.ReadList(new DetailSearchModel() { UserId = userId }); + } + + public bool DeleteDetail(int detailId) + { + return _detailLogic.Delete(new() { Id = detailId }); + } + public bool CreateDetail(DetailBindingModel model) + { + return _detailLogic.Create(model); + } + public bool UpdateDetail(DetailBindingModel model) + { + return _detailLogic.Update(model); + } + public DetailViewModel? GetDetail(int id) + { + return _detailLogic.ReadElement(new() { Id= id }); + } + + public List? GetProducts(int userId) + { + return _productLogic.ReadList(new ProductSearchModel() { UserId = userId }); + } + + public List? GetProductions(int userId) + { + return _productionLogic.ReadList(new() { UserId = userId }); + } + } +} diff --git a/Course/ImplementerApp/Program.cs b/Course/ImplementerApp/Program.cs index 36efc00..463d0ba 100644 --- a/Course/ImplementerApp/Program.cs +++ b/Course/ImplementerApp/Program.cs @@ -1,9 +1,27 @@ +using BusinessLogic.BusinessLogic; using Contracts.BusinessLogicsContracts; +using Contracts.StoragesContracts; +using DatabaseImplement.Implements; +using ImplementerApp; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation(); +builder.Logging.SetMinimumLevel(LogLevel.Trace); +builder.Logging.AddLog4Net("log4net.config"); + +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + + var app = builder.Build(); if (!app.Environment.IsDevelopment()) diff --git a/Course/ImplementerApp/UserImplementer.cs b/Course/ImplementerApp/UserImplementer.cs new file mode 100644 index 0000000..ea8c905 --- /dev/null +++ b/Course/ImplementerApp/UserImplementer.cs @@ -0,0 +1,9 @@ +using Contracts.ViewModels; + +namespace ImplementerApp +{ + public static class UserImplementer + { + public static ImplementerViewModel? user { get; set; } + } +} diff --git a/Course/ImplementerApp/Views/Home/CreateDetail.cshtml b/Course/ImplementerApp/Views/Home/CreateDetail.cshtml index bf68c93..bb0d71a 100644 --- a/Course/ImplementerApp/Views/Home/CreateDetail.cshtml +++ b/Course/ImplementerApp/Views/Home/CreateDetail.cshtml @@ -1,17 +1,20 @@ -@{ +@using Contracts.ViewModels; +@{ ViewData["Title"] = "CreateDetail"; } +@model DetailViewModel;
-

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

+

Деталь

+
Название:
-
+
Цена:
-
+
diff --git a/Course/ImplementerApp/Views/Home/CreateProduct.cshtml b/Course/ImplementerApp/Views/Home/CreateProduct.cshtml index 0033b22..aa11933 100644 --- a/Course/ImplementerApp/Views/Home/CreateProduct.cshtml +++ b/Course/ImplementerApp/Views/Home/CreateProduct.cshtml @@ -19,22 +19,21 @@ - + @foreach (var detail in Model) { - + } diff --git a/Course/ImplementerApp/Views/Home/Index.cshtml b/Course/ImplementerApp/Views/Home/Index.cshtml index b3e0f4b..bceb1e4 100644 --- a/Course/ImplementerApp/Views/Home/Index.cshtml +++ b/Course/ImplementerApp/Views/Home/Index.cshtml @@ -3,14 +3,12 @@ }
-

Приложение "Завод "Иди работать". Исполнитель"

+

Главное меню

diff --git a/Course/ImplementerApp/Views/Home/IndexDetail.cshtml b/Course/ImplementerApp/Views/Home/IndexDetail.cshtml index ac228d1..f24d465 100644 --- a/Course/ImplementerApp/Views/Home/IndexDetail.cshtml +++ b/Course/ImplementerApp/Views/Home/IndexDetail.cshtml @@ -18,7 +18,7 @@ return; }

- Создать деталь + Создать деталь

Выбор Название КоличествоУдалить
- - @detail.Name +
@@ -57,7 +57,10 @@ Изменить } diff --git a/Course/ImplementerApp/Views/Home/IndexNonReg.cshtml b/Course/ImplementerApp/Views/Home/IndexNonReg.cshtml new file mode 100644 index 0000000..3d737d5 --- /dev/null +++ b/Course/ImplementerApp/Views/Home/IndexNonReg.cshtml @@ -0,0 +1,10 @@ +@{ + ViewData["Title"] = "Home Page"; +} + + diff --git a/Course/ImplementerApp/Views/Home/IndexProduct.cshtml b/Course/ImplementerApp/Views/Home/IndexProduct.cshtml index 820b716..97ac210 100644 --- a/Course/ImplementerApp/Views/Home/IndexProduct.cshtml +++ b/Course/ImplementerApp/Views/Home/IndexProduct.cshtml @@ -10,7 +10,6 @@

Изделия

-ProductMachineAdd
@{ if (Model == null) @@ -19,7 +18,7 @@ ProductMachineAdd return; }

- Создать изделие + Создать изделие

- Удалить + + + +
@@ -64,7 +63,10 @@ ProductMachineAdd Изменить } diff --git a/Course/ImplementerApp/Views/Home/IndexProduction.cshtml b/Course/ImplementerApp/Views/Home/IndexProduction.cshtml index 6bdc916..3ef96cf 100644 --- a/Course/ImplementerApp/Views/Home/IndexProduction.cshtml +++ b/Course/ImplementerApp/Views/Home/IndexProduction.cshtml @@ -18,7 +18,7 @@ return; }

- Создать производство + Создать производство

- Удалить +
+ + +
@@ -57,7 +57,10 @@ Изменить } diff --git a/Course/ImplementerApp/Views/Home/Privacy.cshtml b/Course/ImplementerApp/Views/Home/Privacy.cshtml index dcde174..62b4e65 100644 --- a/Course/ImplementerApp/Views/Home/Privacy.cshtml +++ b/Course/ImplementerApp/Views/Home/Privacy.cshtml @@ -5,6 +5,7 @@

Личные данные

+
Логин:
diff --git a/Course/ImplementerApp/Views/Shared/_Layout.cshtml b/Course/ImplementerApp/Views/Shared/_Layout.cshtml index ca5b590..ee64ee1 100644 --- a/Course/ImplementerApp/Views/Shared/_Layout.cshtml +++ b/Course/ImplementerApp/Views/Shared/_Layout.cshtml @@ -1,4 +1,8 @@ - +@{ + ViewData["Name"] = UserImplementer.user == null ? "Пользователь" : UserImplementer.user.Name; +} + + @@ -13,7 +17,8 @@ diff --git a/Course/ImplementerApp/log4net.config b/Course/ImplementerApp/log4net.config new file mode 100644 index 0000000..1fd1998 --- /dev/null +++ b/Course/ImplementerApp/log4net.config @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file
- Удалить +
+ + +