CourseWork_SchoolStudyAgain/SchoolAgainStudy/StudentWebClient/Controllers/HomeController.cs

317 lines
11 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.BusinessLogicContracts;
using SchoolAgainStudyContracts.SearchModel;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataModels.Models;
using StudentWebClient.Models;
using System.Diagnostics;
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;
public HomeController(ILogger<HomeController> logger, IDiyLogic diy, IProductLogic product , ITaskLogic task , IInterestLogic interest, IStudentLogic student)
{
_logger = logger;
_diy = diy;
_product = product;
_task = task;
_interest = interest;
_student = student;
}
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,
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 = _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,
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(_interest.ReadList(new InterestSearchModel { 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("Введите название и описане");
}
_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");
}
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("Вы как суда попали? Суда вход только авторизованным");
}
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
}) ;
Response.Redirect("Interests");
}
[HttpGet]
public IActionResult DiySetting(int id)
{
var diy = _diy.ReadElement(new DiySearchModel { Id = id });
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 }));
}
_diy.Update(new DiyBindingModel
{
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");
}
}
}