Case_accounting/CaseAccounting/CaseAccountingCustomerView/Controllers/Specializations.cs

95 lines
2.9 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;
}
public List<SpecializationViewModel> GetAll()
{
if (APIUser.User == null)
{
return new();
}
List<SpecializationViewModel>? specializations = APIUser.GetRequest<List<SpecializationViewModel>>($"api/specialization/getall");
return specializations ?? new();
}
}
}