using Microsoft.AspNetCore.Mvc; using PlumbingRepairClientApp; using System.Diagnostics; using UniversityClientAppWorker.Models; using UniversityContracts.BindingModels; using UniversityContracts.ViewModels; using UniversityDataModels.Enums; using UniversityDataModels.Models; namespace UniversityClientAppWorker.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } [HttpGet] public async Task Index() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Teachers = APIClient.GetRequest>($"api/teacher/getallteachers"); var planOfStudys = await APIClient.GetRequestPlanOfStudyAsync>($"api/planofstudys/getplanofstudys?userId={APIClient.User.Id}"); return View(planOfStudys); } [HttpGet] public async Task InfoPlanOfStudy(int id) { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } var obj1 = APIClient.GetRequest>($"api/teacher/getallteachers"); ViewBag.Teachers = obj1; var obj = await APIClient.GetRequestPlanOfStudyAsync($"api/planofstudys/getplanofstudy?id={id}&userId={APIClient.User.Id}"); return View(obj); } [HttpPost] public void CreatePlanOfStudy(string profile, string formOfStudy, List teacherIds) { if (APIClient.User == null) { throw new Exception("Вход только авторизованным пользователям"); } if (string.IsNullOrEmpty(formOfStudy) || string.IsNullOrEmpty(profile)) { throw new Exception("Введите данные профиля и формы обучения"); } APIClient.PostRequest("api/planofstudys/createplanofstudy", new PlanOfStudyBindingModel { Profile = profile, FormOfStudy = formOfStudy, UserId = APIClient.User.Id, PlanOfStudyTeachers = teacherIds.ToDictionary(id => id, id => (ITeacherModel)null) }); Response.Redirect("Index"); } [HttpPost] public void DeletePlanOfStudy(int id) { if (id == 0) { throw new Exception("id не может быть равен 0"); } APIClient.PostRequest("api/planofstudys/deleteplanofstudy", new PlanOfStudyBindingModel { Id = id }); Response.Redirect("Index"); } [HttpPost] public void UpdatePlanOfStudy(int id, string profile, string formOfStudy, List teacherIds) { if (id == 0) { throw new Exception("id не может быть равен 0"); } var planOfStudyTeachers = teacherIds.ToDictionary(id => id, id => (ITeacherModel)null); APIClient.PostRequest("api/planofstudys/updateplanofstudy", new PlanOfStudyBindingModel { Id = id, Profile = profile, FormOfStudy = formOfStudy, PlanOfStudyTeachers = planOfStudyTeachers }); Response.Redirect("Index"); } [HttpGet] public IActionResult Privacy() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(APIClient.User); } [HttpPost] public void Privacy(string login, string password, string email) { if (APIClient.User == null) { throw new Exception("Вход только авторизованным пользователям"); } if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(email)) { throw new Exception("Введите логин, пароль и почту"); } APIClient.PostRequest("api/user/updatedata", new UserViewModel { Id = APIClient.User.Id, Email = email, Login = login, Password = password }); APIClient.User.Login = login; APIClient.User.Email = email; APIClient.User.Password = password; Response.Redirect("Index"); } [HttpGet] public IActionResult Attestations() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Students = APIClient.GetRequest>($"api/student/getstudents?userId={APIClient.User.Id}"); ViewBag.AttestationScore = Enum.GetValues(typeof(AttestationScore)).Cast(); var obj = APIClient.GetRequest>($"api/attestation/getattestations?userId={APIClient.User.Id}"); return View(obj); } [HttpPost] public void CreateAttestation(string formOfEvaluation, int student, AttestationScore score) { if (APIClient.User == null) { throw new Exception("Вход только авторизованным"); } if (string.IsNullOrEmpty(formOfEvaluation) || student == 0) { throw new Exception("Введите форму оценивания и выберите студента"); } APIClient.PostRequest("api/attestation/createattestation", new AttestationBindingModel { UserId = APIClient.User.Id, FormOfEvaluation = formOfEvaluation, StudentId = student, Score = score }); Response.Redirect("Attestations"); } [HttpGet] public async Task Students() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } var planOfStudys = await APIClient.GetRequestPlanOfStudyAsync> ($"api/planofstudys/getplanofstudys?userId={APIClient.User.Id}"); ViewBag.PlanOfStudys = planOfStudys; return View(APIClient.GetRequest>($"api/student/getstudents?userId={APIClient.User.Id}")); } [HttpPost] public void CreateStudent(string name, int planOfStudy, string phoneNumber) { if (APIClient.User == null) { throw new Exception("Вход только авторизованным"); } if (string.IsNullOrEmpty(name) || planOfStudy == 0 || string.IsNullOrEmpty(phoneNumber)) { throw new Exception("Введите ФИО, план обучения и телефон"); } APIClient.PostRequest("api/student/createstudent", new StudentBindingModel { UserId = APIClient.User.Id, Name = name, PlanOfStudyId = planOfStudy, PhoneNumber = phoneNumber, }); Response.Redirect("Students"); } [HttpGet] public IActionResult Enter() { return View(); } [HttpGet] public IActionResult Register() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } [HttpPost] public void Enter(string login, string password) { if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) { throw new Exception("Введите логин и пароль"); } APIClient.User = APIClient.GetRequest($"api/user/loginworker?login={login}&password={password}"); if (APIClient.User == null) { throw new Exception("Неверный логин/пароль"); } Response.Redirect("Index"); } [HttpPost] public void Register(string login, string password, string email) { if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(email)) { throw new Exception("Введите логин, пароль и почту"); } APIClient.PostRequest("api/user/registerworker", new UserBindingModel { Email = email, Login = login, Password = password }); Response.Redirect("Enter"); return; } [HttpGet] public IActionResult ReportPlanOfStudys() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View("ReportPlanOfStudys", APIClient.GetRequest>($"api/planofstudys/getplanofstudyanddisciplines?userId={APIClient.User.Id}")); } [HttpPost] public void ReportPlanOfStudys(string type) { if (APIClient.User == null) { Redirect("~/Home/Enter"); throw new Exception("Вход только авторизованным"); } if (string.IsNullOrEmpty(type)) { throw new Exception("Неверный тип отчета"); } if (type == "docx") { APIClient.PostRequest("api/planofstudys/loadreporttoword", new ReportBindingModel { FileName = $"C:\\Users\\{Environment.UserName}\\Desktop\\Планы обучений по дисциплинам.docx" }); Response.Redirect("Index"); return; } if (type == "xlsx") { APIClient.PostRequest("api/planofstudys/loadreporttoexcel", new ReportBindingModel { FileName = $"C:\\Users\\{Environment.UserName}\\Desktop\\Планы обучений по дисциплинам.xlsx" }); Response.Redirect("Index"); return; } } } }