CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs
Илья Федотов 80f101fd23 is done
2024-06-01 06:49:45 +04:00

293 lines
9.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums;
using ElectronicsShopDataModels.Models;
using ElectronicsShopUserApp.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace ElectronicsShopUserApp.Controllers {
public class HomeController : Controller {
private readonly ILogger<HomeController> _logger;
private Dictionary<int, (IProductModel, int)> _productList;
public int Id;
//private readonly IOrderLogic _order;
public HomeController(ILogger<HomeController> logger/*, IOrderLogic orderLogic*/) {
_logger = logger;
//_order = orderLogic;
_productList = new Dictionary<int, (IProductModel, int)>();
}
[HttpGet]
public IActionResult Index() {
if (APIClient.Client == null) {
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequset<List<PaymeantViewModel>>($"api/client/getpaymeants?_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;
}
public IActionResult Orders() {
if (APIClient.Client == null) {
return Redirect("~/Home/Enter");
}
var view = APIClient.GetRequset<List<OrderViewModel>>($"api/main/getorders?_clientid={APIClient.Client.ID}");
return View(view);
}
[HttpGet]
public IActionResult CreateOrder() {
if (APIClient.Client == null) {
return Redirect("~/Home/Enter");
}
APIClient.PostRequest("api/main/createorder", new OrderBindingModel {
ClientID = APIClient.Client.ID,
DateCreate = DateTime.Now,
});
return RedirectToAction("OrderView");
}
[HttpGet]
public IActionResult DeleteOrder(int id) {
APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id });
return RedirectToAction("Orders");
}
[HttpGet]
public IActionResult EditOrder(int id) {
if (APIClient.Client == null) {
return Redirect("~/Home/Enter");
}
if (id == 0) {
return RedirectToAction("Orders");
}
var products = APIClient.GetRequset<List<List<string>>>($"api/main/getorderproducts?_orderid={id}");
foreach (var pr in products) {
var product = JsonConvert.DeserializeObject<ProductViewModel>(pr[0]);
int count = JsonConvert.DeserializeObject<int>(pr[1]);
_productList.Add(product.ID, (product, count));
}
(int, Dictionary<int, (IProductModel, int)>) tuple = (id, _productList);
return View(tuple);
}
[HttpPost]
public void EditOrder(double sum, int id) {
if (sum <= 0) {
APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id });
}
Response.Redirect("Orders");
}
[HttpGet]
public IActionResult OrderView() {
if (APIClient.Client == null) {
return Redirect("~/Home/Enter");
}
var view = APIClient.GetRequset<OrderViewModel>($"api/main/getorder?_clientid={APIClient.Client?.ID}");
if (view != null) {
Id = view.ID;
}
var products = APIClient.GetRequset<List<List<string>>>($"api/main/getorderproducts?_orderid={view?.ID}");
foreach (var pr in products) {
var product = JsonConvert.DeserializeObject<ProductViewModel>(pr[0]);
int count = JsonConvert.DeserializeObject<int>(pr[1]);
_productList.Add(product.ID, (product, count));
}
(int, Dictionary<int, (IProductModel, int)>) tuple = (Id, _productList);
return View(tuple);
}
[HttpPost]
public void OrderView(double sum, int id) {
if (sum <= 0) {
APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id});
}
Response.Redirect("Orders");
}
[HttpGet]
public IActionResult DeleteProductOrder(int id) {
var view = APIClient.GetRequset<OrderViewModel>($"api/main/getorder?_clientid={APIClient.Client?.ID}");
APIClient.PostRequestStr($"api/main/deleteproductorder", view.ID, id);
return RedirectToAction("OrderView");
}
[HttpGet]
public IActionResult AddProduct() {
ViewBag.Products = APIClient.GetRequset<List<ProductViewModel>>($"api/main/getproducts");
return View();
}
[HttpPost]
public void AddProduct(int product, int count) {
var _product = APIClient.GetRequset<ProductViewModel>($"api/main/getproduct?_productid={product}");
var _order = APIClient.GetRequset<OrderViewModel>($"api/main/getorder?_clientid={APIClient.Client?.ID}");
APIClient.ListPostRequest($"api/main/addproduct", _product, count, _order.ID);
Response.Redirect("OrderView");
}
[HttpGet]
public IActionResult Report()
{
return View();
}
[HttpPost]
public void Report(int count)
{
}
[HttpGet]
public IActionResult Message()
{
//ViewBag.Reports = APIClient.GetRequset<List<ProductViewModel>>($"api/main/getproducts"); Ïèñåì òàê æå ïîêà íåìà
return View();
}
[HttpGet]
public IActionResult Payment(int id)
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
if (id == 0)
{
return Redirect("~/Home/Index");
}
var products = APIClient.GetRequset<List<List<string>>>($"api/main/getorderproducts?_orderid={id}");
foreach (var pr in products)
{
var product = JsonConvert.DeserializeObject<ProductViewModel>(pr[0]);
int count = JsonConvert.DeserializeObject<int>(pr[1]);
_productList.Add(product.ID, (product, count));
}
(int, Dictionary<int, (IProductModel, int)>) tuple = (id, _productList);
return View(tuple);
}
[HttpPost]
public void Payment(double sum, double paysum, int id) {
if (APIClient.Client == null) {
throw new Exception("Òîëüêî äëÿ àâòîðèçîâàííûõ");
}
if (paysum <= 0) {
throw new Exception("Ñóììà îïïëàòû äîëæíà áûòü áîëüøå 0");
}
if (paysum > sum) {
throw new Exception("Ññóìà îïëàòû íå äîëæíà ïðåâûøàòü ñóììó ê îïëàòå");
}
APIClient.PostRequest("api/client/createpaymeant", new PaymeantBindingModel {
OrderID = id,
SumPayment = paysum,
PayOption = PayOptionCalc(sum, paysum),
ClientID = APIClient.Client.ID
});
Response.Redirect("Index");
}
[HttpPost]
public PaymeantOption PayOptionCalc(double sum, double paysum) {
if (paysum < sum) {
return PaymeantOption.×àñòè÷íàÿ_îïëàòà;
}
else if (paysum == sum) {
return PaymeantOption.Ïîëíàÿ_îïëàòà;
}
else {
return PaymeantOption.Íåîïëà÷åíî;
}
}
}
}