Case_accounting/CaseAccounting/CaseAccountingProviderView/Controllers/DealController.cs
2023-05-19 21:07:14 +04:00

149 lines
3.6 KiB
C#

using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.ViewModels;
using CaseAccountingDataModels.Models;
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;
var dict = new Dictionary<int, ICaseModel>();
foreach (var element in dealModel.CaseViewModels)
{
var caseModel = APIUser.GetRequest<CaseViewModel>($"api/case/get?id={element.Id}");
dict.Add(element.Id, caseModel);
}
dealModel.Cases = dict;
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;
var contractdict = new Dictionary<int, IContractModel>();
foreach (var element in dealModel.ContractViewModels)
{
var contractModel = APIUser.GetRequest<ContractViewModel>($"api/contract/get?id={element.Id}");
contractdict.Add(element.Id, contractModel);
}
dealModel.Contracts = contractdict;
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;
}
public List<ContractViewModel> GetAllContracts()
{
if (APIUser.User == null)
{
return new();
}
List<ContractViewModel>? contractModel = APIUser.GetRequest<List<ContractViewModel>>($"api/deal/getallcontracts");
return contractModel ?? new();
}
}
}