This commit is contained in:
Галина Федоренко 2024-04-27 21:31:17 +04:00
commit 99b8127bf8
11 changed files with 717 additions and 15 deletions

View File

@ -4,8 +4,6 @@
{
public int? Id { get; set; }
public DateTime? HearingDate { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public int? GuarantorId { get; set; }
}
}

View File

@ -21,17 +21,17 @@ namespace LawFirmDatabaseImplement.Implements
}
public List<ConsultationViewModel> GetFilteredList(ConsultationSearchModel model)
{
if (!model.Id.HasValue && !model.Cost.HasValue && !model.ConsultationDate.HasValue
&& !model.CaseId.HasValue && !model.GuarantorId.HasValue)
if (!model.Id.HasValue && !model.GuarantorId.HasValue)
{
return new();
}
if (!model.DateFrom.HasValue || !model.DateTo.HasValue)
if (!model.GuarantorId.HasValue)
{
using var context = new LawFirmDatabase();
return context.Consultations
.Include(x => x.Lawyers).ThenInclude(x => x.Lawyer)
.Where(x => x.Id == model.Id)
.Include(x => x.Lawyers)
.ThenInclude(x => x.Lawyer)
.Where(x => x.GuarantorId == model.GuarantorId)
.Select(x => x.GetViewModel)
.ToList();
}
@ -39,23 +39,25 @@ namespace LawFirmDatabaseImplement.Implements
{
using var context = new LawFirmDatabase();
return context.Consultations
.Include(x => x.Lawyers).ThenInclude(x => x.Lawyer)
.Where(x => x.ConsultationDate >= model.DateFrom && x.ConsultationDate <= model.DateTo)
.Include(x => x.Lawyers)
.ThenInclude(x => x.Lawyer)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
}
public ConsultationViewModel? GetElement(ConsultationSearchModel model)
{
if (!model.Id.HasValue && !model.ConsultationDate.HasValue && !model.CaseId.HasValue)
if (!model.Id.HasValue && !model.GuarantorId.HasValue)
{
return new();
return null;
}
using var context = new LawFirmDatabase();
return context.Consultations.Include(x => x.Lawyers).ThenInclude(x => x.Lawyer)
.FirstOrDefault(x => (model.CaseId.HasValue && x.Case == model.CaseId)
|| (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
return context.Consultations.
Include(x => x.Lawyers).
ThenInclude(x => x.Lawyer)
.FirstOrDefault(x => (model.GuarantorId.HasValue && x.GuarantorId == model.GuarantorId)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ConsultationViewModel? Insert(ConsultationBindingModel model)
{

View File

@ -0,0 +1,98 @@
using LawFirmContracts.BindingModels;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StoragesContracts;
using LawFirmContracts.ViewModels;
using LawFirmDatabaseImplement.Models;
namespace LawFirmDatabaseImplement.Implements
{
public class GuarantorStorage : IGuarantorStorage
{
public List<GuarantorViewModel> GetFullList()
{
using var context = new LawFirmDatabase();
return context.Guarantors
.Select(x => x.GetViewModel)
.ToList();
}
public List<GuarantorViewModel> GetFilteredList(GuarantorSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue && string.IsNullOrEmpty(model.FIO)
&& string.IsNullOrEmpty(model.Password))
{
return new();
}
if (!string.IsNullOrEmpty(model.Email))
{
using var context = new LawFirmDatabase();
return context.Guarantors
.Where(x => x.Email.Equals(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
else
{
using var context = new LawFirmDatabase();
return context.Guarantors
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
}
public GuarantorViewModel? GetElement(GuarantorSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue && string.IsNullOrEmpty(model.FIO)
&& string.IsNullOrEmpty(model.Password))
{
return null;
}
using var context = new LawFirmDatabase();
return context.Guarantors
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Email)
&& x.Email == model.Email) || (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public GuarantorViewModel? Insert(GuarantorBindingModel model)
{
using var context = new LawFirmDatabase();
var newGuarantor = Guarantor.Create(model);
if (newGuarantor == null)
{
return null;
}
context.Guarantors.Add(newGuarantor);
context.SaveChanges();
return newGuarantor.GetViewModel;
}
public GuarantorViewModel? Update(GuarantorBindingModel model)
{
using var context = new LawFirmDatabase();
var executor = context.Guarantors.FirstOrDefault(x => x.Id == model.Id);
if (executor == null)
{
return null;
}
executor.Update(model);
context.SaveChanges();
return executor.GetViewModel;
}
public GuarantorViewModel? Delete(GuarantorBindingModel model)
{
using var context = new LawFirmDatabase();
var element = context.Guarantors.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Guarantors.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,116 @@
using LawFirmContracts.BindingModels;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StoragesContracts;
using LawFirmContracts.ViewModels;
using LawFirmDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace LawFirmDatabaseImplement.Implements
{
public class HearingStorage : IHearingStorage
{
public List<HearingViewModel> GetFullList()
{
using var context = new LawFirmDatabase();
return context.Hearings
.Include(x => x.Lawyers)
.ThenInclude(x => x.Lawyer)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<HearingViewModel> GetFilteredList(HearingSearchModel model)
{
if (!model.Id.HasValue && !model.GuarantorId.HasValue)
{
return new();
}
if (!model.GuarantorId.HasValue)
{
using var context = new LawFirmDatabase();
return context.Hearings
.Include(x => x.Lawyers)
.ThenInclude(x => x.Lawyer)
.Where(x => x.GuarantorId == model.GuarantorId)
.Select(x => x.GetViewModel)
.ToList();
}
else
{
using var context = new LawFirmDatabase();
return context.Hearings
.Include(x => x.Lawyers)
.ThenInclude(x => x.Lawyer)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
}
public HearingViewModel? GetElement(HearingSearchModel model)
{
if (!model.Id.HasValue && !model.GuarantorId.HasValue)
{
return null;
}
using var context = new LawFirmDatabase();
return context.Hearings
.Include(x => x.Lawyers)
.ThenInclude(x => x.Lawyer)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id || (model.GuarantorId.HasValue
&& x.GuarantorId == model.GuarantorId))
?.GetViewModel;
}
public HearingViewModel? Insert(HearingBindingModel model)
{
using var context = new LawFirmDatabase();
var newHearing = Hearing.Create(context, model);
if (newHearing == null)
{
return null;
}
context.Hearings.Add(newHearing);
context.SaveChanges();
return newHearing.GetViewModel;
}
public HearingViewModel? Update(HearingBindingModel model)
{
using var context = new LawFirmDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var _case = context.Hearings.FirstOrDefault(rec =>
rec.Id == model.Id);
if (_case == null)
{
return null;
}
_case.Update(model);
context.SaveChanges();
_case.UpdateLawyers(context, model);
transaction.Commit();
return _case.GetViewModel;
}
catch
{
throw;
}
}
public HearingViewModel? Delete(HearingBindingModel model)
{
using var context = new LawFirmDatabase();
var element = context.Hearings
.Include(x => x.Lawyers)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Hearings.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,110 @@
using LawFirmContracts.BindingModels;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StoragesContracts;
using LawFirmContracts.ViewModels;
using LawFirmDatabaseImplement.Models;
namespace LawFirmDatabaseImplement.Implements
{
public class LawyerStorage : ILawyerStorage
{
public List<LawyerViewModel> GetFullList()
{
using var context = new LawFirmDatabase();
return context.Lawyers
.Select(x => x.GetViewModel)
.ToList();
}
public List<LawyerViewModel> GetFilteredList(LawyerSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue && !model.GuarantorId.HasValue && string.IsNullOrEmpty(model.FIO))
{
return new();
}
if (!string.IsNullOrEmpty(model.Email))
{
using var context = new LawFirmDatabase();
return context.Lawyers
.Where(x => x.Email.Equals(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.GuarantorId.HasValue)
{
using var context = new LawFirmDatabase();
return context.Lawyers
.Where(x => x.GuarantorId.Equals(model.GuarantorId))
.Select(x => x.GetViewModel)
.ToList();
}
else
{
using var context = new LawFirmDatabase();
return context.Lawyers
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
}
public LawyerViewModel? GetElement(LawyerSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue && !model.GuarantorId.HasValue
&& string.IsNullOrEmpty(model.FIO))
{
return null;
}
using var context = new LawFirmDatabase();
return context.Lawyers
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (model.GuarantorId.HasValue && x.GuarantorId == model.GuarantorId))
?.GetViewModel;
}
public LawyerViewModel? Insert(LawyerBindingModel model)
{
using var context = new LawFirmDatabase();
var newLawyer = Lawyer.Create(model);
if (newLawyer == null)
{
return null;
}
context.Lawyers.Add(newLawyer);
context.SaveChanges();
return newLawyer.GetViewModel;
}
public LawyerViewModel? Update(LawyerBindingModel model)
{
using var context = new LawFirmDatabase();
var lawyer = context.Lawyers.FirstOrDefault(x => x.Id == model.Id);
if (lawyer == null)
{
return null;
}
lawyer.Update(model);
context.SaveChanges();
return lawyer.GetViewModel;
}
public LawyerViewModel? Delete(LawyerBindingModel model)
{
using var context = new LawFirmDatabase();
var element = context.Lawyers.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Lawyers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public List<LawyerViewModel> GetLawyerHearingList(HearingSearchModel model)
{
using var context = new LawFirmDatabase();
return context.HearingLawyers.Where(x => x.HearingId == model.Id).Select(x => x.Lawyer.GetViewModel).ToList();
}
public List<LawyerViewModel> GetLawyerConsultationList(ConsultationSearchModel model)
{
using var context = new LawFirmDatabase();
return context.ConsultationLawyers.Where(x => x.ConsultationId == model.Id).Select(x => x.Lawyer.GetViewModel).ToList();
}
}
}

View File

@ -0,0 +1,116 @@
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;
}
}
}

View File

@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
namespace LawFirmDatabaseImplement.Models
{
public class ConsultationLawyer
{
public int Id { get; set; }
[Required]
public int ConsultationId { get; set; }
[Required]
public int LawyerId { get; set; }
public virtual Consultation Consultation { get; set; } = new();
public virtual Lawyer Lawyer { get; set; } = new();
}
}

View File

@ -0,0 +1,63 @@
using LawFimDataModels.Models;
using LawFirmContracts.BindingModels;
using LawFirmContracts.ViewModels;
using System.ComponentModel.DataAnnotations;
namespace LawFirmDatabaseImplement.Models
{
public class Guarantor : IGuarantorModel
{
public int Id { get; private set; }
[Required]
public string FIO { get; private set; } = string.Empty;
[Required]
public string Email { get; private set; } = string.Empty;
[Required]
public string Password { get; private set; } = string.Empty;
public static Guarantor? Create(GuarantorBindingModel? model)
{
if (model == null)
{
return null;
}
return new Guarantor()
{
Id = model.Id,
FIO = model.FIO,
Email = model.Email,
Password = model.Password,
};
}
public static Guarantor Create(GuarantorViewModel model)
{
return new Guarantor
{
Id = model.Id,
FIO = model.FIO,
Email = model.Email,
Password = model.Password,
};
}
public void Update(GuarantorBindingModel? model)
{
if (model == null)
{
return;
}
FIO = model.FIO;
Email = model.Email;
Password = model.Password;
}
public GuarantorViewModel GetViewModel => new()
{
Id = Id,
FIO = FIO,
Email = Email,
Password = Password,
};
}
}

View File

@ -0,0 +1,99 @@
using LawFimDataModels.Enums;
using LawFimDataModels.Models;
using LawFirmContracts.BindingModels;
using LawFirmContracts.ViewModels;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.Design;
namespace LawFirmDatabaseImplement.Models
{
public class Hearing :IHearingModel
{
public int Id { get; private set; }
[Required]
public DateTime HearingDate { get; private set; }
[Required]
public string Judge{ get; private set; } = string.Empty;
public int GuarantorId { get; set; }
private Dictionary<int, ILawyerModel>? _hearingLawyers = null;
[NotMapped]
public Dictionary<int, ILawyerModel> HearingLawyers
{
get
{
if (_hearingLawyers == null)
{
using var context = new LawFirmDatabase();
_hearingLawyers = Lawyers
.ToDictionary(x => x.LawyerId, x => (context.Lawyers
.FirstOrDefault(y => y.Id == x.LawyerId) as ILawyerModel));
}
return _hearingLawyers;
}
}
[ForeignKey("HearingId")]
public virtual List<HearingLawyer> Lawyers { get; set; } = new();
public static Hearing? Create(LawFirmDatabase context, HearingBindingModel? model)
{
if (model == null)
{
return null;
}
return new Hearing()
{
Id = model.Id,
HearingDate = model.HearingDate,
Judge = model.Judge,
GuarantorId = model.GuarantorId,
Lawyers = model.HearingLawyers.Select(x => new HearingLawyer
{
Lawyer = context.Lawyers.First(y => y.Id == x.Key)
}).ToList()
};
}
public void Update(HearingBindingModel? model)
{
if (model == null)
{
return;
}
HearingDate = model.HearingDate;
Judge = model.Judge;
}
public HearingViewModel GetViewModel => new()
{
Id = Id,
HearingDate = HearingDate,
Judge = Judge,
GuarantorId = GuarantorId,
HearingLawyers = HearingLawyers
};
public void UpdateLawyers(LawFirmDatabase context, HearingBindingModel model)
{
var hearingLawyer = context.HearingLawyers
.Where(rec => rec.HearingId == model.Id)
.ToList();
if (hearingLawyer != null && hearingLawyer.Count > 0)
{
context.HearingLawyers.RemoveRange(hearingLawyer.Where(rec
=> !model.HearingLawyers.ContainsKey(rec.LawyerId)));
context.SaveChanges();
}
var _hearing = context.Hearings.First(x => x.Id == Id);
foreach (var pc in model.HearingLawyers)
{
if (!HearingLawyers.ContainsKey(pc.Key))
{
context.HearingLawyers.Add(new HearingLawyer
{
Hearing = _hearing,
Lawyer = context.Lawyers.First(x => x.Id == pc.Key),
});
}
context.SaveChanges();
}
_hearingLawyers = null;
}
}
}

View File

@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
namespace LawFirmDatabaseImplement.Models
{
public class HearingLawyer
{
public int Id { get; set; }
[Required]
public int HearingId { get; set; }
[Required]
public int LawyerId { get; set; }
public virtual Hearing Hearing { get; set; } = new();
public virtual Lawyer Lawyer { get; set; } = new();
}
}

View File

@ -0,0 +1,70 @@
using LawFimDataModels.Models;
using LawFirmContracts.BindingModels;
using LawFirmContracts.ViewModels;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace LawFirmDatabaseImplement.Models
{
public class Lawyer : ILawyerModel
{
public int Id { get; private set; }
[Required]
public string FIO { get; private set; } = string.Empty;
[Required]
public string Email { get; private set; } = string.Empty;
[Required]
public string Phone { get; private set; } = string.Empty;
[ForeignKey("LawyerId")]
public virtual List<HearingLawyer> HearingLawyers { get; set; } = new();
[ForeignKey("LawyerId")]
public virtual List<ConsultationLawyer> LawyerConsultations { get; set; } = new();
public int GuarantorId { get; set; }
public static Lawyer? Create(LawyerBindingModel? model)
{
if (model == null)
{
return null;
}
return new Lawyer()
{
Id = model.Id,
FIO = model.FIO,
Email = model.Email,
Phone = model.Phone,
GuarantorId = model.GuarantorId,
};
}
public static Lawyer Create(LawyerViewModel model)
{
return new Lawyer
{
Id = model.Id,
FIO = model.FIO,
Email = model.Email,
Phone = model.Phone,
GuarantorId = model.GuarantorId,
};
}
public void Update(LawyerBindingModel? model)
{
if (model == null)
{
return;
}
FIO = model.FIO;
Email = model.Email;
Phone = model.Phone;
}
public LawyerViewModel GetViewModel => new()
{
Id = Id,
FIO = FIO,
Email = Email,
Phone = Phone,
GuarantorId = GuarantorId
};
}
}