CourseWork_CompShop/ComputerShopProvider/OrdererClientApp/Controllers/HomeController.cs

250 lines
7.9 KiB
C#

using ComputerShopContracts.BindingModels;
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<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<EquipmentReceivingViewModel>>($"api/equipmentreceiving/getreceivings?clientId={APIClient.Client.Id}"));
}
public IActionResult Order()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<OrderViewModel>>($"api/component/order"));
}
public IActionResult Supply()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<SupplyViewModel>>($"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<ClientViewModel>($"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 CreateReceiving()
{
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
APIClient.PostRequest("api/equipmentreceiving/createreceiving", new EquipmentReceivingBindingModel
{
ClientId = APIClient.Client.Id
});
Response.Redirect("Index");
return View();
}
[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<List<ComponentViewModel>>("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");
}
[HttpGet]
public IActionResult CreateAssembly()
{
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/component/getcomponentlist");
ViewBag.CurrentComponents = new List<ComponentViewModel>();
return View();
}
[HttpPost]
public void CreateAssembly(string name, int sum)
{
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (sum <= 0)
{
throw new Exception("Сумма должна быть больше 0");
}
APIClient.PostRequest("api/assembly/createassembly", new AssemblyBindingModel
{
ClientId = APIClient.Client.Id,
AssemblyName = name,
Price = 0,
AssemblyComponents = new()
});
System.Diagnostics.Debug.WriteLine("it might work");
Response.Redirect("Index");
}
[HttpPost]
public double Calc(int count, int order)
{
var or = APIClient.GetRequest<OrderViewModel>($"api/main/getcomponent?componentId={order}");
return count * (or?.Sum ?? 1);
}
}
}