using Microsoft.AspNetCore.Mvc; using SchoolAgainStudyContracts.BindingModel; using SchoolAgainStudyContracts.ViewModel; using StudentWebClient.Models; using System.Diagnostics; namespace StudentWebClient.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } 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("Введите данные"); } APIClient.PostRequest("api/student/updatedata", new StudentBindingModel { Id = APIClient.Student.Id, Name = name, Email = login, 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 = APIClient.GetRequest($"api/student/login?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("Введите логин, пароль и ФИО, класс, почту"); } APIClient.PostRequest("api/student/register", new StudentBindingModel { Name = name, Email = login, Password = password, Login = login, Class = scClass }); Response.Redirect("Enter"); return; } [HttpGet] public IActionResult Interests() { if (APIClient.Student == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/main/getinterests?studentId={APIClient.Student.Id}")); } [HttpGet] public IActionResult CreateInterest() { 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("Введите название и описане"); } APIClient.PostRequest("api/main/createinterest", new InterestBindingModel { StudentId = APIClient.Student.Id, Title = title, Description = description }); Response.Redirect("Interests"); } [HttpGet] public IActionResult InterestSetting(int id) { return View(APIClient.GetRequest($"api/main/getinterest?interestId={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("Нет описания"); } APIClient.PostRequest("api/main/UpdateInterest", new InterestBindingModel { Id = id, Title = name, Description = desc, StudentId = APIClient.Student.Id, }); Response.Redirect("/Home/Interests"); } public void DeleteInterest(int id) { APIClient.PostRequest("api/main/DeleteInterest", new InterestBindingModel { Id = id, }); Response.Redirect("/Home/Interests"); } [HttpGet] public IActionResult Diyes() { if (APIClient.Student == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/main/GetDiyes?studentId={APIClient.Student.Id}")); } [HttpGet] public IActionResult CreateDiy() { ViewBag.Tasks = APIClient.GetRequest>("api/shop/GetTaskList"); return View(); } [HttpPost] public void CreateDiy(string title, string description, string dateCreate, int task, int[] interests ) { if (APIClient.Student == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(description) || string.IsNullOrEmpty(dateCreate) && task <=0 || interests.Length==0) { throw new Exception("Введите название и описане"); } APIClient.PostRequest("api/main/creatediy", new InterestBindingModel { StudentId = APIClient.Student.Id, Title = title, Description = description }); Response.Redirect("Interests"); } [HttpGet] public IActionResult DiySetting(int id) { return View(APIClient.GetRequest($"api/main/getdiy?diyId={id}")); } [HttpPost] public void UpdateDiy(int id, string name, string desc,string dateCreate, int task, int[] interests) { if (string.IsNullOrEmpty(name)) { throw new Exception("Нет названия"); } if (string.IsNullOrEmpty(desc)) { throw new Exception("Нет описания"); } if (string.IsNullOrEmpty(dateCreate)) { throw new Exception("Нет даты"); } if (task <= 0) { throw new Exception("Нет задания"); } if (interests.Length == 0) { throw new Exception("Нет интересов"); } APIClient.PostRequest("api/main/UpdateDiy", new InterestBindingModel { Id = id, Title = name, Description = desc, StudentId = APIClient.Student.Id, }); Response.Redirect("/Home/Diyes"); } public void DeleteDiy(int id) { APIClient.PostRequest("api/main/DeletDiy", new DiyBindingModel { Id = id, }); Response.Redirect("/Home/Diyes"); } } }