CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs

341 lines
13 KiB
C#
Raw Normal View History

2024-07-09 19:40:24 +04:00
using DocumentFormat.OpenXml.Bibliography;
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-06-01 05:00:05 +04:00
using ElectronicsShopDataModels.Enums;
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-07-23 20:29:10 +04:00
using System.Globalization;
2024-07-09 19:40:24 +04:00
using System.Net.Mime;
2024-05-29 23:03:32 +04:00
using System.Reflection;
using System.Runtime.Serialization;
2024-06-06 17:21:39 +04:00
using System.Text.RegularExpressions;
2024-05-31 16:05:43 +04:00
using System.Xml.Serialization;
namespace ElectronicsShopUserApp.Controllers {
public class HomeController : Controller {
private readonly ILogger<HomeController> _logger;
2024-07-23 20:29:10 +04:00
private Dictionary<int, (IProductModel, int)> _productList;
2024-06-01 00:42:46 +04:00
public int Id;
2024-06-06 17:21:39 +04:00
public HomeController(ILogger<HomeController> logger) {
_logger = logger;
2024-05-31 16:05:43 +04:00
_productList = new Dictionary<int, (IProductModel, int)>();
}
2024-06-01 06:31:02 +04:00
[HttpGet]
public IActionResult Index() {
2024-07-23 20:29:10 +04:00
if (APIClient.Client == null) {
return Redirect("~/Home/Enter");
2024-07-23 20:29:10 +04:00
}
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("<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");
}
2024-07-23 20:29:10 +04:00
[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();
}
2024-07-23 20:29:10 +04:00
[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-07-23 20:29:10 +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,
2024-07-26 12:41:39 +04:00
Status = OrderStatus.<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-05-30 23:49:53 +04:00
});
return RedirectToAction("OrderView");
2024-07-23 20:29:10 +04:00
}
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) {
2024-07-23 20:29:10 +04:00
APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id });
2024-06-01 00:42:46 +04:00
return RedirectToAction("Orders");
2024-07-23 20:29:10 +04:00
}
2024-06-01 00:42:46 +04:00
2024-06-01 02:22:06 +04:00
[HttpGet]
public IActionResult EditOrder(int id) {
2024-07-23 20:29:10 +04:00
if (APIClient.Client == null) {
return Redirect("~/Home/Enter");
}
2024-06-01 02:22:06 +04:00
2024-06-01 02:45:59 +04:00
if (id == 0) {
2024-06-01 03:37:37 +04:00
return RedirectToAction("Orders");
2024-06-01 02:45:59 +04:00
}
2024-07-23 20:29:10 +04:00
var products = APIClient.GetRequset<List<List<string>>>($"api/main/getorderproducts?_orderid={id}");
2024-06-01 02:22:06 +04:00
2024-07-24 09:53:21 +04:00
foreach (var pr in products ?? new List<List<string>>()) {
2024-07-23 20:29:10 +04:00
var product = JsonConvert.DeserializeObject<ProductViewModel>(pr[0]);
int count = JsonConvert.DeserializeObject<int>(pr[1]);
_productList.Add(product.ID, (product, count));
}
2024-06-01 02:22:06 +04:00
2024-07-23 20:29:10 +04:00
(int, Dictionary<int, (IProductModel, int)>) tuple = (id, _productList);
return View(tuple);
}
2024-06-01 02:22:06 +04:00
[HttpPost]
2024-07-23 20:29:10 +04:00
public void EditOrder(double sum, int id) {
if (sum <= 0) {
APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id });
}
2024-06-01 02:22:06 +04:00
2024-07-23 20:29:10 +04:00
Response.Redirect("Orders");
}
2024-06-01 02:22:06 +04:00
2024-07-23 20:29:10 +04:00
[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) {
2024-07-23 20:29:10 +04:00
var product = JsonConvert.DeserializeObject<ProductViewModel>(pr[0]);
2024-05-31 16:05:43 +04:00
int count = JsonConvert.DeserializeObject<int>(pr[1]);
_productList.Add(product.ID, (product, count));
2024-07-23 20:29:10 +04:00
}
2024-05-31 16:05:43 +04:00
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 05:00:05 +04:00
public void OrderView(double sum, int id) {
2024-06-01 00:42:46 +04:00
if (sum <= 0) {
2024-07-23 20:29:10 +04:00
APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id });
2024-06-01 00:42:46 +04:00
}
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) {
2024-07-24 09:53:21 +04:00
var view = APIClient.GetRequset<OrderViewModel>($"api/main/getorder?_clientid={APIClient.Client?.ID}")
?? throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
2024-07-23 20:29:10 +04:00
APIClient.PostRequestStr($"api/main/deleteproductorder", view.ID, id);
2024-06-01 00:42:46 +04:00
2024-07-26 16:01:31 +04:00
return Redirect($"https://localhost:7219/Home/EditOrder/{view.ID}");
2024-06-01 00:42:46 +04:00
}
2024-07-23 20:29:10 +04:00
[HttpGet]
2024-07-26 16:01:31 +04:00
public IActionResult AddProduct(int id) {
ViewBag.Products = APIClient.GetRequset<List<ProductViewModel>>($"api/main/getproducts");
2024-07-26 16:01:31 +04:00
return View(id);
}
2024-07-23 20:29:10 +04:00
[HttpPost]
2024-07-26 16:01:31 +04:00
public void AddProduct(int product, int count, int id) {
2024-07-24 09:53:21 +04:00
var _product = APIClient.GetRequset<ProductViewModel>($"api/main/getproduct?_productid={product}")
?? throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
2024-07-26 16:01:31 +04:00
APIClient.ListPostRequest($"api/main/addproduct", _product, count, id);
// <20> <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Response.Redirect($"https://localhost:7219/Home/EditOrder/{id}");
}
2024-06-01 05:00:05 +04:00
[HttpGet]
2024-07-23 20:29:10 +04:00
public IActionResult Payment(int id) {
if (APIClient.Client == null) {
return Redirect("~/Home/Enter");
}
if (id == 0) {
2024-06-01 05:00:05 +04:00
return Redirect("~/Home/Index");
2024-07-23 20:29:10 +04:00
}
var products = APIClient.GetRequset<List<List<string>>>($"api/main/getorderproducts?_orderid={id}");
2024-06-01 03:18:46 +04:00
2024-07-23 20:29:10 +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 03:18:46 +04:00
2024-07-24 17:40:56 +04:00
double sum = APIClient.GetRequset<OrderViewModel>($"api/main/getorder?_orderid={id}").Sum;
(double ,int, Dictionary<int, (IProductModel, int)>) tuple = (sum, id, _productList);
2024-07-23 20:29:10 +04:00
return View(tuple);
}
2024-06-01 05:00:05 +04:00
[HttpPost]
public void Payment(double sum, double paysum, int id) {
2024-07-23 20:29:10 +04:00
if (APIClient.Client == null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
2024-06-01 05:00:05 +04:00
if (paysum <= 0) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0");
}
if (paysum > sum) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
APIClient.PostRequest("api/client/createpaymeant", new PaymeantBindingModel {
OrderID = id,
2024-06-01 05:23:37 +04:00
SumPayment = paysum,
PayOption = PayOptionCalc(sum, paysum),
2024-06-06 17:21:39 +04:00
ClientID = APIClient.Client.ID,
DatePaymeant = DateTime.Now,
2024-06-01 05:00:05 +04:00
});
Response.Redirect("Index");
2024-07-23 20:29:10 +04:00
}
2024-06-01 05:00:05 +04:00
[HttpPost]
public PaymeantOption PayOptionCalc(double sum, double paysum) {
if (paysum < sum) {
return PaymeantOption.<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>_<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
}
else if (paysum == sum) {
return PaymeantOption.<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>_<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
}
else {
return PaymeantOption.<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
}
}
2024-06-01 06:50:29 +04:00
[HttpPost]
2024-07-23 20:29:10 +04:00
public double Calc(int count, int product) {
var _product = APIClient.GetRequset<ProductViewModel>($"api/main/getproduct?_productid={product}");
return count * (_product?.Price ?? 1);
}
2024-06-02 09:03:59 +04:00
[HttpGet]
2024-06-06 17:21:39 +04:00
public IActionResult Report() {
if (APIClient.Client == null) {
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
2024-07-23 20:29:10 +04:00
public void Report(DateTime DateFrom, DateTime DateTo){
2024-06-06 17:21:39 +04:00
if (DateTo == DateTime.MinValue || DateFrom > DateTo) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
2024-07-24 09:53:21 +04:00
Response.Redirect($"ReportSearch?_datefrom={DateFrom}&_dateto={DateTo + DateTime.Now.TimeOfDay}");
2024-06-06 17:21:39 +04:00
}
[HttpGet]
2024-07-23 20:29:10 +04:00
public IActionResult ReportSearch(string _datefrom, string _dateto) {
2024-06-06 17:21:39 +04:00
var reports = APIClient.GetRequset<List<PaymeantViewModel>>($"api/client/getreport?_start={_datefrom}&_end={_dateto}");
2024-07-23 20:29:10 +04:00
(DateTime, DateTime, List<PaymeantViewModel>?) tuple = (DateTime.Parse(_datefrom), DateTime.Parse(_dateto), reports);
2024-06-06 17:21:39 +04:00
return View(tuple);
2024-06-02 09:03:59 +04:00
}
2024-07-02 17:28:23 +04:00
2024-07-09 19:40:24 +04:00
[HttpGet]
public IActionResult CreateExcelReport() {
2024-07-24 09:53:21 +04:00
var fileMemStream = APIClient.GetRequset<byte[]>($"api/Client/CreateXlsxReport?_clientID={APIClient.Client?.ID}");
2024-07-10 18:32:46 +04:00
if (fileMemStream == null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
2024-07-09 19:40:24 +04:00
return File(fileMemStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx");
2024-07-23 20:29:10 +04:00
}
2024-07-10 18:32:46 +04:00
[HttpGet]
public IActionResult CreateWordReport() {
2024-07-24 09:53:21 +04:00
var fileMemStream = APIClient.GetRequset<byte[]>($"api/client/CreateDocxReport?_clientID={APIClient.Client?.ID}");
2024-07-10 18:32:46 +04:00
if (fileMemStream == null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
return File(fileMemStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Report.docx");
}
2024-07-23 20:29:10 +04:00
[HttpGet]
public IActionResult CreatePdfReport(string DateFrom, string DateTo) {
APIClient.PostRequest("api/client/SendReportMail", new ReportBindingModel {
2024-07-24 09:53:21 +04:00
ClientEmail = APIClient.Client?.Email ?? throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"),
2024-07-23 20:29:10 +04:00
DateFrom = DateTime.Parse(DateFrom),
DateTo = DateTime.Parse(DateTo)
});
return View("Report");
}
2024-07-02 17:28:23 +04:00
}
}