using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Formatters; using Newtonsoft.Json; using ServiceSourceClientApp.Models; using ServiceSourceExecutorApp; using ServiceStationContracts.BindingModels; using ServiceStationContracts.HelperModels; using ServiceStationContracts.ViewModels; using System.Diagnostics; namespace ServiceSourceClientApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } [HttpGet] public IActionResult Index() { if (APIClient.executor == null) { return Redirect("~/Home/Enter"); } var view = APIClient.GetRequest>($"api/Main/GetWorks?_executorId={APIClient.executor.Id}"); return View(view); } [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.executor = APIClient.GetRequest($"api/main/Login?email={email}&password={password}"); if (APIClient.executor == 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/Main/Register", new ExecutorBindingModel { Email = email, Password = password, FIO = fio }); Response.Redirect("Enter"); } [HttpGet] public IActionResult Privacy() { if (APIClient.executor == null) { return Redirect("~/Home/Enter"); } return View(APIClient.executor); } [HttpPost] public void Privacy(string email, string password, string fio) { if (APIClient.executor == null) { Response.Redirect("~/Home/Enter"); return; } if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) { throw new Exception("Введите почту, пароль и ФИО"); } APIClient.PostRequest("api/Main/UpdateData", new ExecutorBindingModel { Id = APIClient.executor.Id, Email = email, Password = password, FIO = fio }); APIClient.executor.FIO = fio; APIClient.executor.Email = email; APIClient.executor.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 CreateWork() { ViewBag.Tasks = APIClient.GetRequest>($"api/Main/GetTasks?"); return View(APIClient.GetRequest>($"api/Main/GetClients?")); } [HttpPost] public void CreateWork(int[] ids, int task) { if (APIClient.executor == null) { Response.Redirect("~/Home/Enter"); return; } APIClient.PostRequest("api/Main/CreateWork", new WorkFromWebBindingModel { Price = Calc(task, ids.Length), ExecutorId = APIClient.executor.Id, TaskId = task, client_ids = ids }); } [HttpGet] public IActionResult EditWork(int Id) { if (Id == 0) { return RedirectToAction("Index"); } var work_info = APIClient.GetRequest>>($"api/Main/GetWork?_workId={Id}") ?? throw new Exception("Ошибка получения данных"); ViewBag.Tasks = APIClient.GetRequest>($"api/Main/GetTasks?"); int task_load_id = JsonConvert.DeserializeObject(work_info[0][0]); var task_load = APIClient.GetRequest($"api/Main/GetTask?_taskId={task_load_id}") ?? throw new Exception("Ошибка получения данных"); if (ViewBag.Tasks[0].Name != task_load.Name) { int index = 0; for (int i = 0; i < ViewBag.Tasks.Count; i++) { if (ViewBag.Tasks[i].Name == task_load.Name) { index = i; break; } } var tmp = ViewBag.Tasks[0]; ViewBag.Tasks[0] = task_load; ViewBag.Tasks[index] = tmp; } List clients_list = new(); foreach (var rec in work_info[1]) { var client = JsonConvert.DeserializeObject(rec) ?? throw new Exception("Ошибка десериализации"); clients_list.Add(client); } var clients = APIClient.GetRequest>($"api/Main/GetClients?") ?? throw new Exception("Ошибка получения данных"); IEnumerable result = clients.Except(clients_list); List new_client = new(); foreach (var client in result) { new_client.Add(client); } (int, List, List) tuple = (Id, clients_list, new_client); return View(tuple); } [HttpPost] public void EditWork(int[] ids, int task, int id) { if (APIClient.executor == null) { Response.Redirect("~/Home/Enter"); return; } APIClient.PostRequest("api/Main/UpdateWork", new WorkFromWebBindingModel { Id = id, Price = Calc(task, ids.Length), ExecutorId = APIClient.executor.Id, TaskId = task, client_ids = ids }); } [HttpPost] public double Calc(int task, int count) { var task_info = APIClient.GetRequest($"api/Main/GetTask?_taskId={task}") ?? throw new Exception("Ошибка получеия данных"); return task_info.Price + 700 * count; } [HttpGet] public IActionResult DeleteWork(int id) { APIClient.PostRequest("api/Main/DeleteWork", new WorkBindingModel { Id = id, }); return RedirectToAction("Index"); } [HttpGet] public IActionResult Scoring () { if (APIClient.executor == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public void Scoring(int client, int work, int points) { APIClient.PostRequest("api/Main/AddClientPoints", new WorkClientModel { Client_Id = client, Work_Id = work, Point_count = points }); Response.Redirect("Scoring"); } public JsonResult Client() { var clients = APIClient.GetRequest>($"api/Main/GetClients?") ?? throw new Exception("Ошибка получения данных"); return new JsonResult(clients); } public JsonResult Work(int id) { var work_clients_list = APIClient.GetRequest>($"api/Main/GetWorkClients?_clientId={id}") ?? throw new Exception("Ошибка полуения данных"); List works = new(); foreach (var record in work_clients_list) { int work_id = record.Work_Id; var work = APIClient.GetRequest($"api/Main/GetWorkView?_workId={work_id}") ?? throw new Exception("Ошибка получения данных"); works.Add(work); } return new JsonResult(works); } [HttpGet] public IActionResult Report() { if (APIClient.executor == null) { return Redirect("~/Home/Enter"); } var view = APIClient.GetRequest>($"api/Main/GetWorks?_executorId={APIClient.executor.Id}"); return View(view); } [HttpPost] public void Report(DateTime DateFrom, DateTime DateTo) { if (DateTo == DateTime.MinValue || DateFrom > DateTo) { throw new Exception("Некоректно указан временной интервал"); } Response.Redirect($"ReportSearchDate?_datefrom={DateFrom}&_dateto={DateTo + DateTime.Now.TimeOfDay}"); } [HttpGet] public IActionResult ReportSearchDate(string _datefrom, string _dateto) { var reports = APIClient.GetRequest>($"api/Main/GerWorksDate?_start={_datefrom}&_end={_dateto}"); (DateTime, DateTime, List?) tuple = (DateTime.Parse(_datefrom), DateTime.Parse(_dateto), reports); return View(tuple); } [HttpGet] public IActionResult CreatePdfReport(string DateFrom, string DateTo) { APIClient.PostRequest("api/Main/SendReportPdfMail", new ReportBindeingModel { EmailAddress = APIClient.executor?.Email ?? throw new Exception("Ошибка получения адреса"), DateFrom = DateTime.Parse(DateFrom), DateTo = DateTime.Parse(DateTo) }); return Redirect("Report"); } [HttpGet] public IActionResult CreateWordReport(string ids) { if (string.IsNullOrEmpty(ids)) { throw new Exception("Нет выбраных записей"); } var fileMemStream = APIClient.GetRequest($"api/Main/CreateWordReport?_ids={ids}"); if (fileMemStream == null) { throw new Exception("Ошибка создания отчета"); } return File(fileMemStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Report.docx"); } [HttpGet] public IActionResult CreateExcelReport(string ids) { if (string.IsNullOrEmpty(ids)) { throw new Exception("Нет выбраных записей"); } var fileMemStream = APIClient.GetRequest($"api/Main/CreateXlsxReport?_ids={ids}"); if (fileMemStream == null) { throw new Exception("Ошибка создания отчета"); } return File(fileMemStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx"); } } }