diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/CaseAccountingDatabase.cs b/CaseAccounting/CaseAccountingDataBaseImplement/CaseAccountingDatabase.cs index 2d54bb1..3958d65 100644 --- a/CaseAccounting/CaseAccountingDataBaseImplement/CaseAccountingDatabase.cs +++ b/CaseAccounting/CaseAccountingDataBaseImplement/CaseAccountingDatabase.cs @@ -44,7 +44,7 @@ namespace CaseAccountingDataBaseImplement public virtual DbSet Lawyers { set; get; } - public virtual DbSet LawyerContracts { set; get; } + public virtual DbSet LawyerContracts { set; get; } public virtual DbSet Specializations { get; set; } } diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/CaseStorage.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/CaseStorage.cs index 804e23e..de0807a 100644 --- a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/CaseStorage.cs +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/CaseStorage.cs @@ -147,23 +147,28 @@ namespace CaseAccountingDataBaseImplement.Implements public CaseViewModel? Update(CaseBindingModel model) { using var context = new CaseAccountingDatabase(); - - var _case = context.Cases - .Include(x => x.Specialization) - .Include(x => x.User) - .FirstOrDefault(x => x.Id == model.Id); - - if (_case == null) + using var transaction = context.Database.BeginTransaction(); + try { - return null; + var _case = context.Cases + .Include(x => x.Specialization) + .Include(x => x.User) + .FirstOrDefault(x => x.Id == model.Id); + + if (_case == null) + { + return null; + } + _case.Update(model); + context.SaveChanges(); + _case.UpdateLawyers(context, model); + return _case.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; } - _case.Update(model); - context.SaveChanges(); - return context.Cases - .Include(x => x.Specialization) - .Include(x => x.User) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; } } } diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/ContractStorage.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/ContractStorage.cs index dbd593c..b3777af 100644 --- a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/ContractStorage.cs +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/ContractStorage.cs @@ -105,21 +105,27 @@ namespace CaseAccountingDataBaseImplement.Implements public ContractViewModel? Update(ContractBindingModel model) { using var context = new CaseAccountingDatabase(); - - var deal = context.Contracts + using var transaction = context.Database.BeginTransaction(); + try + { + var contract = context.Contracts .Include(x => x.User) .FirstOrDefault(x => x.Id == model.Id); - if (deal == null) - { - return null; + if (contract == null) + { + return null; + } + contract.Update(model); + context.SaveChanges(); + contract.UpdateDeals(context, model); + return contract.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; } - deal.Update(model); - context.SaveChanges(); - return context.Contracts - .Include(x => x.User) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; } } } diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/DealStorage.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/DealStorage.cs index 5a966ce..2066271 100644 --- a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/DealStorage.cs +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/DealStorage.cs @@ -105,21 +105,27 @@ namespace CaseAccountingDataBaseImplement.Implements public DealViewModel? Update(DealBindingModel model) { using var context = new CaseAccountingDatabase(); - - var deal = context.Deals + using var transaction = context.Database.BeginTransaction(); + try + { + var deal = context.Deals .Include(x => x.User) .FirstOrDefault(x => x.Id == model.Id); - if (deal == null) - { - return null; + if (deal == null) + { + return null; + } + deal.Update(model); + context.SaveChanges(); + deal.UpdateCases(context, model); + return deal.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; } - deal.Update(model); - context.SaveChanges(); - return context.Deals - .Include(x => x.User) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; } } } diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/LawyerStorage.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/LawyerStorage.cs index 0e8f51e..b066a10 100644 --- a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/LawyerStorage.cs +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/LawyerStorage.cs @@ -111,23 +111,28 @@ namespace CaseAccountingDataBaseImplement.Implements public LawyerViewModel? Update(LawyerBindingModel model) { using var context = new CaseAccountingDatabase(); - - var lawyer = context.Lawyers + using var transaction = context.Database.BeginTransaction(); + try + { + var lawyer = context.Lawyers .Include(x => x.Specialization) .Include(x => x.User) .FirstOrDefault(x => x.Id == model.Id); - if (lawyer == null) - { - return null; + if (lawyer == null) + { + return null; + } + lawyer.Update(model); + context.SaveChanges(); + lawyer.UpdateContracts(context, model); + return lawyer.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; } - lawyer.Update(model); - context.SaveChanges(); - return context.Lawyers - .Include(x => x.Specialization) - .Include(x => x.User) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; } } } diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Models/Case.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Models/Case.cs index a9e5894..020b5b1 100644 --- a/CaseAccounting/CaseAccountingDataBaseImplement/Models/Case.cs +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Models/Case.cs @@ -45,7 +45,23 @@ namespace CaseAccountingDataBaseImplement.Models public virtual List Hearings { get; set; } = new(); [ForeignKey("CaseId")] - public virtual List Lawyers { get; set; } = new(); + public virtual List CaseLawyers { get; set; } = new(); + + private Dictionary? _lawyers; + + [NotMapped] + public Dictionary Lawyers + { + get + { + if (_lawyers == null) + { + _lawyers = CaseLawyers.ToDictionary( + x => x.LawyerId, x => x.Lawyer as ILawyerModel); + } + return _lawyers; + } + } [ForeignKey("CaseId")] public virtual List Deals { get; set; } = new(); @@ -80,6 +96,38 @@ namespace CaseAccountingDataBaseImplement.Models Date = model.Date; } + public void UpdateLawyers(CaseAccountingDatabase context, CaseBindingModel model) + { + var caseLawyers = context.CaseLawyers + .Where(x => x.CaseId == model.Id) + .ToList(); + if (caseLawyers != null) + { + context.CaseLawyers + .RemoveRange(caseLawyers + .Where(x => !model.Lawyers + .ContainsKey(x.LawyerId)) + ); + context.SaveChanges(); + var _case = context.Cases + .First(x => x.Id == Id); + foreach (var cl in caseLawyers) + { + model.Lawyers.Remove(cl.LawyerId); + } + foreach (var cl in model.Lawyers) + { + context.CaseLawyers.Add(new CaseLawyer + { + Case = _case, + Lawyer = context.Lawyers.First(x => x.Id == cl.Key), + }); + context.SaveChanges(); + } + _lawyers = null; + } + } + public CaseViewModel GetViewModel => new() { Id = Id, @@ -91,7 +139,5 @@ namespace CaseAccountingDataBaseImplement.Models SpecializationId = SpecializationId, UserId = UserId }; - - Dictionary ICaseModel.Lawyers => throw new NotImplementedException(); } } diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Models/Contract.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Models/Contract.cs index 497995b..3694646 100644 --- a/CaseAccounting/CaseAccountingDataBaseImplement/Models/Contract.cs +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Models/Contract.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Diagnostics; +using System.Diagnostics.Contracts; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -31,11 +32,27 @@ namespace CaseAccountingDataBaseImplement.Models public virtual User User { get; set; } = new(); [ForeignKey("ContractId")] - public virtual List LawyerContracts { get; set; } = new(); + public virtual List LawyerContracts { get; set; } = new(); [ForeignKey("ContractId")] public virtual List DealContracts { get; set; } = new(); + private Dictionary? _deals; + + [NotMapped] + public Dictionary Deals + { + get + { + if (_deals == null) + { + _deals = DealContracts.ToDictionary( + x => x.DealId, x => x.Deal as IDealModel); + } + return _deals; + } + } + public static Contract? Create(ContractBindingModel? model) { if (model == null) @@ -63,6 +80,38 @@ namespace CaseAccountingDataBaseImplement.Models Date = model.Date; } + public void UpdateDeals(CaseAccountingDatabase context, ContractBindingModel model) + { + var contractDeals = context.DealContracts + .Where(x => x.ContractId == model.Id) + .ToList(); + if (contractDeals != null) + { + context.DealContracts + .RemoveRange(contractDeals + .Where(x => !model.Deals + .ContainsKey(x.DealId)) + ); + context.SaveChanges(); + var contract = context.Contracts + .First(x => x.Id == Id); + foreach (var cd in contractDeals) + { + model.Deals.Remove(cd.DealId); + } + foreach (var cd in model.Deals) + { + context.DealContracts.Add(new DealContract + { + Contract = contract, + Deal = context.Deals.First(x => x.Id == cd.Key), + }); + context.SaveChanges(); + } + _deals = null; + } + } + public ContractViewModel GetViewModel => new() { Id = Id, diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Models/Deal.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Models/Deal.cs index ac1c118..4f75d8b 100644 --- a/CaseAccounting/CaseAccountingDataBaseImplement/Models/Deal.cs +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Models/Deal.cs @@ -32,6 +32,22 @@ namespace CaseAccountingDataBaseImplement.Models [ForeignKey("DealId")] public virtual List CaseDeals { get; set; } = new(); + private Dictionary? _cases; + + [NotMapped] + public Dictionary Cases + { + get + { + if (_cases == null) + { + _cases = CaseDeals.ToDictionary( + x => x.CaseId, x => x.Case as ICaseModel); + } + return _cases; + } + } + [ForeignKey("DealId")] public virtual List Contracts { get; set; } = new(); @@ -62,6 +78,38 @@ namespace CaseAccountingDataBaseImplement.Models Date = model.Date; } + public void UpdateCases(CaseAccountingDatabase context, DealBindingModel model) + { + var dealCases = context.CaseDeals + .Where(x => x.DealId == model.Id) + .ToList(); + if (dealCases != null) + { + context.CaseDeals + .RemoveRange(dealCases + .Where(x => !model.Cases + .ContainsKey(x.CaseId)) + ); + context.SaveChanges(); + var deal = context.Deals + .First(x => x.Id == Id); + foreach (var dc in dealCases) + { + model.Cases.Remove(dc.CaseId); + } + foreach (var dc in model.Cases) + { + context.CaseDeals.Add(new CaseDeal + { + Deal = deal, + Case = context.Cases.First(x => x.Id == dc.Key), + }); + context.SaveChanges(); + } + _cases = null; + } + } + public DealViewModel GetViewModel => new() { Id = Id, diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Models/Lawyer.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Models/Lawyer.cs index f97444c..6f3f0d0 100644 --- a/CaseAccounting/CaseAccountingDataBaseImplement/Models/Lawyer.cs +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Models/Lawyer.cs @@ -39,7 +39,23 @@ namespace CaseAccountingDataBaseImplement.Models public virtual List CaseLawyers { get; set; } = new(); [ForeignKey("LawyerId")] - public virtual List Contracts { get; set; } = new(); + public virtual List LawyerContracts { get; set; } = new(); + + private Dictionary? _contracts; + + [NotMapped] + public Dictionary Contracts + { + get + { + if (_contracts == null) + { + _contracts = LawyerContracts.ToDictionary( + x => x.ContractId, x => x.Contract as IContractModel); + } + return _contracts; + } + } public static Lawyer? Create(LawyerBindingModel? model) { @@ -72,6 +88,38 @@ namespace CaseAccountingDataBaseImplement.Models SpecializationId = model.SpecializationId; } + public void UpdateContracts(CaseAccountingDatabase context, LawyerBindingModel model) + { + var lawyerContracts = context.LawyerContracts + .Where(x => x.LawyerId == model.Id) + .ToList(); + if (lawyerContracts != null) + { + context.LawyerContracts + .RemoveRange(lawyerContracts + .Where(x => !model.Contracts + .ContainsKey(x.ContractId)) + ); + context.SaveChanges(); + var lawyer = context.Lawyers + .First(x => x.Id == Id); + foreach (var lc in lawyerContracts) + { + model.Contracts.Remove(lc.ContractId); + } + foreach (var lc in model.Contracts) + { + context.LawyerContracts.Add(new LawyerContract + { + Lawyer = lawyer, + Contract = context.Contracts.First(x => x.Id == lc.Key), + }); + context.SaveChanges(); + } + _contracts = null; + } + } + public LawyerViewModel GetViewModel => new() { Id = Id, diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Models/LawyerContracts.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Models/LawyerContract.cs similarity index 100% rename from CaseAccounting/CaseAccountingDataBaseImplement/Models/LawyerContracts.cs rename to CaseAccounting/CaseAccountingDataBaseImplement/Models/LawyerContract.cs