CourseWork_SchoolStudyAgain/SchoolAgainStudy/StudentWebClient/Controllers/HomeController.cs

502 lines
20 KiB
C#
Raw Normal View History

2023-05-18 23:25:22 +04:00
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
2023-05-18 23:25:22 +04:00
using SchoolAgainStudyBusinessLogic.MailWorker;
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.BusinessLogicContracts;
using SchoolAgainStudyContracts.SearchModel;
using SchoolAgainStudyContracts.ViewModel;
2023-05-18 00:03:47 +04:00
using SchoolAgainStudyDataBaseImplements.Models;
using SchoolAgainStudyDataModels.Models;
using StudentWebClient.Models;
using System.Diagnostics;
2023-05-18 12:56:00 +04:00
using System.IO;
namespace StudentWebClient.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly ITaskLogic _task;
private readonly IDiyLogic _diy;
private readonly IProductLogic _product;
private readonly IInterestLogic _interest;
private readonly IStudentLogic _student;
2023-05-18 20:23:52 +04:00
private readonly IReportLogic _report;
2023-05-18 23:25:22 +04:00
private readonly AbstractMailWorker mailSender;
2023-05-18 20:23:52 +04:00
public HomeController(ILogger<HomeController> logger, IDiyLogic diy,
2023-05-18 23:25:22 +04:00
IProductLogic product , ITaskLogic task , IInterestLogic interest, IStudentLogic student, IReportLogic report, AbstractMailWorker abstractMailWorker)
{
_logger = logger;
_diy = diy;
_product = product;
_task = task;
_interest = interest;
_student = student;
2023-05-18 20:23:52 +04:00
_report = report;
2023-05-18 23:25:22 +04:00
try
{
mailSender = abstractMailWorker;
mailSender?.MailConfig(new MailConfigBindingModel
{
MailLogin = System.Configuration.ConfigurationManager.AppSettings["MailLogin"] ?? string.Empty,
MailPassword = System.Configuration.ConfigurationManager.AppSettings["MailPassword"] ?? string.Empty,
SmtpClientHost = System.Configuration.ConfigurationManager.AppSettings["SmtpClientHost"] ?? string.Empty,
SmtpClientPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpClientPort"]),
PopHost = System.Configuration.ConfigurationManager.AppSettings["PopHost"] ?? string.Empty,
PopPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PopPort"])
});
}
catch (Exception ex)
{
logger?.LogError(ex, "Ошибка работы с почтой");
}
}
public IActionResult Index()
{
if (APIClient.Student == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpGet]
public IActionResult Privacy()
{
if (APIClient.Student == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.Student);
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
[HttpPost]
public void Privacy(string name, string email,string login, string password, int scClass)
{
if (APIClient.Student == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(email) || scClass<=0 || scClass > 11)
{
throw new Exception("Введите данные");
}
_student.Update( new StudentBindingModel
{
Id = APIClient.Student.Id,
Name = name,
2023-05-18 20:23:52 +04:00
Email = email,
Password = password,
Login = login,
Class = scClass
});
APIClient.Student.Name = name;
APIClient.Student.Email = email;
APIClient.Student.Password = password;
APIClient.Student.Login = login;
APIClient.Student.Class = scClass;
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Enter()
{
return View();
}
[HttpPost]
public void Enter(string login, string password)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
{
throw new Exception("Введите логин и пароль");
}
APIClient.Student = _student.ReadElement(new StudentSearchModel { Login = login, Password = password });
if (APIClient.Student == null)
{
throw new Exception("Неверный логин/пароль");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public void Register(string name, string email, string login, string password, int scClass)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(email) || scClass <= 0 || scClass > 11)
{
throw new Exception("Введите логин, пароль и ФИО, класс, почту");
}
_student.Create( new StudentBindingModel
{
Name = name,
2023-05-18 20:23:52 +04:00
Email = email,
Password = password,
Login = login,
Class = scClass
});
Response.Redirect("Enter");
return;
}
[HttpGet]
public IActionResult Interests()
{
if (APIClient.Student == null)
{
return Redirect("~/Home/Enter");
}
return View(_interest.ReadList(new InterestSearchModel { StudentId = APIClient.Student.Id}));
}
[HttpGet]
public IActionResult CreateInterest()
{
2023-05-18 23:25:22 +04:00
return View();
}
[HttpPost]
public void CreateInterest(string title,string description)
{
if (APIClient.Student == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(description))
{
throw new Exception("Введите название и описане");
}
_interest.Create( new InterestBindingModel
{
StudentId = APIClient.Student.Id,
Title = title,
Description = description
});
Response.Redirect("Interests");
}
[HttpGet]
public IActionResult InterestSetting(int id)
{
return View(_interest.ReadElement(new InterestSearchModel { Id = id}));
}
[HttpPost]
public void UpdateInterest(int id,string name, string desc)
{
if (string.IsNullOrEmpty(name))
{
throw new Exception("Нет названия");
}
if (string.IsNullOrEmpty(desc))
{
throw new Exception("Нет описания");
}
_interest.Update( new InterestBindingModel
{
Id = id,
Title = name,
Description = desc,
StudentId = APIClient.Student.Id,
});
Response.Redirect("/Home/Interests");
}
public void DeleteInterest(int id)
{
_interest.Delete(new InterestBindingModel
{
Id = id,
});
Response.Redirect("/Home/Interests");
}
[HttpGet]
public IActionResult Diyes()
{
if (APIClient.Student == null)
{
return Redirect("~/Home/Enter");
}
2023-05-17 22:15:18 +04:00
return View(_diy.ReadList(new DiySearchModel { StudentId = APIClient.Student.Id}));
}
[HttpGet]
public IActionResult CreateDiy()
{
ViewBag.Tasks = _task.ReadList(null);
var list = _interest.ReadList(new InterestSearchModel { StudentId = APIClient.Student.Id});
var simpInterest = list.Select(x => new { InterestId = x.Id, InterestName = x.Title });
ViewBag.Interests = new MultiSelectList(simpInterest, "InterestId", "InterestName");
return View();
}
[HttpPost]
public void CreateDiy(string title, string description, string dateCreate, int task, int[] interests )
{
if (APIClient.Student == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
2023-05-18 00:03:47 +04:00
if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(description) || string.IsNullOrEmpty(dateCreate) || task <=0 || interests.Length==0)
{
throw new Exception("Введите название и описане");
}
Dictionary<int, IInterest> diyInterests = new Dictionary<int, IInterest>();
foreach(int id in interests)
{
diyInterests.Add(id,_interest.ReadElement(new InterestSearchModel { Id = id}));
}
_diy.Create(new DiyBindingModel
{
StudentId = APIClient.Student.Id,
StudentName = APIClient.Student.Name,
Title = title,
Description = description,
DateCreate = DateTime.SpecifyKind(DateTime.Parse(dateCreate), DateTimeKind.Utc),
TaskId = task,
TaskName = _task.ReadElement(new TaskSearchModel { Id = task }).Title,
DiyInterests = diyInterests
}) ;
2023-05-18 00:03:47 +04:00
Response.Redirect("Diyes");
}
[HttpGet]
public IActionResult DiySetting(int id)
{
var diy = _diy.ReadElement(new DiySearchModel { Id = id });
2023-05-17 22:15:18 +04:00
var interests = _interest.ReadList(new InterestSearchModel { StudentId = APIClient.Student.Id}).Select(x => new {InterestId = x.Id, InterestName = x.Title}).ToList();
var selectedInterests = diy.DiyInterests.Select(x => x.Key).ToArray();
ViewBag.Interests = new MultiSelectList(interests, "InterestId", "InterestName", selectedInterests);
ViewBag.Tasks = _task.ReadList(null);
return View(diy);
}
[HttpPost]
public void UpdateDiy(int idDiy,string title, string description, string dateCreate, int task, int[] interests)
{
if (string.IsNullOrEmpty(title))
{
throw new Exception("Нет названия");
}
if (string.IsNullOrEmpty(description))
{
throw new Exception("Нет описания");
}
if (string.IsNullOrEmpty(dateCreate))
{
throw new Exception("Нет даты");
}
if (task <= 0)
{
throw new Exception("Нет задания");
}
if (interests.Length == 0)
{
throw new Exception("Нет интересов");
}
Dictionary<int, IInterest> diyInterests = new Dictionary<int, IInterest>();
foreach (int id in interests)
{
diyInterests.Add(id, _interest.ReadElement(new InterestSearchModel { Id = id }));
}
2023-05-17 22:15:18 +04:00
_diy.Update(new DiyBindingModel
{
2023-05-17 22:15:18 +04:00
Id = idDiy,
StudentId = APIClient.Student.Id,
StudentName = APIClient.Student.Name,
Title = title,
Description = description,
DateCreate = DateTime.SpecifyKind(DateTime.Parse(dateCreate), DateTimeKind.Utc),
TaskId = task,
TaskName = _task.ReadElement(new TaskSearchModel { Id = task }).Title,
DiyInterests = diyInterests
});
Response.Redirect("/Home/Diyes");
}
public void DeleteDiy(int id)
{
_diy.Delete( new DiyBindingModel
{
Id = id,
});
Response.Redirect("/Home/Diyes");
}
2023-05-18 00:03:47 +04:00
[HttpGet]
public IActionResult Products()
{
if (APIClient.Student == null)
{
return Redirect("~/Home/Enter");
}
return View(_product.ReadList(new ProductSearchModel { StudentId = APIClient.Student.Id }));
}
[HttpGet]
public IActionResult CreateProduct()
{
var list = _interest.ReadList(new InterestSearchModel { StudentId = APIClient.Student.Id });
var simpInterest = list.Select(x => new { InterestId = x.Id, InterestName = x.Title });
ViewBag.Interests = new MultiSelectList(simpInterest, "InterestId", "InterestName");
return View();
}
[HttpPost]
public void CreateProduct(string title, string description, string dateCreate, int[] interests)
{
if (APIClient.Student == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(description) || string.IsNullOrEmpty(dateCreate)|| interests.Length == 0)
{
throw new Exception("Введите название и описане");
}
Dictionary<int, IInterest> productInterests = new Dictionary<int, IInterest>();
foreach (int id in interests)
{
productInterests.Add(id, _interest.ReadElement(new InterestSearchModel { Id = id }));
}
_product.Create(new ProductBindingModel
{
StudentId = APIClient.Student.Id,
StudentName = APIClient.Student.Name,
Title = title,
Description = description,
DateCreate = DateTime.SpecifyKind(DateTime.Parse(dateCreate), DateTimeKind.Utc),
ProductInterests = productInterests
2023-05-18 00:03:47 +04:00
});
Response.Redirect("Products");
}
[HttpGet]
public IActionResult ProductSetting(int id)
{
var product = _product.ReadElement(new ProductSearchModel { Id = id });
var interests = _interest.ReadList(new InterestSearchModel { StudentId = APIClient.Student.Id }).Select(x => new { InterestId = x.Id, InterestName = x.Title }).ToList();
var selectedInterests = product.ProductInterests.Select(x => x.Key).ToArray();
ViewBag.Interests = new MultiSelectList(interests, "InterestId", "InterestName", selectedInterests);
return View(product);
}
[HttpPost]
public void UpdateProduct(int idDiy, string title, string description, string dateCreate, int[] interests)
{
if (string.IsNullOrEmpty(title))
{
throw new Exception("Нет названия");
}
if (string.IsNullOrEmpty(description))
{
throw new Exception("Нет описания");
}
if (string.IsNullOrEmpty(dateCreate))
{
throw new Exception("Нет даты");
}
if (interests.Length == 0)
{
throw new Exception("Нет интересов");
}
Dictionary<int, IInterest> productInterests = new Dictionary<int, IInterest>();
foreach (int id in interests)
{
productInterests.Add(id, _interest.ReadElement(new InterestSearchModel { Id = id }));
}
_product.Update(new ProductBindingModel
{
Id = idDiy,
StudentId = APIClient.Student.Id,
StudentName = APIClient.Student.Name,
Title = title,
Description = description,
DateCreate = DateTime.SpecifyKind(DateTime.Parse(dateCreate), DateTimeKind.Utc),
ProductInterests = productInterests
});
Response.Redirect("/Home/Products");
}
public void DeleteProduct(int id)
{
_product.Delete(new ProductBindingModel
{
Id = id,
});
Response.Redirect("/Home/Products");
}
2023-05-18 12:56:00 +04:00
public IActionResult Reports()
{
2023-05-18 20:23:52 +04:00
var list = _interest.ReadList(new InterestSearchModel { StudentId = APIClient.Student.Id });
var simpInterest = list.Select(x => new { InterestId = x.Id, InterestName = x.Title });
ViewBag.Interests = new MultiSelectList(simpInterest, "InterestId", "InterestName");
2023-05-18 12:56:00 +04:00
return View();
}
2023-05-18 20:23:52 +04:00
public IActionResult GetPartial(int[] interests, string mode)
{
var list = interests.Select(x => _interest.ReadElement(new InterestSearchModel { Id = x })).ToList();
if (mode.Equals("Excel"))
{
_report.SaveInterestLessonToExcelFile
(new ReportBindingModel { FileName = $"C:\\Users\\Вова\\Documents\\Учеба\\Reports\\{APIClient.Student.Name}-{DateTime.Now.ToString("dd/MM/yyyy")}.xlsx", Interests = list, StudentId = APIClient.Student.Id });
}
else
{
_report.SaveInterestLessonToWordFile
(new ReportBindingModel { FileName = $"C:\\Users\\Вова\\Documents\\Учеба\\Reports\\{APIClient.Student.Name}-{DateTime.Now.ToString("dd/MM/yyyy")}.docx", Interests = list, StudentId = APIClient.Student.Id });
}
var items = _report.GetInterestLesson(new ReportBindingModel { Interests = list, StudentId = APIClient.Student.Id});
return PartialView("_LessonListPartial", items);
}
public IActionResult SendingEmail()
{
return View();
}
public IActionResult GetPartialForPDF(string dateFrom, string dateTo)
{
var _dateFrom = DateTime.SpecifyKind(DateTime.Parse(dateFrom), DateTimeKind.Utc);
var _dateTo = DateTime.SpecifyKind(DateTime.Parse(dateTo), DateTimeKind.Utc);
if(_dateFrom > _dateTo)
{
throw new Exception("Не верные даты");
}
2023-05-18 23:25:22 +04:00
string path = $"C:\\Users\\Вова\\Documents\\Учеба\\Reports\\{APIClient.Student.Name} от {_dateFrom.ToString("dd/MM/yyyy")}.pdf";
_report.SaveInterestsToPdfFile
2023-05-18 23:25:22 +04:00
(new ReportBindingModel { FileName = path,
DateFrom = _dateFrom, DateTo = _dateTo, StudentId = APIClient.Student.Id});
2023-05-18 20:23:52 +04:00
var items = _report.GetInterests(new ReportBindingModel { DateFrom = _dateFrom, DateTo = _dateTo, StudentId = APIClient.Student.Id });
2023-05-18 23:25:22 +04:00
mailSender.MailSendAsync(new MailSendInfoBindingModel
{
MailAddress = APIClient.Student.Email,
Subject = "Заказ номер",
Path = path
});
return PartialView("_LessonListPartial", items);
}
}
}