using CaseAccountingContracts.BindingModels; using CaseAccountingContracts.ViewModels; using CaseAccountingDataModels.Models; using Microsoft.AspNetCore.Mvc; namespace CaseAccountingCustomerView.Controllers { public class Lawyers : Controller { public IActionResult Create() { if (APIUser.User == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public void Create([FromBody] LawyerBindingModel lawyerModel) { if (APIUser.User == null) { throw new Exception("403"); } lawyerModel.UserId = APIUser.User.Id; lawyerModel.SpecializationId = null; var dict = new Dictionary(); foreach (var element in lawyerModel.ContractViewModels) { var contract = APIUser.GetRequest($"api/contract/get?id={element.Id}"); dict.Add(element.Id, contract); } lawyerModel.Contracts = dict; APIUser.PostRequest("api/lawyer/create", lawyerModel); Response.Redirect("/Home/Lawyers"); } public IActionResult Update(int id) { if (APIUser.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Lawyer = APIUser.GetRequest($"api/lawyer/get?id={id}"); return View(); } [HttpPost] public void Update([FromBody] LawyerBindingModel lawyerModel) { if (APIUser.User == null) { throw new Exception("403"); } lawyerModel.UserId = APIUser.User.Id; var dict = new Dictionary(); foreach (var element in lawyerModel.CaseViewModels) { var _case = APIUser.GetRequest($"api/case/get?id={element.Id}"); dict.Add(element.Id, _case); } lawyerModel.Cases = dict; var dictContr = new Dictionary(); foreach (var element in lawyerModel.ContractViewModels) { var contract = APIUser.GetRequest($"api/contract/get?id={element.Id}"); dictContr.Add(element.Id, contract); } lawyerModel.Contracts = dictContr; APIUser.PostRequest("api/lawyer/update", lawyerModel); Response.Redirect("/Home/Lawyers"); } [HttpPost] public void Delete(int id) { if (APIUser.User == null) { throw new Exception("403"); } APIUser.PostRequest($"api/lawyer/delete", new LawyerBindingModel() { Id = id }); Response.Redirect("/Home/Lawyers"); } public List GetAllByUser() { if (APIUser.User == null) { return new(); } List ? lawyers = APIUser.GetRequest>($"api/lawyer/getallbyuser?userId={APIUser.User.Id}"); return lawyers ?? new(); } public LawyerViewModel? Get(int id) { if (APIUser.User == null) { return new(); } LawyerViewModel? lawyer = APIUser.GetRequest($"api/lawyer/get?id={id}"); return lawyer; } public IActionResult Bind(int id) { if (APIUser.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Lawyer = APIUser.GetRequest($"api/lawyer/get?id={id}"); return View(); } [HttpPost] public void Bind([FromBody] LawyerBindingModel lawyerModel) { if (APIUser.User == null) { throw new Exception("403"); } APIUser.PostRequest("api/lawyer/update", lawyerModel); } public List GetAllByUserAndSpecialization(int specialization) { if (APIUser.User == null) { return new(); } List? lawyers = APIUser.GetRequest>($"api/lawyer/getallbyuserandspecialization?userId={APIUser.User.Id}&specialization={specialization}"); return lawyers ?? new(); } public List GetAllCases() { if (APIUser.User == null) { return new(); } List? _case = APIUser.GetRequest>("api/lawyer/GetAllCases"); return _case ?? new(); } } }