using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.ViewModels; using ElectronicsShopDataModels.Models; using ElectronicsShopUserApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using System.Reflection; using System.Runtime.Serialization; namespace ElectronicsShopUserApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; private Dictionary _productList; //private readonly IOrderLogic _order; public HomeController(ILogger logger/*, IOrderLogic orderLogic*/) { _logger = logger; _productList = new Dictionary(); } public IActionResult Index() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequset>($"api/main/getorders?_clientid={APIClient.Client.ID}")); } [HttpGet] public IActionResult Privacy() { if (APIClient.Client == null) { return Redirect("~/home/Enter"); } return View(APIClient.Client); } [HttpPost] public void Privacy(string email, string password, string fio) { if (APIClient.Client == null) { throw new Exception("Вход для авторизованных"); } if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) { throw new Exception("Введите почту, пароль и ФИО"); } APIClient.PostRequest("api/client/updatedata", new ClientBindingModel { ID = APIClient.Client.ID, ClientFIO = fio, Email = email, Password = password, }); APIClient.Client.ClientFIO = fio; APIClient.Client.Email = email; 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 email, string password) { if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { throw new Exception("Введите почту и пароль"); } APIClient.Client = APIClient.GetRequset($"api/Client/Login?email={email}&password={password}"); if (APIClient.Client == null) { throw new Exception("Неверный адрес почты/пароль"); } Response.Redirect("Index"); } [HttpGet] public IActionResult Register() { return View(); } [HttpPost] public void Register(string email, string password, string fio) { if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) { throw new Exception("Введите почту,пароль и ФИО"); } APIClient.PostRequest("api/client/register", new ClientBindingModel { ClientFIO = fio, Email = email, Password = password }); Response.Redirect("Enter"); return; } public IActionResult Orders() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequset>($"api/main/getorders?_clientid={APIClient.Client.ID}")); } [HttpGet] public IActionResult CreateOrder() { var view = APIClient.GetRequset($"api/main/getorder?_clientid={APIClient.Client?.ID}") ?? new OrderViewModel { ProductList = _productList }; return View(view); } [HttpGet] public IActionResult AddProduct() { ViewBag.Products = APIClient.GetRequset>($"api/main/getproducts"); return View(); } [HttpPost] public void AddProduct(double sum, int product, int count) { if (APIClient.Client == null) { throw new Exception("Только для авторизованых"); } if (sum <= 0) { throw new Exception("Сумма затрат должна быть больше 0"); } var _product = APIClient.GetRequset($"api/main/getproduct?_productid={product}"); _product.Price = sum; if (_productList.ContainsKey(product)) { _productList[product] = (_product, count); } else { _productList.Add(product, (_product, count)); } APIClient.PostDictRequest($"api/main/saveorder", new OrderBindingModel { ProductList = _productList, Sum = CalcAll(_productList), ClientID = APIClient.Client.ID, }); Response.Redirect("CreateOrder"); } [HttpPost] private double CalcAll(Dictionary ProductList) { Double Sum = 0; foreach (var ProductItem in ProductList) { Sum += (ProductItem.Value.Item1.Price * ProductItem.Value.Item2); } return Sum; } [HttpPost] public double Calc(int count, int product) { var prod = APIClient.GetRequset($"api/main/getproduct?_productid={product}"); return count * (prod?.Price ?? 1); } } }