From 45a6a528822e863c1cdc6ffcd484f4bb3afe6ee4 Mon Sep 17 00:00:00 2001 From: "a.puchkina" Date: Sat, 27 Apr 2024 21:24:12 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=80=D1=83=D1=87=D0=B8=D1=82=D0=B5=D0=BB=D1=8C?= =?UTF-8?q?=D1=81=D0=BA=D0=BE=D0=B3=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/HearingSearchModel.cs | 2 - .../Implements/ConsultationStorage.cs | 28 +++-- .../Implements/GuarantorStorage.cs | 98 +++++++++++++++ .../Implements/HearingStorage.cs | 116 ++++++++++++++++++ .../Implements/LawyerStorage.cs | 110 +++++++++++++++++ .../Models/Consultation.cs | 116 ++++++++++++++++++ .../Models/ConsultationLawyer.cs | 15 +++ .../Models/Guarantor.cs | 63 ++++++++++ .../Models/Hearing.cs | 99 +++++++++++++++ .../Models/HearingLawyer.cs | 15 +++ .../LawFirmDatabaseImplement/Models/Lawyer.cs | 70 +++++++++++ 11 files changed, 717 insertions(+), 15 deletions(-) create mode 100644 LawFim/LawFirmDatabaseImplement/Implements/GuarantorStorage.cs create mode 100644 LawFim/LawFirmDatabaseImplement/Implements/HearingStorage.cs create mode 100644 LawFim/LawFirmDatabaseImplement/Implements/LawyerStorage.cs create mode 100644 LawFim/LawFirmDatabaseImplement/Models/Consultation.cs create mode 100644 LawFim/LawFirmDatabaseImplement/Models/ConsultationLawyer.cs create mode 100644 LawFim/LawFirmDatabaseImplement/Models/Guarantor.cs create mode 100644 LawFim/LawFirmDatabaseImplement/Models/Hearing.cs create mode 100644 LawFim/LawFirmDatabaseImplement/Models/HearingLawyer.cs create mode 100644 LawFim/LawFirmDatabaseImplement/Models/Lawyer.cs diff --git a/LawFim/LawFirmContracts/SearchModels/HearingSearchModel.cs b/LawFim/LawFirmContracts/SearchModels/HearingSearchModel.cs index 046d1f3..d55b3ec 100644 --- a/LawFim/LawFirmContracts/SearchModels/HearingSearchModel.cs +++ b/LawFim/LawFirmContracts/SearchModels/HearingSearchModel.cs @@ -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; } } } diff --git a/LawFim/LawFirmDatabaseImplement/Implements/ConsultationStorage.cs b/LawFim/LawFirmDatabaseImplement/Implements/ConsultationStorage.cs index 94b67f0..5903d9f 100644 --- a/LawFim/LawFirmDatabaseImplement/Implements/ConsultationStorage.cs +++ b/LawFim/LawFirmDatabaseImplement/Implements/ConsultationStorage.cs @@ -21,17 +21,17 @@ namespace LawFirmDatabaseImplement.Implements } public List 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) { diff --git a/LawFim/LawFirmDatabaseImplement/Implements/GuarantorStorage.cs b/LawFim/LawFirmDatabaseImplement/Implements/GuarantorStorage.cs new file mode 100644 index 0000000..8853de5 --- /dev/null +++ b/LawFim/LawFirmDatabaseImplement/Implements/GuarantorStorage.cs @@ -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 GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Guarantors + .Select(x => x.GetViewModel) + .ToList(); + } + + public List 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; + } + } +} diff --git a/LawFim/LawFirmDatabaseImplement/Implements/HearingStorage.cs b/LawFim/LawFirmDatabaseImplement/Implements/HearingStorage.cs new file mode 100644 index 0000000..4865c15 --- /dev/null +++ b/LawFim/LawFirmDatabaseImplement/Implements/HearingStorage.cs @@ -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 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 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; + } + } +} diff --git a/LawFim/LawFirmDatabaseImplement/Implements/LawyerStorage.cs b/LawFim/LawFirmDatabaseImplement/Implements/LawyerStorage.cs new file mode 100644 index 0000000..95ede52 --- /dev/null +++ b/LawFim/LawFirmDatabaseImplement/Implements/LawyerStorage.cs @@ -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 GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Lawyers + .Select(x => x.GetViewModel) + .ToList(); + } + public List 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 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 GetLawyerConsultationList(ConsultationSearchModel model) + { + using var context = new LawFirmDatabase(); + return context.ConsultationLawyers.Where(x => x.ConsultationId == model.Id).Select(x => x.Lawyer.GetViewModel).ToList(); + + } + } +} diff --git a/LawFim/LawFirmDatabaseImplement/Models/Consultation.cs b/LawFim/LawFirmDatabaseImplement/Models/Consultation.cs new file mode 100644 index 0000000..6960378 --- /dev/null +++ b/LawFim/LawFirmDatabaseImplement/Models/Consultation.cs @@ -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? _consultationLawyers = null; + [NotMapped] + public Dictionary 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 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; + } + } +} diff --git a/LawFim/LawFirmDatabaseImplement/Models/ConsultationLawyer.cs b/LawFim/LawFirmDatabaseImplement/Models/ConsultationLawyer.cs new file mode 100644 index 0000000..5bea511 --- /dev/null +++ b/LawFim/LawFirmDatabaseImplement/Models/ConsultationLawyer.cs @@ -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(); + } +} diff --git a/LawFim/LawFirmDatabaseImplement/Models/Guarantor.cs b/LawFim/LawFirmDatabaseImplement/Models/Guarantor.cs new file mode 100644 index 0000000..1bfa0e8 --- /dev/null +++ b/LawFim/LawFirmDatabaseImplement/Models/Guarantor.cs @@ -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, + }; + } +} diff --git a/LawFim/LawFirmDatabaseImplement/Models/Hearing.cs b/LawFim/LawFirmDatabaseImplement/Models/Hearing.cs new file mode 100644 index 0000000..a428dc5 --- /dev/null +++ b/LawFim/LawFirmDatabaseImplement/Models/Hearing.cs @@ -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? _hearingLawyers = null; + [NotMapped] + public Dictionary 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 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; + } + } +} diff --git a/LawFim/LawFirmDatabaseImplement/Models/HearingLawyer.cs b/LawFim/LawFirmDatabaseImplement/Models/HearingLawyer.cs new file mode 100644 index 0000000..349018f --- /dev/null +++ b/LawFim/LawFirmDatabaseImplement/Models/HearingLawyer.cs @@ -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(); + } +} diff --git a/LawFim/LawFirmDatabaseImplement/Models/Lawyer.cs b/LawFim/LawFirmDatabaseImplement/Models/Lawyer.cs new file mode 100644 index 0000000..cf50a3f --- /dev/null +++ b/LawFim/LawFirmDatabaseImplement/Models/Lawyer.cs @@ -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 HearingLawyers { get; set; } = new(); + [ForeignKey("LawyerId")] + public virtual List 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 + }; + } +}