PIbd-21_Pyatakov_KM_Markov_.../UniversityCustomer/Controllers/EducationGroupController.cs

94 lines
2.3 KiB
C#

using Microsoft.AspNetCore.Mvc;
using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
namespace UniversityCustomer.Controllers
{
public class EducationGroupController : Controller
{
public IActionResult Create()
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void Create([FromBody] EducationGroupBindingModel educationGroupModel)
{
if (APIClient.User == null)
{
throw new Exception("403");
}
educationGroupModel.UserId = APIClient.User.Id;
APIClient.PostRequest("api/educationgroup/create", educationGroupModel);
Response.Redirect("/Home/EducationGroups");
}
public IActionResult Update(int id)
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Student = APIClient.GetRequest<StudentViewModel>($"api/student/get?id={id}");
return View();
}
[HttpPost]
public void Update([FromBody] StudentBindingModel studentModel)
{
if (APIClient.User == null)
{
throw new Exception("403");
}
studentModel.UserId = APIClient.User.Id;
APIClient.PostRequest("api/student/update", studentModel);
Response.Redirect("/Home/Students");
}
[HttpPost]
public void Delete(int id)
{
if (APIClient.User == null)
{
throw new Exception("403");
}
APIClient.PostRequest($"api/student/delete", new StudentBindingModel() { Id = id });
Response.Redirect("/Home/Students");
}
public List<StudentViewModel> GetAllByUser()
{
if (APIClient.User == null)
{
return new();
}
List<StudentViewModel>? students = APIClient.GetRequest<List<StudentViewModel>>($"api/student/getallbyuser?userId={APIClient.User.Id}");
return students ?? new();
}
public List<StudentViewModel> GetAllByUserAndEducationStatus(int educationstatus)
{
if (APIClient.User == null)
{
return new();
}
List<StudentViewModel>? students = APIClient.GetRequest<List<StudentViewModel>>($"api/student/getallbyuserandeducationstatus?userId={APIClient.User.Id}&educationstatus={educationstatus}");
return students ?? new();
}
public StudentViewModel? Get(int id)
{
if (APIClient.User == null)
{
return new();
}
StudentViewModel? student = APIClient.GetRequest<StudentViewModel>($"api/student/get?id={id}");
return student;
}
}
}