PIbd-21_Pyatakov_KM_Markov_.../UniversityCustomer/Controllers/EducationGroupController.cs
2023-05-20 05:46:02 +04:00

85 lines
2.1 KiB
C#

using Microsoft.AspNetCore.Mvc;
using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
using UniversityModels.Models;
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.EducationGroup = APIClient.GetRequest<EducationGroupViewModel>($"api/educationgroup/get?id={id}");
return View();
}
[HttpPost]
public void Update([FromBody] EducationGroupBindingModel egsModel)
{
if (APIClient.User == null)
{
throw new Exception("403");
}
egsModel.UserId = APIClient.User.Id;
APIClient.PostRequest("api/educationgroup/update", egsModel);
Response.Redirect("/Home/EducationGroups");
}
[HttpPost]
public void Delete(int id)
{
if (APIClient.User == null)
{
throw new Exception("403");
}
APIClient.PostRequest($"api/educationgroup/delete", new EducationGroupBindingModel() { Id = id });
Response.Redirect("/Home/EducationGroups");
}
public List<EducationGroupViewModel> GetAllByUser()
{
if (APIClient.User == null)
{
return new();
}
List<EducationGroupViewModel>? egs = APIClient.GetRequest<List<EducationGroupViewModel>>($"api/educationgroup/getallbyuser?userId={APIClient.User.Id}");
return egs ?? new();
}
public EducationGroupViewModel? Get(int id)
{
if (APIClient.User == null)
{
return new();
}
EducationGroupViewModel? eg = APIClient.GetRequest<EducationGroupViewModel>($"api/educationgroup/get?id={id}");
return eg;
}
}
}