CourseWork_CompShop/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs

604 lines
22 KiB
C#
Raw Permalink Normal View History

using ComputerShopClientApp.Models;
using ComputerShopContracts.BindingModels;
2023-05-24 21:14:10 +04:00
using ComputerShopContracts.BusinessLogicContracts;
2023-05-17 13:52:06 +04:00
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels;
2023-05-25 09:17:38 +04:00
using ComputerShopDataModels.Enums;
2023-05-20 07:41:13 +04:00
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
2023-05-17 15:27:57 +04:00
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
2023-05-20 07:41:13 +04:00
using System.ComponentModel;
using System.Diagnostics;
namespace ComputerShopClientApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
2023-05-24 21:14:10 +04:00
private readonly IReportLogic _report;
2023-05-24 21:14:10 +04:00
public HomeController(ILogger<HomeController> logger, IReportLogic report)
{
_logger = logger;
2023-05-24 21:14:10 +04:00
_report = report;
}
public IActionResult Index()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<PurchaseViewModel>>($"api/main/getpurchases?clientId={APIClient.Client.Id}"));
}
public IActionResult Component()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
2023-05-17 13:52:06 +04:00
return View(APIClient.GetRequest<List<ComponentViewModel>>($"api/component/getcomponentlist?clientId={APIClient.Client.Id}"));
}
2023-04-09 11:30:54 +04:00
public IActionResult Assembly()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
2023-05-17 12:19:00 +04:00
return View(APIClient.GetRequest<List<AssemblyViewModel>>($"api/Assembly/getassemblylist?clientId={APIClient.Client.Id}"));
2023-04-09 11:30:54 +04:00
}
2023-05-18 22:18:40 +04:00
public IActionResult Report()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
public IActionResult ReportDocXml()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<ComponentViewModel>>($"api/Component/getcomponentlist?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 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 });
}
2023-05-18 22:18:40 +04:00
[HttpPost]
2023-05-19 19:22:24 +04:00
public void ReportDocXml(int[] Ids, string type)
2023-05-18 22:18:40 +04:00
{
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (Ids.Length <= 0)
{
throw new Exception("Количество должно быть больше 0");
}
if (string.IsNullOrEmpty(type))
{
throw new Exception("Неверный тип отчета");
}
List<int> res = new List<int>();
foreach (var item in Ids)
{
res.Add(item);
}
if (type == "docx")
{
APIClient.PostRequest("api/report/createdocreport", new ReportBindingModel
{
Ids = res,
FileName = "F:\\ReportsCourseWork\\wordfile.docx"
});
Response.Redirect("GetWordFile");
}
else
{
APIClient.PostRequest("api/report/createxmlreport", new ReportBindingModel
{
Ids = res,
FileName = "F:\\ReportsCourseWork\\excelfile.xlsx"
});
Response.Redirect("GetExcelFile");
}
}
2023-05-24 21:14:10 +04:00
[HttpGet]
public IActionResult ReportPdf()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View("ReportPdf");
}
[HttpGet]
public string GetPurchasesReport(DateTime dateFrom, DateTime dateTo)
{
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
List<ReportPurchaseSupplyViewModel> result;
try
{
result = _report.GetPurchaseSupply(new ReportBindingModel
{
DateFrom = dateFrom,
DateTo = dateTo
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания отчета");
throw;
}
double sum = 0;
string table = "";
table += $"<h2 class=\"u-text u-text-custom-color-1 u-text-default u-text-1\">Предварительный отчет</h2>";
table += $"<table class=\"u-table-entity\">";
table += "<colgroup>";
table += "<col width=\"20%\" />";
table += "<col width=\"20%\" />";
table += "<col width=\"20%\" />";
table += "<col width=\"20%\" />";
table += "<col width=\"20%\" />";
table += "</colgroup>";
table += "<thead class=\"u-custom-color-1 u-table-header u-table-header-1\">";
table += "<tr style=\"height: 31px\">";
table += $"<th class=\"u-border-1 u-border-grey-50 u-table-cell\">ID закупки</th>";
table += $"<th class=\"u-border-1 u-border-grey-50 u-table-cell\">Название компонента</th>";
table += $"<th class=\"u-border-1 u-border-grey-50 u-table-cell\">Дата создание закупки</th>";
table += $"<th class=\"u-border-1 u-border-grey-50 u-table-cell\">Дата создания поставки</th>";
table += $"<th class=\"u-border-1 u-border-grey-50 u-table-cell\">Статус поставки</th>";
table += "</tr>";
table += "</thead>";
foreach (var report in result)
{
table += "<tbody class=\"u-table-body\">";
table += "<tr style=\"height: 75px\">";
table += $"<td class=\"u-border-1 u-border-grey-40 u-border-no-left u-border-no-right u-table-cell\">{report.Purchase.Id.ToString()}</td>";
table += $"<td class=\"u-border-1 u-border-grey-40 u-border-no-left u-border-no-right u-table-cell\">{report.Purchase.ComponentName}</td>";
table += $"<td class=\"u-border-1 u-border-grey-40 u-border-no-left u-border-no-right u-table-cell\">{report.Purchase.DateCreate.ToShortDateString()}</td>";
table += $"<td class=\"u-border-1 u-border-grey-40 u-border-no-left u-border-no-right u-table-cell\">{report.Supply.DateCreate.ToShortDateString()}</td>";
2023-05-25 09:17:38 +04:00
table += $"<td class=\"u-border-1 u-border-grey-40 u-border-no-left u-border-no-right u-table-cell\">{Enum.GetName(typeof(SupplyStatus), report.Supply.Status)}</td>";
2023-05-24 21:14:10 +04:00
table += "</tr>";
table += "</tbody>";
}
table += "</table>";
return table;
}
[HttpPost]
public void ReportPdf(DateTime dateFrom, DateTime dateTo, string clientEmail)
{
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
2023-05-24 21:41:02 +04:00
if (string.IsNullOrEmpty(clientEmail))
{
throw new Exception("Email пуст");
}
2023-05-24 21:14:10 +04:00
APIClient.PostRequest("api/report/CreatePdfReport", new ReportBindingModel
{
DateFrom = dateFrom,
DateTo = dateTo,
});
2023-05-24 21:41:02 +04:00
APIClient.PostRequest("api/report/SendPdfToMail", new MailSendInfoBindingModel
{
MailAddress = clientEmail,
Subject = "Отчет по закупкам (pdf)",
Text = "Отчет по закупкам с " + dateFrom.ToShortDateString() + " до " + dateTo.ToShortDateString()
});
2023-05-24 21:14:10 +04:00
Response.Redirect("GetPdfFile");
}
2023-05-20 07:41:13 +04:00
[HttpGet]
public IActionResult Enter()
{
return View();
}
2023-05-19 19:38:08 +04:00
[HttpGet]
public IActionResult GetWordFile()
{
return new PhysicalFileResult("F:\\ReportsCourseWork\\wordfile.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
}
public IActionResult GetExcelFile()
{
return new PhysicalFileResult("F:\\ReportsCourseWork\\excelfile.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
2023-05-20 07:41:13 +04:00
[HttpPost]
public void SetInWork(int id)
{
2023-05-20 07:41:13 +04:00
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
APIClient.PostRequest($"api/main/setinwork?id={id}", id);
Response.Redirect("Index");
}
[HttpPost]
public void SetReady(int id)
{
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
APIClient.PostRequest($"api/main/setready?id={id}", id);
Response.Redirect("Index");
}
[HttpPost]
public void SetDone(int id)
{
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
APIClient.PostRequest($"api/main/setdone?id={id}", id);
Response.Redirect("Index");
}
[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 CreatePurchase()
{
2023-05-17 13:52:06 +04:00
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>($"api/main/getcomponentlist?clientId={APIClient.Client.Id}");
return View();
}
[HttpPost]
public void CreatePurchase(int component, int count)
{
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (count <= 0)
{
throw new Exception("Количество и сумма должны быть больше 0");
}
APIClient.PostRequest("api/main/createpurchase", new PurchaseBindingModel
{
ClientId = APIClient.Client.Id,
ComponentId = component,
Count = count,
Sum = Calc(count, component)
});
Response.Redirect("Index");
}
[HttpGet]
public IActionResult CreateComponent()
{
return View();
}
[HttpPost]
public void CreateComponent(string name, int cost)
{
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (cost <= 0)
{
throw new Exception("Цена должна быть больше 0");
}
APIClient.PostRequest("api/component/createcomponent", new ComponentBindingModel
{
ClientId = APIClient.Client.Id,
ComponentName = name,
Cost = cost
});
Response.Redirect("Index");
}
2023-04-09 12:04:14 +04:00
[HttpGet]
public IActionResult DeleteComponent()
{
2023-05-17 13:52:06 +04:00
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>($"api/main/getcomponentlist?clientId={APIClient.Client.Id}");
2023-04-09 12:04:14 +04:00
return View();
}
2023-05-17 17:28:31 +04:00
[HttpPost]
2023-04-09 12:04:14 +04:00
public void DeleteComponent(int component)
{
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
APIClient.PostRequest("api/component/deletecomponent", new ComponentBindingModel
{
Id = component
});
2023-05-17 17:28:31 +04:00
Response.Redirect("Component");
2023-04-09 12:04:14 +04:00
}
2023-04-09 11:30:54 +04:00
[HttpGet]
public IActionResult CreateAssembly()
{
2023-05-17 13:52:06 +04:00
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>($"api/component/getcomponentlist?clientId={APIClient.Client.Id}");
2023-04-09 11:30:54 +04:00
ViewBag.CurrentComponents = new List<ComponentViewModel>();
return View();
}
[HttpPost]
2023-05-17 12:19:00 +04:00
public void CreateAssembly(string name, int cost)
2023-04-09 11:30:54 +04:00
{
if (APIClient.Client == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
2023-05-17 12:19:00 +04:00
APIClient.PostRequest("api/assembly/createassembly", new AssemblyBindingModel
2023-04-09 11:30:54 +04:00
{
2023-05-17 12:19:00 +04:00
ClientId = APIClient.Client.Id,
AssemblyName = name,
Price = cost,
});
2023-04-09 11:30:54 +04:00
System.Diagnostics.Debug.WriteLine("it might work");
2023-05-17 12:19:00 +04:00
Response.Redirect("Assembly");
}
[HttpGet]
public IActionResult DeleteAssembly()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Assemblies = APIClient.GetRequest<List<AssemblyViewModel>>($"api/assembly/GetAssemblyList?clientId={APIClient.Client.Id}");
return View();
}
[HttpPost]
public void DeleteAssembly(int assembly)
{
if (APIClient.Client == null)
{
throw new Exception("Необходима авторизация");
}
APIClient.PostRequest("api/assembly/deleteassembly", new AssemblyBindingModel
{
Id = assembly
});
Response.Redirect("Assembly");
2023-04-09 11:30:54 +04:00
}
2023-05-17 13:52:06 +04:00
public IActionResult AddComponentToAssembly()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Assemblies = APIClient.GetRequest<List<AssemblyViewModel>>($"api/assembly/getassemblylist?clientId={APIClient.Client.Id}");
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>($"api/component/getcomponentlist?clientId={APIClient.Client.Id}");
return View();
}
[HttpPost]
public void AddComponentToAssembly(int assembly, int component, int amount)
{
if (APIClient.Client == null)
{
throw new Exception("Необходима авторизация");
}
APIClient.PostRequest("api/assembly/AddComponentToAssembly", Tuple.Create(
new AssemblySearchModel() { Id = assembly },
new ComponentSearchModel() { Id = component },
amount
));
Response.Redirect("Assembly");
}
2023-05-17 15:27:57 +04:00
public IActionResult EditAssembly()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Assemblies = APIClient.GetRequest<List<AssemblyViewModel>>($"api/assembly/getassemblylist?clientId={APIClient.Client.Id}");
return View();
}
[HttpPost]
public void EditAssembly(int assembly, string assemblyName, int price)
{
if (APIClient.Client == null)
{
throw new Exception("Необходима авторизация");
}
if (string.IsNullOrEmpty(assemblyName))
{
throw new Exception("Название не может быть пустым");
}
if (price < 0)
{
throw new Exception("Цена не может быть меньше нуля");
}
APIClient.PostRequest("api/assembly/editassembly", new AssemblyBindingModel
{
Id = assembly,
AssemblyName = assemblyName,
Price = price,
ClientId = APIClient.Client.Id
});
Response.Redirect("Assembly");
}
[HttpGet]
public Tuple<AssemblyViewModel, string>? GetAssembly(int assemblyId)
{
if (APIClient.Client == null)
{
throw new Exception("Необходима авторизация");
}
var result = APIClient.GetRequest<Tuple<AssemblyViewModel, List<Tuple<string, int>>>>($"api/assembly/getassembly?assemblyId={assemblyId}");
if (result == null)
{
return default;
}
string table = "";
for (int i = 0; i < result.Item2.Count; i++)
{
var componentName = result.Item2[i].Item1;
var componentAmount = result.Item2[i].Item2;
table += "<tr style=\"height: 44px\">";
table += $"<td class=\"u-border-1 u-border-grey-30 u-table-cell\">{componentName}</td>";
table += $"<td class=\"u-border-1 u-border-grey-30 u-table-cell\">{componentAmount}</td>";
table += "</tr>";
}
return Tuple.Create(result.Item1, table);
}
2023-04-09 11:30:54 +04:00
[HttpPost]
public double Calc(int count, int component)
{
var comp = APIClient.GetRequest<ComponentViewModel>($"api/main/getcomponent?componentId={component}");
return count * (comp?.Cost ?? 1);
}
2023-05-17 17:28:31 +04:00
public IActionResult EditComponent()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>($"api/component/getcomponentlist?clientId={APIClient.Client.Id}");
return View();
}
[HttpPost]
public void EditComponent(int component, string componentName, int price)
{
if (APIClient.Client == null)
{
throw new Exception("Необходима авторизация");
}
if (string.IsNullOrEmpty(componentName))
{
throw new Exception("Название не может быть пустым");
}
if (price < 0)
{
throw new Exception("Цена не может быть меньше нуля");
}
APIClient.PostRequest("api/component/editcomponent", new ComponentBindingModel
{
Id = component,
ComponentName = componentName,
Cost = price,
ClientId = APIClient.Client.Id
});
Response.Redirect("Assembly");
}
[HttpGet]
public ComponentViewModel? GetComponent(int componentId)
{
if (APIClient.Client == null)
{
throw new Exception("Необходима авторизация");
}
var result = APIClient.GetRequest<ComponentViewModel>($"api/component/getcomponent?componentId={componentId}");
if (result == null)
{
return default;
}
return result;
}
}
}