using ComputerShopContracts.BindingModels; using ComputerShopContracts.SearchModels; using ComputerShopContracts.ViewModels; using DocumentFormat.OpenXml.Office2010.Excel; using Microsoft.AspNetCore.Mvc; using OrdererClientApp.Models; using System.ComponentModel; using System.Diagnostics; namespace OrdererClientApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult Index() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/equipmentreceiving/getreceivings?clientId={APIClient.Client.Id}")); } public IActionResult Order() { if (APIClient.Client == null) { return Redirect("~/Order/Enter"); } return View(APIClient.GetRequest>($"api/order/getorderlist?clientId={APIClient.Client.Id}")); } public IActionResult Supply() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/supply/getsupplylist")); } [HttpGet] public IActionResult Privacy() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } return View(APIClient.Client); } [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; } [HttpPost] public void CreateReceiving() { if (APIClient.Client == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } APIClient.PostRequest("api/equipmentreceiving/createreceiving", new EquipmentReceivingBindingModel { ClientId = APIClient.Client.Id }); Response.Redirect("Index"); } [HttpPost] public void SetFinish(int id) { if (APIClient.Client == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } APIClient.PostRequest($"api/equipmentreceiving/setfinish?id={id}", id); Response.Redirect("Index"); } [HttpPost] public void DeleteReceiving(int receiving) { if (APIClient.Client == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } APIClient.PostRequest($"api/equipmentreceiving/deletereceiving?id={receiving}", receiving); Response.Redirect("Index"); } [HttpGet] public IActionResult CreateOrder() { return View(); } [HttpPost] public void CreateOrder(int sum) { if (APIClient.Client == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } APIClient.PostRequest("api/order/createorder", new OrderBindingModel { ClientId = APIClient.Client.Id, Sum = sum }); Response.Redirect("Index"); } [HttpGet] public IActionResult DeleteOrder() { ViewBag.Components = APIClient.GetRequest>("api/main/getorderlist"); return View(); } [HttpDelete] public void DeleteOrder(int order) { if (APIClient.Client == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } APIClient.PostRequest("api/order/deleteorder", new ComponentBindingModel { Id = order }); Response.Redirect("Index"); } public IActionResult AddAssemblyToOrder() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } ViewBag.Assemblies = APIClient.GetRequest>($"api/order/getorderlist?clientId={APIClient.Client.Id}"); ViewBag.Components = APIClient.GetRequest>($"api/assembly/getassemblylist?clientId={APIClient.Client.Id}"); return View(); } [HttpPost] public void AddAssemblyToOrder(int order, int assembly, int amount) { if (APIClient.Client == null) { throw new Exception("Необходима авторизация"); } APIClient.PostRequest("api/order/AddAssemblyToOrder", Tuple.Create( new AssemblySearchModel() { Id = assembly }, new OrderSearchModel() { Id = order }, amount )); } [HttpPost] public double Calc(int count, int assembly) { var or = APIClient.GetRequest($"api/main/getassembly?assemblyId={assembly}"); return count * (or?.Sum ?? 1); } } }