PIbd23_Ivanova_Yakobchuk_Co.../LawCompany/LawCompanyDatabaseImplement/Models/Consultation.cs
2024-05-01 17:41:34 +04:00

117 lines
4.2 KiB
C#

using LawCompanyContracts.BindingModels;
using LawCompanyContracts.ViewModels;
using LawCompanyDatabaseImplement.Models;
using LawCompanyDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace LawCompanyDatabaseImplement.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 LawCompanyDatabase();
_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(LawCompanyDatabase 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 LawCompanyDatabase();
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(LawCompanyDatabase 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;
}
}
}