129 lines
4.6 KiB
C#
129 lines
4.6 KiB
C#
using ElectronicsShopContracts.BindingModels;
|
|
using ElectronicsShopContracts.ViewModels;
|
|
using ElectronicsShopDataModels.Models;
|
|
using ElectronicsShopUserApp.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Diagnostics;
|
|
using System.Runtime.Serialization;
|
|
|
|
namespace ElectronicsShopUserApp.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.GetRequset<List<OrderViewModel>>($"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<ClientViewModel>($"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;
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Create() {
|
|
ViewBag.Products = APIClient.GetRequset<List<ProductViewModel>>("api/main/getproducts");
|
|
return View();
|
|
}
|
|
[HttpPost]
|
|
public void Create(OrderBindingModel order) {
|
|
if (APIClient.Client == null)
|
|
{
|
|
throw new Exception("Âû êàê ñóäà ïîïàëè? Ñóäà âõîä òîëüêî àâòîðèçîâàííûì");
|
|
}
|
|
if (order.ProductList.Count <= 0)
|
|
{
|
|
throw new Exception("Êîëè÷åñòâî è ñóììà äîëæíû áûòü áîëüøå 0");
|
|
}
|
|
APIClient.PostRequest("api/main/createorder", new OrderBindingModel
|
|
{
|
|
ClientID = APIClient.Client.ID,
|
|
Sum = Calc(order.ProductList),
|
|
ProductList = order.ProductList,
|
|
DateCreate = DateTime.Now,
|
|
});
|
|
Response.Redirect("Index");
|
|
}
|
|
[HttpPost]
|
|
private double Calc(Dictionary<int, (IProductModel, int)> ProductList)
|
|
{
|
|
Double Sum = 0;
|
|
foreach (var ProductItem in ProductList)
|
|
{
|
|
Sum += (ProductItem.Value.Item1.Price * ProductItem.Value.Item2);
|
|
}
|
|
return Sum;
|
|
}
|
|
}
|
|
}
|