PIbd-21_Anisin_R.S._CourseW.../TravelAgency/TravelAgencyWebApp/Controllers/HomeController.cs

318 lines
9.6 KiB
C#
Raw Normal View History

2024-04-30 18:42:41 +04:00
using TravelAgencyContracts.BindingModels;
using TravelAgencyContracts.ViewModels;
using TravelAgencyWebApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
2024-05-28 18:32:51 +04:00
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;
2024-04-30 18:42:41 +04:00
namespace TravelAgencyWebApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
2024-05-28 18:32:51 +04:00
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<HomeController> logger, IUserLogic userLogic, IReportLogic reportLogic, ITourLogic tourLogic, IGuideLogic guideLogic, AbstractSaveToPdf pdfLogic, AbstractMailWorker mailLogic)
2024-04-30 18:42:41 +04:00
{
_logger = logger;
2024-05-28 18:32:51 +04:00
_userLogic = userLogic;
_reportLogic = reportLogic;
_tourLogic = tourLogic;
_guideLogic = guideLogic;
_pdfLogic = pdfLogic;
_mailLogic = mailLogic;
}
2024-04-30 18:42:41 +04:00
public IActionResult Index()
{
2024-05-28 18:32:51 +04:00
if (LoggedinUser.User == null)
{
return Redirect("~/Home/Enter");
}
return View(LoggedinUser.User);
2024-04-30 18:42:41 +04:00
}
[HttpGet]
public IActionResult Privacy()
{
if (LoggedinUser.User == null)
{
return Redirect("~/Home/Enter");
}
return View(LoggedinUser.User);
}
[HttpPost]
public void Privacy(string userFIO, string email, string phoneNumber, string password)
{
if (LoggedinUser.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(userFIO) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(phoneNumber) || string.IsNullOrEmpty(password))
{
throw new Exception("Введены не все данные!");
}
2024-04-30 18:42:41 +04:00
_userLogic.Update(new UserBindingModel
{
Id = LoggedinUser.User.Id,
UserFIO = userFIO,
Email = email,
PhoneNumber = phoneNumber,
Password = password
});
LoggedinUser.User.UserFIO = userFIO;
LoggedinUser.User.Email = email;
LoggedinUser.User.PhoneNumber = phoneNumber;
LoggedinUser.User.Password = password;
Response.Redirect("Privacy");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
2024-04-30 18:42:41 +04:00
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
2024-05-28 18:32:51 +04:00
2024-04-30 18:42:41 +04:00
[HttpGet]
2024-05-28 18:32:51 +04:00
public IActionResult Enter()
2024-04-30 18:42:41 +04:00
{
2024-05-28 18:32:51 +04:00
if (LoggedinUser.User != null)
{
throw new Exception("Вы уже авторизовались!");
}
2024-04-30 18:42:41 +04:00
return View();
}
2024-05-28 18:32:51 +04:00
[HttpPost]
public void Enter(string email, string password)
2024-04-30 18:42:41 +04:00
{
2024-05-28 18:32:51 +04:00
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");
2024-04-30 18:42:41 +04:00
}
2024-05-28 18:32:51 +04:00
2024-04-30 18:42:41 +04:00
[HttpGet]
2024-05-28 18:32:51 +04:00
public IActionResult Register()
2024-04-30 18:42:41 +04:00
{
2024-05-28 18:32:51 +04:00
if (LoggedinUser.User != null)
{
throw new Exception("Вы уже зарегистрировались!");
}
2024-04-30 18:42:41 +04:00
return View();
}
2024-05-28 18:32:51 +04:00
[HttpPost]
public void Register(string fio, string email, string password, string phone)
2024-04-30 18:42:41 +04:00
{
2024-05-28 18:32:51 +04:00
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");
2024-04-30 18:42:41 +04:00
}
2024-05-28 18:32:51 +04:00
public void Logout()
2024-04-30 18:42:41 +04:00
{
2024-05-28 18:32:51 +04:00
if (LoggedinUser.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
LoggedinUser.User = null;
Response.Redirect("Enter");
2024-04-30 18:42:41 +04:00
}
2024-05-28 18:32:51 +04:00
[HttpGet]
public IActionResult ReportMenu()
{
if (LoggedinUser.User == null)
{
return Redirect("~/Home/Enter");
}
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<int> 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 });
}
2024-04-30 18:42:41 +04:00
}
}