using TravelAgencyContracts.BindingModels; using TravelAgencyContracts.ViewModels; using TravelAgencyWebApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using TravelAgencyContracts.BusinessLogicsContracts; using TravelAgencyContracts.SearchModels; using TravelAgencyBusinessLogic.BusinessLogics; using DocumentFormat.OpenXml.Spreadsheet; using TravelAgencyBusinessLogic.OfficePackage; using TravelAgencyBusinessLogic.MailWorker; using DocumentFormat.OpenXml.Wordprocessing; using TravelAgencyDatabaseImplement.Models; namespace TravelAgencyWebApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; private readonly IUserLogic _userLogic; private readonly IReportLogic _reportLogic; private readonly ITourLogic _tourLogic; private readonly IGuideLogic _guideLogic; private readonly AbstractSaveToPdf _pdfLogic; private readonly AbstractMailWorker _mailLogic; public HomeController(ILogger logger, IUserLogic userLogic, IReportLogic reportLogic, ITourLogic tourLogic, IGuideLogic guideLogic, AbstractSaveToPdf pdfLogic, AbstractMailWorker mailLogic) { _logger = logger; _userLogic = userLogic; _reportLogic = reportLogic; _tourLogic = tourLogic; _guideLogic = guideLogic; _pdfLogic = pdfLogic; _mailLogic = mailLogic; } public IActionResult Index() { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } return View(LoggedinUser.User); } [HttpGet] public IActionResult Privacy() { return View(); } [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() { if (LoggedinUser.User != null) { throw new Exception("Вы уже авторизовались!"); } return View(); } [HttpPost] public void Enter(string email, string password) { if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { throw new Exception("Введены не все данные!"); } LoggedinUser.User = _userLogic.ReadElement(new UserSearchModel { Email = email, Password = password }); if (LoggedinUser.User == null) { throw new Exception("Неверный логин/пароль"); } Response.Redirect("Index"); } [HttpGet] public IActionResult Register() { if (LoggedinUser.User != null) { throw new Exception("Вы уже зарегистрировались!"); } return View(); } [HttpPost] public void Register(string fio, string email, string password, string phone) { if (string.IsNullOrEmpty(fio) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { throw new Exception("Введены не все данные!"); } _userLogic.Create(new UserBindingModel { UserFIO = fio, Email = email, PhoneNumber = phone, Password = password }); Response.Redirect("Enter"); } public void Logout() { if (LoggedinUser.User == null) { throw new Exception("Необходимо авторизоваться!"); } LoggedinUser.User = null; Response.Redirect("Enter"); } [HttpGet] public IActionResult ReportMenu() { return View(); } [HttpGet] public IActionResult ReportTourPlace() { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Tours = _tourLogic.ReadList(new TourSearchModel { UserId = LoggedinUser.User.Id, }); return View(); } [HttpGet] public IActionResult ReportTourPeriod() { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public IActionResult ReportTourPeriod(DateTime dateFrom, DateTime dateTo) { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } if (dateFrom == DateTime.MinValue || dateTo == DateTime.MinValue) { throw new Exception("Введены не все данные!"); } ViewBag.DateFrom = dateFrom; ViewBag.DateTo = dateTo; return View(_reportLogic.GetTourPeriod(new ReportBindingModel { DateFrom = dateFrom, DateTo = dateTo, UserId = LoggedinUser.User.Id })); } [HttpPost] public IActionResult CreateReport(List tours, string type) { if (LoggedinUser.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (tours == null) { throw new Exception("Выберите туры!"); } if (type.Equals("docx")) { _reportLogic.SaveTourPlacesToWordFile(new ReportBindingModel { FileName = $@"C:\Users\User\Downloads\Список мест по турам{DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss")}.docx", UserId = LoggedinUser.User.Id, Tours = tours }); } else if (type.Equals("xlsx")) { _reportLogic.SaveTourPlacesToExcelFile(new ReportBindingModel { FileName = $@"C:\Users\User\Downloads\Список мест по турам{DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss")}.xlsx", UserId = LoggedinUser.User.Id, Tours = tours }); } ViewBag.Tours = _tourLogic.ReadList(new TourSearchModel { UserId = LoggedinUser.User.Id, }); return View("ReportTourPlace", tours); } [HttpPost] public void SentPdfToMail(DateTime dateFrom, DateTime dateTo) { if (LoggedinUser.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (dateFrom == DateTime.MinValue || dateTo == DateTime.MinValue) { throw new Exception("Введены не все данные!"); } using (MemoryStream memoryStream = new MemoryStream()) { SendMailReport(dateFrom, dateTo, memoryStream); } Response.Redirect("/Home/ReportTourPeriod"); } public void SendMailReport(DateTime? dateFrom, DateTime? dateTo, MemoryStream stream) { var tours = _reportLogic.GetTourPeriod(new ReportBindingModel { DateFrom = dateFrom, DateTo = dateTo, UserId = LoggedinUser.User.Id, }); if (tours == null) return; _pdfLogic.CreateDoc(new() { DateFrom = dateFrom!.Value, DateTo = dateTo!.Value, FileName = stream, Tours = tours, Title = "Отчет" }); byte[] report = stream.GetBuffer(); _mailLogic.MailSendAsync(new() { MailAddress = LoggedinUser.User.Email, Subject = "Отчет", FileName = "PdfReport.pdf", Pdf = report }); } } }