117 lines
3.2 KiB
C#
117 lines
3.2 KiB
C#
|
using LawFimDataModels.Models;
|
|||
|
using LawFirmContracts.BindingModels;
|
|||
|
using LawFirmContracts.ViewModels;
|
|||
|
using System.ComponentModel.DataAnnotations.Schema;
|
|||
|
using System.ComponentModel.DataAnnotations;
|
|||
|
|
|||
|
namespace LawFirmDatabaseImplement.Models
|
|||
|
{
|
|||
|
public class Consultation : IConsultationModel
|
|||
|
{
|
|||
|
public int Id { get; private set; }
|
|||
|
[Required]
|
|||
|
public double Cost { get; private set; }
|
|||
|
[Required]
|
|||
|
public DateTime ConsultationDate { get; private set; }
|
|||
|
[Required]
|
|||
|
public int CaseId { get; private set; }
|
|||
|
public Case Case { get; private set; }
|
|||
|
public int GuarantorId { get; set; }
|
|||
|
|
|||
|
private Dictionary<int, ILawyerModel>? _consultationLawyers = null;
|
|||
|
[NotMapped]
|
|||
|
public Dictionary<int, ILawyerModel> ConsultationLawyers
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_consultationLawyers == null)
|
|||
|
{
|
|||
|
using var context = new LawFirmDatabase();
|
|||
|
_consultationLawyers = Lawyers
|
|||
|
.ToDictionary(x => x.LawyerId, x => (context.Lawyers
|
|||
|
.FirstOrDefault(y => y.Id == x.LawyerId) as ILawyerModel));
|
|||
|
}
|
|||
|
return _consultationLawyers;
|
|||
|
}
|
|||
|
}
|
|||
|
[ForeignKey("ConsultationId")]
|
|||
|
public virtual List<ConsultationLawyer> Lawyers { get; set; } = new();
|
|||
|
public static Consultation? Create(LawFirmDatabase context, ConsultationBindingModel? model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
var consultations = context.Consultations.Where(x => x.CaseId == model.CaseId).ToList();
|
|||
|
if (consultations.Count > 0)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return new Consultation()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
Cost = model.Cost,
|
|||
|
ConsultationDate = model.ConsultationDate,
|
|||
|
CaseId = model.CaseId,
|
|||
|
Case = context.Cases.First(x => x.Id == model.CaseId),
|
|||
|
GuarantorId = model.GuarantorId,
|
|||
|
Lawyers = model.ConsultationLawyers.Select(x => new ConsultationLawyer
|
|||
|
{
|
|||
|
Lawyer = context.Lawyers.First(y => y.Id == x.Key)
|
|||
|
}).ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
public void Update(ConsultationBindingModel? model)
|
|||
|
{
|
|||
|
using var context = new LawFirmDatabase();
|
|||
|
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
Cost = model.Cost;
|
|||
|
ConsultationDate = model.ConsultationDate;
|
|||
|
CaseId = model.CaseId;
|
|||
|
}
|
|||
|
public ConsultationViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
Cost = Cost,
|
|||
|
ConsultationDate = ConsultationDate,
|
|||
|
CaseId = CaseId,
|
|||
|
GuarantorId = GuarantorId,
|
|||
|
ConsultationLawyers = ConsultationLawyers
|
|||
|
};
|
|||
|
public void UpdateLawyers(LawFirmDatabase context,
|
|||
|
ConsultationBindingModel model)
|
|||
|
{
|
|||
|
var consultationLawyers = context.ConsultationLawyers
|
|||
|
.Where(rec => rec.ConsultationId == model.Id)
|
|||
|
.ToList();
|
|||
|
if (consultationLawyers != null && consultationLawyers.Count > 0)
|
|||
|
{
|
|||
|
context.ConsultationLawyers
|
|||
|
.RemoveRange(consultationLawyers
|
|||
|
.Where(rec => !model.ConsultationLawyers
|
|||
|
.ContainsKey(rec.LawyerId)));
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
var _consultation = context.Consultations.First(x => x.Id == Id);
|
|||
|
foreach (var pc in model.ConsultationLawyers)
|
|||
|
{
|
|||
|
if (!ConsultationLawyers.ContainsKey(pc.Key))
|
|||
|
{
|
|||
|
context.ConsultationLawyers.Add(new ConsultationLawyer
|
|||
|
{
|
|||
|
Consultation = _consultation,
|
|||
|
Lawyer = context.Lawyers.First(x => x.Id == pc.Key),
|
|||
|
});
|
|||
|
}
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
_consultationLawyers = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|