Case_accounting/CaseAccounting/CaseAccountingProviderView/Controllers/DealController.cs

124 lines
2.6 KiB
C#

using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace CaseAccountingProviderView.Controllers
{
public class DealController : Controller
{
public IActionResult Create()
{
if (APIUser.User == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void Create([FromBody] DealBindingModel dealModel)
{
if (APIUser.User == null)
{
throw new Exception("403");
}
dealModel.UserId = APIUser.User.Id;
APIUser.PostRequest("api/deal/create", dealModel);
Response.Redirect("/Home/Deals");
}
public IActionResult Update(int id)
{
if (APIUser.User == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Deal = APIUser.GetRequest<DealViewModel>($"api/deal/get?id={id}");
return View();
}
[HttpPost]
public void Update([FromBody] DealBindingModel dealModel)
{
if (APIUser.User == null)
{
throw new Exception("403");
}
dealModel.UserId = APIUser.User.Id;
APIUser.PostRequest("api/deal/update", dealModel);
Response.Redirect("/Home/Deals");
}
public IActionResult AddDeal(int id)
{
if (APIUser.User == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.DealId = id;
return View();
}
[HttpPost]
public void AddDeal([FromBody] DealBindingModel dealModel)
{
if (APIUser.User == null)
{
throw new Exception("403");
}
APIUser.PostRequest("api/deal/update", dealModel);
}
public IActionResult Bind(int id)
{
if (APIUser.User == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Deal = APIUser.GetRequest<DealViewModel>($"api/deal/get?id={id}");
return View();
}
[HttpPost]
public void Bind([FromBody] DealBindingModel dealModel)
{
if (APIUser.User == null)
{
throw new Exception("403");
}
APIUser.PostRequest("api/deal/update", dealModel);
}
[HttpPost]
public void Delete(int id)
{
if (APIUser.User == null)
{
throw new Exception("403");
}
APIUser.PostRequest($"api/deal/delete", new DealBindingModel() { Id = id });
Response.Redirect("/Home/Deals");
}
public List<DealViewModel> GetAllByUser()
{
if (APIUser.User == null)
{
return new();
}
List<DealViewModel>? dealModel = APIUser.GetRequest<List<DealViewModel>>($"api/deal/getallbyuser?userId={APIUser.User.Id}");
return dealModel ?? new();
}
public DealViewModel? Get(int id)
{
if (APIUser.User == null)
{
return new();
}
DealViewModel? dealModel = APIUser.GetRequest<DealViewModel>($"api/deal/get?id={id}");
return dealModel;
}
}
}