From 2c7e357bdfb699cca367ba197d11fefae2ab0e6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=88=D0=B8=D0=BD=20=D0=9C=D0=B0=D0=BA=D1=81?= =?UTF-8?q?=D0=B8=D0=BC?= Date: Sun, 23 Apr 2023 20:44:40 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=81=D0=B5=20=D0=BF=D0=BE=D1=88=D0=BB?= =?UTF-8?q?=D0=BE=20=D0=BD=D0=B5=20=D0=BF=D0=BE=20=D0=BF=D0=BB=D0=B0=D0=BD?= =?UTF-8?q?=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/HomeController.cs | 153 +++++++++++++++--- 1 file changed, 134 insertions(+), 19 deletions(-) diff --git a/PrecastConcretePlant/PrecastConcretePlantShopApp/Controllers/HomeController.cs b/PrecastConcretePlant/PrecastConcretePlantShopApp/Controllers/HomeController.cs index 7ea2c8b..19eb6e0 100644 --- a/PrecastConcretePlant/PrecastConcretePlantShopApp/Controllers/HomeController.cs +++ b/PrecastConcretePlant/PrecastConcretePlantShopApp/Controllers/HomeController.cs @@ -1,4 +1,6 @@ using Microsoft.AspNetCore.Mvc; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; using PrecastConcretePlantShopApp.Models; using System.Diagnostics; @@ -6,27 +8,140 @@ namespace PrecastConcretePlantShopApp.Controllers { public class HomeController : Controller { - private readonly ILogger _logger; + private readonly ILogger _logger; - public HomeController(ILogger logger) - { - _logger = logger; - } + public HomeController(ILogger logger) + { + _logger = logger; + } - public IActionResult Index() - { - return View(); - } + public IActionResult Index() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.GetRequest>($"api/main/getorders?clientId={APIClient.Client.Id}")); + } - public IActionResult Privacy() - { - return View(); - } + [HttpGet] + public IActionResult Privacy() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.Client); + } - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] - public IActionResult Error() - { - return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); - } - } + [HttpPost] + public void Privacy(string login, string password, string fio) + { + if (APIClient.Client == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) + { + throw new Exception("Введите логин, пароль и ФИО"); + } + APIClient.PostRequest("api/client/updatedata", new ClientBindingModel + { + Id = APIClient.Client.Id, + ClientFIO = fio, + Email = login, + Password = password + }); + + APIClient.Client.ClientFIO = fio; + APIClient.Client.Email = login; + APIClient.Client.Password = password; + Response.Redirect("Index"); + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult Error() + { + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); + } + + [HttpGet] + public IActionResult Enter() + { + return View(); + } + + [HttpPost] + public void Enter(string login, string password) + { + if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) + { + throw new Exception("Введите логин и пароль"); + } + APIClient.Client = APIClient.GetRequest($"api/client/login?login={login}&password={password}"); + if (APIClient.Client == null) + { + throw new Exception("Неверный логин/пароль"); + } + Response.Redirect("Index"); + } + + [HttpGet] + public IActionResult Register() + { + return View(); + } + + [HttpPost] + public void Register(string login, string password, string fio) + { + if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) + { + throw new Exception("Введите логин, пароль и ФИО"); + } + APIClient.PostRequest("api/client/register", new ClientBindingModel + { + ClientFIO = fio, + Email = login, + Password = password + }); + Response.Redirect("Enter"); + return; + } + + [HttpGet] + public IActionResult Create() + { + ViewBag.Reinforcedies = APIClient.GetRequest>("api/main/getreinforcedylist"); + return View(); + } + + [HttpPost] + public void Create(int reinforced, int count) + { + if (APIClient.Client == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (count <= 0) + { + throw new Exception("Количество и сумма должны быть больше 0"); + } + APIClient.PostRequest("api/main/createorder", new OrderBindingModel + { + ClientId = APIClient.Client.Id, + ReinforcedId = reinforced, + Count = count, + Sum = Calc(count, reinforced) + }); + Response.Redirect("Index"); + } + + [HttpPost] + public double Calc(int count, int reinforced) + { + var prod = APIClient.GetRequest($"api/main/getreinforced?reinforcedId={reinforced}"); + return count * (prod?.Price ?? 1); + } + } } \ No newline at end of file