diff --git a/LawCompany/LawCompanyDatabaseImplement/Implements/GuarantorStorage.cs b/LawCompany/LawCompanyDatabaseImplement/Implements/GuarantorStorage.cs index 08433df..6200376 100644 --- a/LawCompany/LawCompanyDatabaseImplement/Implements/GuarantorStorage.cs +++ b/LawCompany/LawCompanyDatabaseImplement/Implements/GuarantorStorage.cs @@ -1,4 +1,9 @@ -using System; +using LawCompanyContracts.BindingModels; +using LawCompanyContracts.SearchModels; +using LawCompanyContracts.StoragesContracts; +using LawCompanyContracts.ViewModels; +using LawCompanyDatabaseImplement.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +11,93 @@ using System.Threading.Tasks; namespace LawCompanyDatabaseImplement.Implements { - internal class GuarantorStorage + public class GuarantorStorage : IGuarantorStorage { + public List GetFullList() + { + using var context = new LawCompanyDatabase(); + 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 LawCompanyDatabase(); + return context.Guarantors + .Where(x => x.Email.Equals(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + using var context = new LawCompanyDatabase(); + 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 LawCompanyDatabase(); + + 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 LawCompanyDatabase(); + 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 LawCompanyDatabase(); + 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 LawCompanyDatabase(); + var element = context.Guarantors.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Guarantors.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } -} +} \ No newline at end of file diff --git a/LawCompany/LawCompanyDatabaseImplement/Implements/HearingStorage.cs b/LawCompany/LawCompanyDatabaseImplement/Implements/HearingStorage.cs index 89dbe62..a329865 100644 --- a/LawCompany/LawCompanyDatabaseImplement/Implements/HearingStorage.cs +++ b/LawCompany/LawCompanyDatabaseImplement/Implements/HearingStorage.cs @@ -1,12 +1,116 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using LawCompanyContracts.BindingModels; +using LawCompanyContracts.SearchModels; +using LawCompanyContracts.StoragesContracts; +using LawCompanyContracts.ViewModels; +using LawCompanyDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace LawCompanyDatabaseImplement.Implements { - internal class HearingStorage + public class HearingStorage : IHearingStorage { + public List GetFullList() + { + using var context = new LawCompanyDatabase(); + 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 LawCompanyDatabase(); + 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 LawCompanyDatabase(); + 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 LawCompanyDatabase(); + 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 LawCompanyDatabase(); + 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 LawCompanyDatabase(); + 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 LawCompanyDatabase(); + 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; + } } -} +} \ No newline at end of file diff --git a/LawCompany/LawCompanyDatabaseImplement/Implements/LawyerStorage.cs b/LawCompany/LawCompanyDatabaseImplement/Implements/LawyerStorage.cs index d8a6120..6a21383 100644 --- a/LawCompany/LawCompanyDatabaseImplement/Implements/LawyerStorage.cs +++ b/LawCompany/LawCompanyDatabaseImplement/Implements/LawyerStorage.cs @@ -1,12 +1,110 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using LawCompanyContracts.BindingModels; +using LawCompanyContracts.SearchModels; +using LawCompanyContracts.StoragesContracts; +using LawCompanyContracts.ViewModels; +using LawCompanyDatabaseImplement.Models; namespace LawCompanyDatabaseImplement.Implements { - internal class LawyerStorage + public class LawyerStorage : ILawyerStorage { + public List GetFullList() + { + using var context = new LawCompanyDatabase(); + 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 LawCompanyDatabase(); + return context.Lawyers + .Where(x => x.Email.Equals(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.GuarantorId.HasValue) + { + using var context = new LawCompanyDatabase(); + return context.Lawyers + .Where(x => x.GuarantorId.Equals(model.GuarantorId)) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + using var context = new LawCompanyDatabase(); + 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 LawCompanyDatabase(); + 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 LawCompanyDatabase(); + 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 LawCompanyDatabase(); + 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 LawCompanyDatabase(); + 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 LawCompanyDatabase(); + return context.HearingLawyers.Where(x => x.HearingId == model.Id).Select(x => x.Lawyer.GetViewModel).ToList(); + + } + public List GetLawyerConsultationList(ConsultationSearchModel model) + { + using var context = new LawCompanyDatabase(); + return context.ConsultationLawyers.Where(x => x.ConsultationId == model.Id).Select(x => x.Lawyer.GetViewModel).ToList(); + + } } -} +} \ No newline at end of file diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/Consultation.cs b/LawCompany/LawCompanyDatabaseImplement/Models/Consultation.cs index 7eb846e..0eef22e 100644 --- a/LawCompany/LawCompanyDatabaseImplement/Models/Consultation.cs +++ b/LawCompany/LawCompanyDatabaseImplement/Models/Consultation.cs @@ -1,12 +1,117 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using LawCompanyContracts.BindingModels; +using LawCompanyContracts.ViewModels; +using LawCompanyDatabaseImplement.Models; +using LawCompanyDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; namespace LawCompanyDatabaseImplement.Models { - internal class Consultation + 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 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 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; + } } -} +} \ No newline at end of file diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/ConsultationLawyer.cs b/LawCompany/LawCompanyDatabaseImplement/Models/ConsultationLawyer.cs index 1ce5c5b..c5c7e28 100644 --- a/LawCompany/LawCompanyDatabaseImplement/Models/ConsultationLawyer.cs +++ b/LawCompany/LawCompanyDatabaseImplement/Models/ConsultationLawyer.cs @@ -1,12 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; namespace LawCompanyDatabaseImplement.Models { - internal class ConsultationLawyer + 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(); } -} +} \ No newline at end of file diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/Guarantor.cs b/LawCompany/LawCompanyDatabaseImplement/Models/Guarantor.cs index d687b95..a7c1560 100644 --- a/LawCompany/LawCompanyDatabaseImplement/Models/Guarantor.cs +++ b/LawCompany/LawCompanyDatabaseImplement/Models/Guarantor.cs @@ -1,12 +1,63 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using LawCompanyContracts.BindingModels; +using LawCompanyContracts.ViewModels; +using LawCompanyDataModels.Models; +using System.ComponentModel.DataAnnotations; -namespace LawCompanyDatabaseImplement.Models +namespace LawFirmDatabaseImplement.Models { - internal class Guarantor + 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, + }; } -} +} \ No newline at end of file diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/Hearing.cs b/LawCompany/LawCompanyDatabaseImplement/Models/Hearing.cs index a538f10..a0b20c1 100644 --- a/LawCompany/LawCompanyDatabaseImplement/Models/Hearing.cs +++ b/LawCompany/LawCompanyDatabaseImplement/Models/Hearing.cs @@ -1,12 +1,99 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.Design; +using LawCompanyContracts.BindingModels; +using LawCompanyContracts.ViewModels; +using LawCompanyDataModels.Models; +using LawCompanyDataModels.Enums; namespace LawCompanyDatabaseImplement.Models { - internal class Hearing + 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 LawCompanyDatabase(); + _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(LawCompanyDatabase 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(LawCompanyDatabase 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; + } } -} +} \ No newline at end of file diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/HearingLawyer.cs b/LawCompany/LawCompanyDatabaseImplement/Models/HearingLawyer.cs index dae9a83..8eaeeb6 100644 --- a/LawCompany/LawCompanyDatabaseImplement/Models/HearingLawyer.cs +++ b/LawCompany/LawCompanyDatabaseImplement/Models/HearingLawyer.cs @@ -1,12 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; namespace LawCompanyDatabaseImplement.Models { - internal class HearingLawyer + 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(); } -} +} \ No newline at end of file diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/Lawyer.cs b/LawCompany/LawCompanyDatabaseImplement/Models/Lawyer.cs index 84c79f1..9ed3b5f 100644 --- a/LawCompany/LawCompanyDatabaseImplement/Models/Lawyer.cs +++ b/LawCompany/LawCompanyDatabaseImplement/Models/Lawyer.cs @@ -1,12 +1,71 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using LawCompanyContracts.BindingModels; +using LawCompanyContracts.ViewModels; +using LawCompanyDatabaseImplement.Models; +using LawCompanyDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; -namespace LawCompanyDatabaseImplement.Models + +namespace LawFirmDatabaseImplement.Models { - internal class Lawyer + 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 + }; } -} +} \ No newline at end of file