Case_accounting/CaseAccounting/CaseAccountingCustomerView/Controllers/Specializations.cs

84 lines
2.6 KiB
C#

using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace CaseAccountingCustomerView.Controllers
{
public class Specializations : Controller
{
public IActionResult Create()
{
if (APIUser.User == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void Create([FromBody] SpecializationBindingModel specializationModel)
{
if (APIUser.User == null)
{
throw new Exception("403");
}
specializationModel.UserId = APIUser.User.Id;
APIUser.PostRequest("api/specialization/create", specializationModel);
Response.Redirect("/Home/Specializations");
}
public IActionResult Update(int id)
{
if (APIUser.User == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Specialization = APIUser.GetRequest<SpecializationViewModel>($"api/specialization/get?id={id}");
return View();
}
[HttpPost]
public void Update([FromBody] SpecializationBindingModel specializationModel)
{
if (APIUser.User == null)
{
throw new Exception("403");
}
specializationModel.UserId = APIUser.User.Id;
APIUser.PostRequest("api/specialization/update", specializationModel);
Response.Redirect("/Home/Specializations");
}
[HttpPost]
public void Delete(int id)
{
if (APIUser.User == null)
{
throw new Exception("403");
}
APIUser.PostRequest($"api/specialization/delete", new SpecializationBindingModel() { Id = id });
Response.Redirect("/Home/Specializations");
}
public List<SpecializationViewModel> GetAllByUser()
{
if (APIUser.User == null)
{
return new();
}
List<SpecializationViewModel>? specializations = APIUser.GetRequest<List<SpecializationViewModel>>($"api/specialization/getallbyuser?userId={APIUser.User.Id}");
return specializations ?? new();
}
public SpecializationViewModel? Get(int id)
{
if (APIUser.User == null)
{
return new();
}
SpecializationViewModel? specialization = APIUser.GetRequest<SpecializationViewModel>($"api/specialization/get?id={id}");
return specialization;
}
}
}