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

74 lines
2.2 KiB
C#

using Microsoft.AspNetCore.Mvc;
using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
namespace UniversityCustomer.Controllers
{
public class DisciplineController : Controller
{
public IActionResult Create()
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void Create([FromBody] DisciplineBindingModel disciplineModel)
{
if (APIClient.User == null)
{
throw new Exception("403");
}
disciplineModel.UserId = APIClient.User.Id;
APIClient.PostRequest("api/discipline/create", disciplineModel);
Response.Redirect("/Home/Disciplines");
}
public IActionResult Update(int id)
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Discipline = APIClient.GetRequest<DisciplineViewModel>($"api/discipline/get?id={id}");
return View();
}
[HttpPost]
public void Update([FromBody] DisciplineBindingModel disciplineModel)
{
if (APIClient.User == null)
{
throw new Exception("403");
}
disciplineModel.UserId = APIClient.User.Id;
APIClient.PostRequest("api/discipline/update", disciplineModel);
Response.Redirect("/Home/Disciplines");
}
[HttpPost]
public void Delete(int id)
{
if (APIClient.User == null)
{
throw new Exception("403");
}
APIClient.PostRequest($"api/discipline/delete", new DisciplineBindingModel() { Id = id });
Response.Redirect("/Home/Disciplines");
}
public DisciplineViewModel? Get(int id)
{
if (APIClient.User == null)
{
return new();
}
DisciplineViewModel? document = APIClient.GetRequest<DisciplineViewModel>($"api/discipline/get?id={id}");
return document;
}
}
}