Исправил datebase
This commit is contained in:
parent
e7ae669c08
commit
0b1f1dfccf
@ -44,7 +44,7 @@ namespace CaseAccountingDataBaseImplement
|
||||
|
||||
public virtual DbSet<Lawyer> Lawyers { set; get; }
|
||||
|
||||
public virtual DbSet<LawyerContracts> LawyerContracts { set; get; }
|
||||
public virtual DbSet<LawyerContract> LawyerContracts { set; get; }
|
||||
|
||||
public virtual DbSet<Specialization> Specializations { get; set; }
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,23 @@ namespace CaseAccountingDataBaseImplement.Models
|
||||
public virtual List<Hearing> Hearings { get; set; } = new();
|
||||
|
||||
[ForeignKey("CaseId")]
|
||||
public virtual List<CaseLawyer> Lawyers { get; set; } = new();
|
||||
public virtual List<CaseLawyer> CaseLawyers { get; set; } = new();
|
||||
|
||||
private Dictionary<int, ILawyerModel>? _lawyers;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, ILawyerModel> Lawyers
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_lawyers == null)
|
||||
{
|
||||
_lawyers = CaseLawyers.ToDictionary(
|
||||
x => x.LawyerId, x => x.Lawyer as ILawyerModel);
|
||||
}
|
||||
return _lawyers;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("CaseId")]
|
||||
public virtual List<CaseDeal> 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<int, ILawyerModel> ICaseModel.Lawyers => throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
@ -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> LawyerContracts { get; set; } = new();
|
||||
public virtual List<LawyerContract> LawyerContracts { get; set; } = new();
|
||||
|
||||
[ForeignKey("ContractId")]
|
||||
public virtual List<DealContract> DealContracts { get; set; } = new();
|
||||
|
||||
private Dictionary<int, IDealModel>? _deals;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, IDealModel> 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,
|
||||
|
@ -32,6 +32,22 @@ namespace CaseAccountingDataBaseImplement.Models
|
||||
[ForeignKey("DealId")]
|
||||
public virtual List<CaseDeal> CaseDeals { get; set; } = new();
|
||||
|
||||
private Dictionary<int, ICaseModel>? _cases;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, ICaseModel> Cases
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_cases == null)
|
||||
{
|
||||
_cases = CaseDeals.ToDictionary(
|
||||
x => x.CaseId, x => x.Case as ICaseModel);
|
||||
}
|
||||
return _cases;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("DealId")]
|
||||
public virtual List<DealContract> 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,
|
||||
|
@ -39,7 +39,23 @@ namespace CaseAccountingDataBaseImplement.Models
|
||||
public virtual List<CaseLawyer> CaseLawyers { get; set; } = new();
|
||||
|
||||
[ForeignKey("LawyerId")]
|
||||
public virtual List<LawyerContracts> Contracts { get; set; } = new();
|
||||
public virtual List<LawyerContract> LawyerContracts { get; set; } = new();
|
||||
|
||||
private Dictionary<int, IContractModel>? _contracts;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, IContractModel> 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,
|
||||
|
Loading…
Reference in New Issue
Block a user