PIbd-22_Fedorenko_Puchkina_.../LawFim/LawFirmDatabaseImplement/Models/Consultation.cs
2024-04-27 22:16:36 +04:00

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;
[ForeignKey("ConsultationId")]
public virtual List<ConsultationLawyer> Lawyers { get; set; } = new();
[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;
}
}
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;
}
}
}