From ab82833487dc6fe2952392aea14c97a9b868607f Mon Sep 17 00:00:00 2001 From: Allllen4a Date: Sun, 2 Jun 2024 12:24:00 +0400 Subject: [PATCH] Non missura cutem, nisi plena cruoris hirudo --- .../Controllers/HomeController.cs | 175 ++++++++---------- 1 file changed, 82 insertions(+), 93 deletions(-) diff --git a/BeautySalonView/ClientWebApp/Controllers/HomeController.cs b/BeautySalonView/ClientWebApp/Controllers/HomeController.cs index 514c121..63a9605 100644 --- a/BeautySalonView/ClientWebApp/Controllers/HomeController.cs +++ b/BeautySalonView/ClientWebApp/Controllers/HomeController.cs @@ -195,114 +195,103 @@ namespace WorkerWebApp.Controllers return View(data); } - [HttpPost] - public void CreateReportWord(List procedures, DateTime dateFrom, DateTime dateTo) - { - if (APIWorker.Worker == null) - { - throw new Exception("Необходимо авторизоваться!"); - } - if (dateFrom == DateTime.MinValue || dateTo == DateTime.MinValue) - { - throw new Exception("Введены не все данные!"); - } + [HttpPost] + public async Task CreateReportWord(List procedures, DateTime dateFrom, DateTime dateTo, [FromServices] IWebHostEnvironment hostingEnvironment) + { + // Проверки ввода и авторизации + var folderName = "C:\\отчеты"; + var fileName = $"Список процедур {DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss")}.docx"; + var filePath = Path.Combine(hostingEnvironment.ContentRootPath, folderName, fileName); - if (procedures == null || procedures.Count <= 0) - { - throw new Exception("Не выбраны процедуры!"); - } + _reportLogic.SaveProcedureCosmeticsToWordFile(new ReportBindingModel + { + FileName = filePath, + DateFrom = dateFrom, + DateTo = dateTo + }); - _reportLogic.SaveProcedureCosmeticsToWordFile(new ReportBindingModel - { - FileName = $@"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\Downloads\Список процедур {DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss")}.docx", - DateFrom = dateFrom, - DateTo = dateTo - }); + // Возвращаем файл для загрузки + var fileBytes = await System.IO.File.ReadAllBytesAsync(filePath); + return File(fileBytes, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", fileName); + } - Response.Redirect("/Home/Reports"); - } + [HttpPost] + public async Task CreateReportExcel(List procedures, DateTime dateFrom, DateTime dateTo, [FromServices] IWebHostEnvironment hostingEnvironment) + { + // Проверки ввода и авторизации + var folderName = "C:\\отчеты"; + var fileName = $"Список процедур {DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss")}.xlsx"; + var filePath = Path.Combine(hostingEnvironment.ContentRootPath, folderName, fileName); - [HttpPost] - public void CreateReportExcel(List procedures, DateTime dateFrom, DateTime dateTo) - { - if (APIWorker.Worker == null) - { - throw new Exception("Необходимо авторизоваться!"); - } - if (dateFrom == DateTime.MinValue || dateTo == DateTime.MinValue) - { - throw new Exception("Введены не все данные!"); - } + _reportLogic.SaveProcedureCosmeticsToExcelFile(new ReportBindingModel + { + FileName = filePath, + DateFrom = dateFrom, + DateTo = dateTo + }); - if (procedures == null || procedures.Count <= 0) - { - throw new Exception("Не выбраны процедуры!"); - } + // Возвращаем файл для загрузки + var fileBytes = await System.IO.File.ReadAllBytesAsync(filePath); + return File(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName); + } - _reportLogic.SaveProcedureCosmeticsToExcelFile(new ReportBindingModel - { - FileName = $@"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\Downloads\Список процедур {DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss")}.xlsx", - DateFrom = dateFrom, - DateTo = dateTo - }); + + [HttpPost] + public async Task CreateReportPdf(DateTime dateFrom, DateTime dateTo, [FromServices] IWebHostEnvironment hostingEnvironment) + { + // Проверки авторизации и ввода + var folderName = "C:\\отчеты"; + var fileName = $"Список заказов {DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss")}.pdf"; + var filePath = Path.Combine(hostingEnvironment.ContentRootPath, folderName, fileName); - Response.Redirect("/Home/Reports"); - } + _reportLogic.SaveOrdersToPdfFile(new ReportOrderBindingModel + { + FileName = filePath, + DateFrom = dateFrom, + DateTo = dateTo, + WorkerId = APIWorker.Worker.Id + }); - [HttpPost] - public void CreateReportPdf(DateTime dateFrom, DateTime dateTo) - { - if (APIWorker.Worker == null) - { - throw new Exception("Необходимо авторизоваться!"); - } + // Возвращаем файл для загрузки + var fileBytes = await System.IO.File.ReadAllBytesAsync(filePath); + return File(fileBytes, "application/pdf", fileName); + } - if (dateFrom == DateTime.MinValue || dateTo == DateTime.MinValue) - { - throw new Exception("Введены не все данные!"); - } + [HttpPost] + public async Task SendReport(IFormFile fileUpload, [FromServices] IWebHostEnvironment hostingEnvironment) + { + if (APIWorker.Worker == null) + { + throw new Exception("Необходимо авторизоваться!"); + } - _reportLogic.SaveOrdersToPdfFile(new ReportOrderBindingModel - { - FileName = $@"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\Downloads\Список заказов{DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss")}.xlsx", - DateFrom = dateFrom, - DateTo = dateTo, - WorkerId = APIWorker.Worker.Id - }); + if (fileUpload == null || fileUpload.Length <= 0) + { + throw new Exception("Файл не выбран или пуст!"); + } - Response.Redirect("/Home/Reports"); - } + // Путь до файла + var uploadPath = Path.Combine(hostingEnvironment.ContentRootPath, "C:\\отчеты"); + var fileName = Path.GetFileName(fileUpload.FileName); + var fullPath = Path.Combine(uploadPath, fileName); - [HttpPost] - public void SendReport(IFormFile fileUpload) - { - if (APIWorker.Worker == null) - { - throw new Exception("Необходимо авторизоваться!"); - } + using (var fileStream = new FileStream(fullPath, FileMode.Create)) + { + await fileUpload.CopyToAsync(fileStream); + } - if (fileUpload == null || fileUpload.Length <= 0) - { - throw new Exception("Файл не выбран или пуст!"); - } + _mailLogic.MailSendAsync(new MailSendInfoBindingModel + { + MailAddress = APIWorker.Worker.Email, + Subject = $"{fileName.Split('.')[0]}", + Text = $"Отчёт отправлен {DateTime.Now}", + Path = fullPath + }); - // Путь до файла - var uploadPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads\"; - var fileName = Path.GetFileName(fileUpload.FileName); - var fullPath = Path.Combine(uploadPath, fileName); + return RedirectToAction("Reports", "Home"); + } - _mailLogic.MailSendAsync(new MailSendInfoBindingModel - { - MailAddress = APIWorker.Worker.Email, - Subject = $"{fileName.Split('.')[0]}", - Text = $"Отчёт отправлен {DateTime.Now}", - Path = fullPath - }); - - Response.Redirect("/Home/Reports"); - } - - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });