CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs

252 lines
9.1 KiB
C#
Raw Normal View History

using ElectronicsShopContracts.BindingModels;
2024-05-29 23:03:32 +04:00
using ElectronicsShopContracts.BusinessLogicContracts;
2024-05-29 18:32:24 +04:00
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.ViewModels;
2024-05-29 15:31:22 +04:00
using ElectronicsShopDataModels.Models;
using ElectronicsShopUserApp.Models;
using Microsoft.AspNetCore.Mvc;
2024-05-31 16:05:43 +04:00
using Newtonsoft.Json;
using System.Diagnostics;
2024-05-29 23:03:32 +04:00
using System.Reflection;
using System.Runtime.Serialization;
2024-05-31 16:05:43 +04:00
using System.Xml.Serialization;
namespace ElectronicsShopUserApp.Controllers {
public class HomeController : Controller {
private readonly ILogger<HomeController> _logger;
private Dictionary<int, (IProductModel, int)> _productList;
2024-06-01 00:42:46 +04:00
public int Id;
//private readonly IOrderLogic _order;
public HomeController(ILogger<HomeController> logger) {
_logger = logger;
2024-05-31 16:05:43 +04:00
_productList = new Dictionary<int, (IProductModel, int)>();
}
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("<22><><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD>");
}
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("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
2024-05-27 19:12:19 +04:00
APIClient.Client = APIClient.GetRequset<ClientViewModel>($"api/Client/Login?email={email}&password={password}");
if (APIClient.Client == null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Register() {
return View();
}
2024-05-29 15:31:22 +04:00
[HttpPost]
public void Register(string email, string password, string fio) {
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD>");
}
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");
}
2024-05-30 23:49:53 +04:00
2024-05-31 16:05:43 +04:00
var view = APIClient.GetRequset<List<OrderViewModel>>($"api/main/getorders?_clientid={APIClient.Client.ID}");
2024-05-30 23:49:53 +04:00
2024-05-31 16:05:43 +04:00
return View(view);
}
[HttpGet]
public IActionResult CreateOrder() {
2024-05-30 23:49:53 +04:00
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");
}
2024-05-29 23:03:32 +04:00
2024-05-30 23:49:53 +04:00
[HttpGet]
2024-06-01 00:42:46 +04:00
public IActionResult DeleteOrder(int id) {
APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id });
return RedirectToAction("Orders");
}
[HttpGet]
2024-05-30 23:49:53 +04:00
public IActionResult OrderView() {
if (APIClient.Client == null) {
return Redirect("~/Home/Enter");
}
var view = APIClient.GetRequset<OrderViewModel>($"api/main/getorder?_clientid={APIClient.Client?.ID}");
2024-06-01 00:42:46 +04:00
if (view != null) {
Id = view.ID;
}
2024-05-31 16:05:43 +04:00
var products = APIClient.GetRequset<List<List<string>>>($"api/main/getorderproducts?_orderid={view?.ID}");
2024-05-30 23:49:53 +04:00
2024-05-31 16:05:43 +04:00
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));
}
2024-06-01 00:42:46 +04:00
(int, Dictionary<int, (IProductModel, int)>) tuple = (Id, _productList);
return View(tuple);
2024-05-31 16:05:43 +04:00
}
[HttpPost]
2024-06-01 00:42:46 +04:00
public void OrderView(int sum, int id) {
if (sum <= 0) {
APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id});
}
2024-05-31 16:05:43 +04:00
Response.Redirect("Orders");
2024-05-30 23:49:53 +04:00
}
2024-06-01 00:42:46 +04:00
[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]
2024-05-30 23:49:53 +04:00
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}");
2024-05-30 23:49:53 +04:00
APIClient.ListPostRequest($"api/main/addproduct", _product, count, _order.ID);
Response.Redirect("OrderView");
}
2024-05-29 18:32:24 +04:00
[HttpPost]
2024-05-29 21:23:34 +04:00
public double Calc(int count, int product)
2024-05-29 18:32:24 +04:00
{
2024-05-30 23:49:53 +04:00
var _product = APIClient.GetRequset<ProductViewModel>($"api/main/getproduct?_productid={product}");
return count * (_product?.Price ?? 1);
2024-05-29 18:32:24 +04:00
}
[HttpGet]
public IActionResult Report()
{
//ViewBag.Reports = APIClient.GetRequset<List<ProductViewModel>>($"api/main/getproducts"); <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return View();
}
[HttpGet]
public IActionResult Message()
{
//ViewBag.Reports = APIClient.GetRequset<List<ProductViewModel>>($"api/main/getproducts"); <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
return View();
}
[HttpGet]
public IActionResult GetWordFile()
{
return new PhysicalFileResult("C:\\gg\\wordfile.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
}
public IActionResult GetExcelFile()
{
return new PhysicalFileResult("C:\\gg\\excelfile.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
[HttpPost]
public void ListPreserves(List<int> preserve, string type)
{/*
if (APIEmployee.Employee == null)
{
throw new Exception("<22><> <20><><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>? <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
<EFBFBD> <EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (preserve.Count <= 0)
{
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0");
}
if (string.IsNullOrEmpty(type))
{
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
if (type == "docx")
{
APIEmployee.PostRequest("api/report/createroductlistwordfile", new ReportProductBindingModel
{
Preserve = preserve,
FileName = "C:\\gg\\wordfile.docx"
});
Response.Redirect("GetWordFile");
}
else
{
APIEmployee.PostRequest("api/report/createproductlistexcelfile", new ReportProductBindingModel
{
Preserve = preserve,
FileName = "C:\\gg\\excelfile.xlsx"
});
Response.Redirect("GetExcelFile");
}*/
}
}
}