diff --git a/CaseAccounting/CaseAccountingContracts/BindingModels/DealBindingModel.cs b/CaseAccounting/CaseAccountingContracts/BindingModels/DealBindingModel.cs index e321d1d..6f2c914 100644 --- a/CaseAccounting/CaseAccountingContracts/BindingModels/DealBindingModel.cs +++ b/CaseAccounting/CaseAccountingContracts/BindingModels/DealBindingModel.cs @@ -25,12 +25,17 @@ namespace CaseAccountingContracts.BindingModels public List CaseViewModels { get; set; } = new(); + public Dictionary Contracts { get; set; } = new(); + + public List ContractViewModels { get; set; } = new(); + public DealBindingModel() { } [JsonConstructor] - public DealBindingModel(Dictionary Cases) + public DealBindingModel(Dictionary Cases, Dictionary Contracts) { this.Cases = Cases.ToDictionary(x => x.Key, x => (ICaseModel)x.Value); + this.Contracts = Contracts.ToDictionary(x => x.Key, x => (IContractModel)x.Value); } } } diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/DealStorage.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/DealStorage.cs index 7c44a90..718ad0d 100644 --- a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/DealStorage.cs +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/DealStorage.cs @@ -115,9 +115,9 @@ namespace CaseAccountingDataBaseImplement.Implements { return null; } - deal.Update(model); + deal.Update(context, model); context.SaveChanges(); - deal.UpdateCases(context, model); + if(model.Cases.Count > 0) deal.UpdateCases(context, model); transaction.Commit(); return deal.GetViewModel; } diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Models/Deal.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Models/Deal.cs index 7bc42a9..b3fde5e 100644 --- a/CaseAccounting/CaseAccountingDataBaseImplement/Models/Deal.cs +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Models/Deal.cs @@ -72,7 +72,7 @@ namespace CaseAccountingDataBaseImplement.Models }; } - public void Update(DealBindingModel? model) + public void Update(CaseAccountingDatabase context, DealBindingModel? model) { if (model == null) { @@ -81,6 +81,13 @@ namespace CaseAccountingDataBaseImplement.Models Subject = model.Subject; Responsibilities = model.Responsibilities; Date = model.Date; + if (model.Contracts.Count > 0) + { + Contracts = model.Contracts.Select(x => new DealContract + { + Contract = context.Contracts.First(y => y.Id == x.Key) + }).ToList(); + } } public void UpdateCases(CaseAccountingDatabase context, DealBindingModel model) diff --git a/CaseAccounting/CaseAccountingProviderView/Controllers/DealController.cs b/CaseAccounting/CaseAccountingProviderView/Controllers/DealController.cs index f8b1c51..200646e 100644 --- a/CaseAccounting/CaseAccountingProviderView/Controllers/DealController.cs +++ b/CaseAccounting/CaseAccountingProviderView/Controllers/DealController.cs @@ -53,7 +53,14 @@ namespace CaseAccountingProviderView.Controllers throw new Exception("403"); } dealModel.UserId = APIUser.User.Id; - APIUser.PostRequest("api/deal/update", dealModel); + var contractdict = new Dictionary(); + foreach (var element in dealModel.ContractViewModels) + { + var contractModel = APIUser.GetRequest($"api/contract/get?id={element.Id}"); + contractdict.Add(element.Id, contractModel); + } + dealModel.Contracts = contractdict; + APIUser.PostRequest("api/deal/update", dealModel); Response.Redirect("/Home/Deals"); } @@ -127,5 +134,15 @@ namespace CaseAccountingProviderView.Controllers DealViewModel? dealModel = APIUser.GetRequest($"api/deal/get?id={id}"); return dealModel; } - } + + public List GetAllContracts() + { + if (APIUser.User == null) + { + return new(); + } + List? contractModel = APIUser.GetRequest>($"api/deal/getallcontracts"); + return contractModel ?? new(); + } + } } diff --git a/CaseAccounting/CaseAccountingProviderView/Views/Deal/Bind.cshtml b/CaseAccounting/CaseAccountingProviderView/Views/Deal/Bind.cshtml new file mode 100644 index 0000000..9bb03da --- /dev/null +++ b/CaseAccounting/CaseAccountingProviderView/Views/Deal/Bind.cshtml @@ -0,0 +1,37 @@ +@{ + ViewData["Title"] = "Договор"; +} + +@{ +

Привязка контракта к договору

+ +
+
+

+
+
+ +

Номер:

+ + +
+
+ + + + + + + + + + +
Услуга:Цена:Дата:
+
+
+ + +} + diff --git a/CaseAccounting/CaseAccountingProviderView/Views/Home/Deals.cshtml b/CaseAccounting/CaseAccountingProviderView/Views/Home/Deals.cshtml index 73b4f83..5279818 100644 --- a/CaseAccounting/CaseAccountingProviderView/Views/Home/Deals.cshtml +++ b/CaseAccounting/CaseAccountingProviderView/Views/Home/Deals.cshtml @@ -28,6 +28,9 @@ Дата составления + + Привязать запись + Изменить запись @@ -49,6 +52,9 @@ @item.Date.ToString("yyyy-MM-dd") + + Привязка + Изменить diff --git a/CaseAccounting/CaseAccountingProviderView/wwwroot/js/deal/deal-bind.js b/CaseAccounting/CaseAccountingProviderView/wwwroot/js/deal/deal-bind.js new file mode 100644 index 0000000..e3eaacc --- /dev/null +++ b/CaseAccounting/CaseAccountingProviderView/wwwroot/js/deal/deal-bind.js @@ -0,0 +1,97 @@ +const saveBtn = document.getElementById("save-button"); +const tbody = document.getElementById("scrollable-table__tbody"); +const currentDealId = document.getElementById("deal-data").dataset.id; + +var contracts = []; +var dataArray = []; +var currentDeal = null; + +window.addEventListener("load", async () => { + try { + await $.ajax({ + url: `/deal/getallcontracts`, + type: "GET", + contentType: "json" + }).done((result) => { + contracts = result; + console.log(contracts); + contracts.forEach((contract) => { + const { id, service, coast, date } = contract; + const row = tbody.insertRow(); + row.setAttribute("data-id", id); + + const cells = [service, coast, date]; + cells.forEach((value) => { + const cell = row.insertCell(); + cell.textContent = value; + }); + + row.addEventListener('click', () => addAndRemoveFromList(row)); + }); + }); + + await $.ajax({ + url: `/deal/get?id=${currentDealId}`, + type: "GET", + contentType: "json" + }).done((result) => { + currentDeal = result; + console.log(currentDeal) + }); + } catch (error) { + console.error(error); + } +}); + +saveBtn.addEventListener("click", () => { + if (!correctData()) { + return; + } + if (!validate()) { + return; + } +}); + +const correctData = function () { + + return true; +}; + +const validate = function () { + + return true; +}; + +saveBtn.addEventListener("click", () => { + let dealModel = { + "Id": currentDeal.id, + "Subject": currentDeal.subject, + "Responsibilities": currentDeal.responsibilities, + "Date": currentDeal.date, + "ContractViewModels": dataArray + }; + console.log(dealModel); + console.log(dataArray); + $.ajax({ + url: "/deal/update", + type: "POST", + contentType: "application/json", + data: JSON.stringify(dealModel) + }).done(() => { + window.location.href = "/Home/Deals"; + }); +}); + +const addAndRemoveFromList = (row) => { + var id = parseInt(row.dataset.id); + console.log(contracts.find(x => x.id === id)) + var index = dataArray.indexOf(contracts.find(x => x.id === id)); + if (index === -1) { + dataArray.push(contracts.find(x => x.id === id)); + row.classList.add("bg-primary"); + } else { + dataArray.splice(index, 1); + row.classList.remove("bg-primary"); + } + console.log(dataArray); +} \ No newline at end of file diff --git a/CaseAccounting/CaseAccountingRestApi/Controllers/DealController.cs b/CaseAccounting/CaseAccountingRestApi/Controllers/DealController.cs index 762d9eb..8423c40 100644 --- a/CaseAccounting/CaseAccountingRestApi/Controllers/DealController.cs +++ b/CaseAccounting/CaseAccountingRestApi/Controllers/DealController.cs @@ -1,4 +1,5 @@ -using CaseAccountingContracts.BindingModels; +using CaseAccountingBusinessLogic.BusinessLogics; +using CaseAccountingContracts.BindingModels; using CaseAccountingContracts.BusinessLogicContracts; using CaseAccountingContracts.SearchModels; using CaseAccountingContracts.ViewModels; @@ -11,10 +12,12 @@ namespace CaseAccountingRestApi.Controllers public class DealController : Controller { private readonly IDealLogic _logic; + private readonly IContractLogic _contractLogic; - public DealController(IDealLogic logic) + public DealController(IDealLogic logic, IContractLogic contractLogic) { _logic = logic; + _contractLogic = contractLogic; } [HttpGet] @@ -43,7 +46,20 @@ namespace CaseAccountingRestApi.Controllers } } - [HttpPost] + [HttpGet] + public List? GetAllContracts() + { + try + { + return _contractLogic.ReadList(null); + } + catch (Exception) + { + throw; + } + } + + [HttpPost] public void Create(DealBindingModel model) { try