using Microsoft.AspNetCore.Mvc; using ServiceStationContracts.BindingModels; using ServiceStationContracts.BusinessLogicsContracts; using ServiceStationContracts.SearchModels; using ServiceStationContracts.ViewModels; using ServiceStationExecutorApp.Models; using System.Collections.Generic; using System.Diagnostics; namespace ServiceStationExecutorApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; private readonly IExecutorReportLogic _report; public HomeController(ILogger logger, IExecutorReportLogic executorReportLogic) { _logger = logger; _report = executorReportLogic; } public IActionResult Index() { return View(); } public IActionResult Privacy() { if(APIExecutor.Executor == null) { return RedirectToAction("Enter"); } return View(APIExecutor.Executor); } [HttpPost] public void Privacy(string email, string fio, string number, string password) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } if (string.IsNullOrEmpty(fio) || string.IsNullOrEmpty(number) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(email)) { throw new Exception("Введены не все данные"); } APIExecutor.PostRequest("api/executor/updateexecutor", new ExecutorBindingModel { ExecutorEmail = email, ExecutorNumber = number, ExecutorFIO = fio, ExecutorPassword = password, Id = APIExecutor.Executor.Id }); Response.Redirect("Index"); } public IActionResult Enter() { return View(); } [HttpPost] public void Enter(string executorNumber, string password) { if(string.IsNullOrEmpty(executorNumber) || string.IsNullOrEmpty(password)) { throw new Exception("Не хватает пароля или номера."); } APIExecutor.Executor = APIExecutor.GetRequest($"api/executor/login?executorNumber={executorNumber}&password={password}"); if (APIExecutor.Executor == null) { throw new Exception("Неверный логин/пароль"); } Response.Redirect("Index"); } public IActionResult Register() { return View(); } [HttpPost] public void Register(string fio, string executorNumber, string password, string email) { if(string.IsNullOrEmpty(fio) || string.IsNullOrEmpty(executorNumber) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(email)) { throw new Exception("Введены не все данные"); } APIExecutor.PostRequest("api/executor/register", new ExecutorBindingModel { ExecutorNumber = executorNumber, ExecutorEmail = email, ExecutorFIO = fio, ExecutorPassword = password }); Response.Redirect("Enter"); return; } public IActionResult ListCars() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } return View(APIExecutor.GetRequest>($"api/main/getcarlist?executorId={APIExecutor.Executor.Id}")); } public IActionResult ListDefects() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } return View(APIExecutor.GetRequest>($"api/main/getdefectlist?executorId={APIExecutor.Executor.Id}")); } public IActionResult ListTechnicalWorks() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } return View(APIExecutor.GetRequest>($"api/main/gettechnicalworklist?executorId={APIExecutor.Executor.Id}")); } [HttpGet] public Tuple? GetDefect(int defectId) { if(APIExecutor.Executor == null) { throw new Exception("Необходима авторизация"); } var result = APIExecutor.GetRequest>>>($"api/main/getdefect?defectId={defectId}"); if (result == null) return default; string table = ""; for(int i = 0; i < result.Item2.Count; i++) { var carNumber = result.Item2[i].Item1; var carBrand = result.Item2[i].Item2; table += ""; table += $"{carNumber}"; table += $"{carBrand}"; table += ""; } return Tuple.Create(result.Item1, table); } [HttpGet] public Tuple? GetTechnicalWork(int technicalWorkId) { if(APIExecutor.Executor == null) { throw new Exception("Необходима авторизация"); } var result = APIExecutor.GetRequest>>>($"api/main/gettechnicalwork?technicalworkId={technicalWorkId}"); if (result == null) return default; string table = ""; for(int i = 0;i{carNumber}"; table += $"{carBrand}"; table += ""; } return Tuple.Create(result.Item1, table); } public IActionResult DeleteCar() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } ViewBag.Cars = APIExecutor.GetRequest>($"api/main/getcarlist?executorId={APIExecutor.Executor.Id}"); return View(); } [HttpPost] public void DeleteCar(int car) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } APIExecutor.PostRequest("api/main/deletecar", new CarBindingModel { Id = car }); Response.Redirect("ListCars"); } public IActionResult DeleteDefect() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } ViewBag.Defects = APIExecutor.GetRequest>($"api/main/getdefectlist?executorId={APIExecutor.Executor.Id}"); return View(); } [HttpPost] public void DeleteDefect(int defect) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } APIExecutor.PostRequest("api/main/deletedefect", new DefectBindingModel { Id = defect }); Response.Redirect("ListDefects"); } public IActionResult DeleteTechnicalWork() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } ViewBag.TechnicalWorks = APIExecutor.GetRequest>($"api/main/gettechnicalworklist?executorId={APIExecutor.Executor.Id}"); return View(); } [HttpPost] public void DeleteTechnicalWork(int technicalWork) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } APIExecutor.PostRequest("api/main/deletetechnicalwork", new TechnicalWorkBindingModel { Id = technicalWork }); Response.Redirect("ListTechnicalWorks"); } public IActionResult UpdateCar() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } ViewBag.Cars = APIExecutor.GetRequest>($"api/main/getcarlist?executorId={APIExecutor.Executor.Id}"); return View(); } [HttpPost] public void UpdateCar(int car, string carNumber, string carBrand) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } if (string.IsNullOrEmpty(carNumber)) { throw new Exception("Введите номер машины"); } if (string.IsNullOrEmpty(carBrand)) { throw new Exception("Введите бренд машины"); } APIExecutor.PostRequest("api/main/updatecar", new CarBindingModel { Id = car, CarNumber = carNumber, CarBrand = carBrand, ExecutorId = APIExecutor.Executor.Id }); Response.Redirect("ListCars"); } public IActionResult UpdateDefect() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } ViewBag.Defects = APIExecutor.GetRequest>($"api/main/getdefectlist?executorId={APIExecutor.Executor.Id}"); return View(); } [HttpPost] public void UpdateDefect(int defect, string DefectType, double DefectPrice) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } if (string.IsNullOrEmpty(DefectType)) { throw new Exception("Введите тип неисправности"); } if(DefectPrice < 100) { throw new Exception("Минимальная цена неисправности 100"); } APIExecutor.PostRequest("api/main/updatedefect", new DefectBindingModel { ExecutorId = APIExecutor.Executor.Id, Id = defect, DefectType = DefectType, DefectPrice = DefectPrice }); Response.Redirect("ListDefects"); } public IActionResult UpdateTechnicalWork() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } ViewBag.TechnicalWorks = APIExecutor.GetRequest>($"api/main/gettechnicalworklist?executorId={APIExecutor.Executor.Id}"); return View(); } [HttpPost] public void UpdateTechnicalWork(int technicalWork, string TechnicalWorkType, double TechnicalWorkPrice) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } if (string.IsNullOrEmpty(TechnicalWorkType)) { throw new Exception("Введите тип ТО"); } if(TechnicalWorkPrice < 100) { throw new Exception("Цена ТО должны быть больше 100"); } APIExecutor.PostRequest("api/main/updatetechnicalwork", new TechnicalWorkBindingModel { Id = technicalWork, WorkType = TechnicalWorkType, ExecutorId = APIExecutor.Executor.Id, WorkPrice = TechnicalWorkPrice, }); Response.Redirect("ListTechnicalWorks"); } public IActionResult CreateCar() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } return View(); } [HttpPost] public void CreateCar(string CarNumber, string CarBrand) { if(APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } APIExecutor.PostRequest("api/main/createcar", new CarBindingModel { ExecutorId = APIExecutor.Executor.Id, CarNumber = CarNumber, CarBrand = CarBrand }); Response.Redirect("ListCars"); } public IActionResult CreateDefect() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } return View(); } [HttpPost] public void CreateDefect(string defectType, double defectPrice) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } APIExecutor.PostRequest("api/main/createdefect", new DefectBindingModel { ExecutorId = APIExecutor.Executor.Id, DefectType = defectType, DefectPrice = defectPrice }); Response.Redirect("ListDefects"); } public IActionResult CreateTechnicalWork() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } return View(); } [HttpPost] public void CreateTechnicalWork(string WorkType, double WorkPrice) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } APIExecutor.PostRequest("api/main/createtechnicalwork", new TechnicalWorkBindingModel { ExecutorId = APIExecutor.Executor.Id, WorkType = WorkType, WorkPrice = WorkPrice, DateStartWork = DateTime.Now }); Response.Redirect("ListTechnicalWorks"); } public IActionResult AddCarToDefect() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } return View(Tuple.Create(APIExecutor.GetRequest>($"api/main/getdefectlist?executorId={APIExecutor.Executor.Id}"), APIExecutor.GetRequest>($"api/main/getcarlist?executorId={APIExecutor.Executor.Id}"))); } [HttpPost] public void AddCarToDefect(int defect, int[] car) { if(APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } APIExecutor.PostRequest("api/main/addcartodefect", Tuple.Create( new DefectSearchModel() { Id = defect }, car )); Response.Redirect("ListDefects"); } public IActionResult AddCarToTechnicalWork() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } return View(Tuple.Create(APIExecutor.GetRequest>($"api/main/gettechnicalworklist?executorId={APIExecutor.Executor.Id}"), APIExecutor.GetRequest>($"api/main/getcarlist?executorId={APIExecutor.Executor.Id}"))); } [HttpPost] public void AddCarToTechnicalWork(int technicalWork, int[] car) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } APIExecutor.PostRequest("api/main/addcartotechnicalwork", Tuple.Create( new TechnicalWorkSearchModel() { Id = technicalWork }, car )); Response.Redirect("ListTechnicalWorks"); } public IActionResult BindingTechnicalWorkToWork() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } return View(Tuple.Create(APIExecutor.GetRequest>($"api/main/gettechnicalworklist?executorId={APIExecutor.Executor.Id}"), APIExecutor.GetRequest>("api/main/getworks"))); } [HttpPost] public void BindingTechnicalWorkToWork(int technicalWork, int work) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } APIExecutor.PostRequest("api/main/updatework", new WorkBindingModel { Id = work, TechnicalWorkId = technicalWork }); } [HttpGet] public IActionResult ListWorkToFile() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } return View(APIExecutor.GetRequest>($"api/main/getcarlist?executorId={APIExecutor.Executor.Id}")); } [HttpPost] public void ListWorkToFile(int[] Ids, string type) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } if(Ids.Length <= 0) { throw new Exception("Кол-во меньше нуля"); } if(string.IsNullOrEmpty(type)) { throw new Exception("Неопознанный тип"); } List res = new List(); foreach(var id in Ids) { res.Add(id); } if(type == "docx") { APIExecutor.PostRequest("api/report/createexecutorreporttoword", new ReportExecutorBindingModel { Ids = res, FileName = Directory.GetCurrentDirectory() + "\\Reports\\wordfile.docx" }); Response.Redirect("GetWordFile"); } else { APIExecutor.PostRequest("api/report/createexecutorreporttoexcel", new ReportExecutorBindingModel { Ids = res, FileName = Directory.GetCurrentDirectory() + "\\Reports\\excelfile.xlsx" }); Response.Redirect("GetExcelFile"); } } public IActionResult GetWordFile() { return new PhysicalFileResult(Directory.GetCurrentDirectory() + "\\Reports\\wordfile.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); } public IActionResult GetExcelFile() { return new PhysicalFileResult(Directory.GetCurrentDirectory() + "\\Reports\\excelfile.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); } public IActionResult GetPdfFile() { return new PhysicalFileResult(Directory.GetCurrentDirectory() + "\\Reports\\pdffile.pdf", "application/pdf"); } public IActionResult ListCarsToPdf() { if (APIExecutor.Executor == null) { return RedirectToAction("Enter"); } return View(); } [HttpPost] public void ListCarsToPdf(DateTime dateFrom, DateTime dateTo, string executorEmail) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } if (string.IsNullOrEmpty(executorEmail)) { throw new Exception("Email пуст"); } APIExecutor.PostRequest("api/report/createexecutorreporttopdf", new ReportExecutorBindingModel { FileName = Directory.GetCurrentDirectory() + "\\Reports\\pdffile.pdf", DateFrom = dateFrom, DateTo = dateTo, ExecutorId = APIExecutor.Executor.Id, }); Response.Redirect("GetPdfFile"); } [HttpGet] public string GetCarsReport(DateTime dateFrom, DateTime dateTo) { if (APIExecutor.Executor == null) { throw new Exception("Авторизироваться не забыли?"); } List cars; try { cars = _report.GetCars(new ReportExecutorBindingModel { ExecutorId = APIExecutor.Executor.Id, DateFrom = dateFrom, DateTo = dateTo, }); } catch (Exception ex) { _logger.LogError(ex, "Ошибка создания отчета"); throw; } string table = ""; table += "

Предварительный отчет

"; table += ""; table += ""; table += ""; table += ""; table += ""; table += ""; table += ""; table += ""; table += ""; table += ""; table += ""; table += ""; foreach(var car in cars) { bool isRepair = true; if (car.RepairPrice == 0) { isRepair = false; } table += ""; table += ""; table += $""; table += $""; table += $""; table += $""; table += $""; table += $""; table += $""; table += ""; table += ""; } table += "
Номер машиныМарка машиныТип ТОДата ТОЦена ТОНазвание ремонтаЦена ремонта
{car.CarNumber}{car.CarBrand}{car.WorkType}{(isRepair ? string.Empty : car.TechnicalWorkDate)}{(isRepair ? string.Empty : car.TechnicalWorkPrice)}{(isRepair ? car.RepairName : string.Empty)}{(isRepair ? car.RepairPrice : string.Empty)}
"; return table; } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } } }