diff --git a/LawCompany/LawCompanyBusinessLogic/BusinessLogics/HearingLogic.cs b/LawCompany/LawCompanyBusinessLogic/BusinessLogics/HearingLogic.cs index 08c0aef..29cfe35 100644 --- a/LawCompany/LawCompanyBusinessLogic/BusinessLogics/HearingLogic.cs +++ b/LawCompany/LawCompanyBusinessLogic/BusinessLogics/HearingLogic.cs @@ -1,139 +1,146 @@ -using LawCompanyContracts.BindingModels; +using LawCompanyDataModels.Models; +using LawCompanyContracts.BindingModels; using LawCompanyContracts.BusinessLogicContracts; using LawCompanyContracts.SearchModels; using LawCompanyContracts.StoragesContracts; using LawCompanyContracts.ViewModels; -using LawCompanyDataModels.Models; using Microsoft.Extensions.Logging; namespace LawCompanyBusinessLogic.BusinessLogics { - public class HearingLogic : IHearingLogic - { - private readonly ILogger _logger; - private readonly IHearingStorage _hearingStorage; + public class HearingLogic : IHearingLogic + { + private readonly ILogger _logger; + private readonly IHearingStorage _hearingStorage; - public HearingLogic(ILogger logger, IHearingStorage hearingStorage) - { - _logger = logger; - _hearingStorage = hearingStorage; - } + public HearingLogic(ILogger logger, IHearingStorage hearingStorage) + { + _logger = logger; + _hearingStorage = hearingStorage; + } - public bool Create(HearingBindingModel model) - { - CheckModel(model); - if (_hearingStorage.Insert(model) == null) - { - _logger.LogWarning("Insert operation failed"); - return false; - } - return true; - } + public bool Create(HearingBindingModel model) + { + CheckModel(model); + if (_hearingStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } - public bool Delete(HearingBindingModel model) - { - CheckModel(model, false); - _logger.LogInformation("Delete. Id:{Id}", model.Id); - if (_hearingStorage.Delete(model) == null) - { - _logger.LogWarning("Delete operation failed"); - return false; - } - return true; - } + public bool Delete(HearingBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_hearingStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } - public HearingViewModel? ReadElement(HearingSearchModel model) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - _logger.LogInformation("ReadElement. HearingDate:{HearingDate}. Id:{Id}", model.HearingDate, model.Id); - var element = _hearingStorage.GetElement(model); - if (element == null) - { - _logger.LogWarning("ReadElement element not found"); - return null; - } - _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); - return element; - } + public HearingViewModel? ReadElement(HearingSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. HearingDate:{HearingDate}. Id:{Id}", model.HearingDate, model.Id); + var element = _hearingStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } - public List? ReadList(HearingSearchModel? model) - { - _logger.LogInformation("ReadList. HearingDate:{HearingDate}.Id:{Id}", model?.HearingDate, model?.Id); - var list = model == null ? _hearingStorage.GetFullList() : _hearingStorage.GetFilteredList(model); - if (list == null) - { - _logger.LogWarning("ReadList return null list"); - return null; - } - _logger.LogInformation("ReadList. Count:{Count}", list.Count); - return list; - } + public List? ReadList(HearingSearchModel? model) + { + _logger.LogInformation("ReadList. HearingDate:{HearingDate}.Id:{Id}", model?.HearingDate, model?.Id); + var list = model == null ? _hearingStorage.GetFullList() : _hearingStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } - public bool Update(HearingBindingModel model) - { - CheckModel(model); - if (_hearingStorage.Update(model) == null) - { - _logger.LogWarning("Update operation failed"); - return false; - } - return true; - } - public bool AddLawyerToHearing(HearingSearchModel model, ILawyerModel lawyer) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } + public bool Update(HearingBindingModel model) + { + CheckModel(model); + if (_hearingStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool AddLawyerToHearing(HearingSearchModel model, ILawyerModel lawyer) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } - var element = _hearingStorage.GetElement(model); + var element = _hearingStorage.GetElement(model); - if (element == null) - { - return false; - } + if (element == null) + { + return false; + } + if (element.HearingLawyers.ContainsKey(lawyer.Id)) + { + return false; + } + element.HearingLawyers[lawyer.Id] = lawyer; - element.HearingLawyers[lawyer.Id] = lawyer; + _hearingStorage.Update(new() + { + Id = element.Id, + HearingDate = element.HearingDate, + Judge = element.Judge, + HearingLawyers = element.HearingLawyers, + GuarantorId = element.GuarantorId, + }); - _hearingStorage.Update(new() - { - Id = element.Id, - HearingDate = element.HearingDate, - Judge = element.Judge, - HearingLawyers = element.HearingLawyers, - GuarantorId = element.GuarantorId, - }); + return true; + } + private void CheckModel(HearingBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Judge)) + { + throw new ArgumentNullException("Не указан суд", + nameof(model.Judge)); + } + if (string.IsNullOrEmpty((model.HearingDate).ToString())) + { + throw new ArgumentNullException("Не поставлено время", nameof(model.HearingDate)); + } + _logger.LogInformation("Hearing. HearingDate:{HearingDate}. Id: {Id} ", model.HearingDate, model.Id); + var element = _hearingStorage.GetElement(new HearingSearchModel + { + HearingDate = model.HearingDate - return true; - } - private void CheckModel(HearingBindingModel model, bool withParams = true) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - if (!withParams) - { - return; - } - if (string.IsNullOrEmpty((model.HearingDate).ToString())) - { - throw new ArgumentNullException("Не поставлено время", nameof(model.HearingDate)); - } - - _logger.LogInformation("Hearing. HearingDate:{HearingDate}. Id: {Id} ", model.HearingDate, model.Id); - var element = _hearingStorage.GetElement(new HearingSearchModel - { - HearingDate = model.HearingDate - - }); - if (element != null && element.Id != model.Id) - { - throw new InvalidOperationException("На данное время уже назначено слушание"); - } - } - } -} \ No newline at end of file + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("На данное время уже назначено слушание"); + } + } + } +} diff --git a/LawCompany/LawCompanyContracts/BindingModels/HearingBindingModel.cs b/LawCompany/LawCompanyContracts/BindingModels/HearingBindingModel.cs index 905f0a5..df8bf7c 100644 --- a/LawCompany/LawCompanyContracts/BindingModels/HearingBindingModel.cs +++ b/LawCompany/LawCompanyContracts/BindingModels/HearingBindingModel.cs @@ -2,12 +2,12 @@ namespace LawCompanyContracts.BindingModels { - public class HearingBindingModel : IHearingModel - { - public int Id { get; set; } - public DateTime HearingDate { get; set; } - public string Judge { get; set; } = string.Empty; - public int GuarantorId { get; set; } - public Dictionary HearingLawyers { get; set; } = new(); - } + public class HearingBindingModel : IHearingModel + { + public int Id { get; set; } + public DateTime HearingDate { get; set; } + public string Judge { get; set; } = string.Empty; + public int GuarantorId { get; set; } + public Dictionary HearingLawyers { get; set; } = new(); + } } diff --git a/LawCompany/LawCompanyContracts/BindingModels/LawyerBindingModel.cs b/LawCompany/LawCompanyContracts/BindingModels/LawyerBindingModel.cs index 3dc9c83..19090b9 100644 --- a/LawCompany/LawCompanyContracts/BindingModels/LawyerBindingModel.cs +++ b/LawCompany/LawCompanyContracts/BindingModels/LawyerBindingModel.cs @@ -8,6 +8,6 @@ namespace LawCompanyContracts.BindingModels public string FIO { get; set; } = string.Empty; public string Phone { get; set; } = string.Empty; public string Email { get; set; } = string.Empty; - public int GuarantorId { get; set; } + public int? GuarantorId { get; set; } } } diff --git a/LawCompany/LawCompanyContracts/ViewModels/IHearingViewModel.cs b/LawCompany/LawCompanyContracts/ViewModels/IHearingViewModel.cs index 9a7552f..684dc53 100644 --- a/LawCompany/LawCompanyContracts/ViewModels/IHearingViewModel.cs +++ b/LawCompany/LawCompanyContracts/ViewModels/IHearingViewModel.cs @@ -3,17 +3,15 @@ using System.ComponentModel; namespace LawCompanyContracts.ViewModels { - public class HearingViewModel : IHearingModel - { - [DisplayName("Номер слушания")] - public int Id { get; set; } - [DisplayName("Дата слушания")] - public DateTime HearingDate { get; set; } - [DisplayName("Суд")] - public string Judge { get; set; } = string.Empty; - [DisplayName("Имя поручителя")] - public string GuarantorName { get; set; } = string.Empty; - public int GuarantorId { get; set; } - public Dictionary HearingLawyers { get; set; } = new(); - } -} \ No newline at end of file + public class HearingViewModel : IHearingModel + { + [DisplayName("Номер слушания")] + public int Id { get; set; } + [DisplayName("Дата слушания")] + public DateTime HearingDate { get; set; } + [DisplayName("Суд")] + public string Judge { get; set; } = string.Empty; + public int GuarantorId { get; set; } + public Dictionary HearingLawyers { get; set; } = new(); + } +} diff --git a/LawCompany/LawCompanyContracts/ViewModels/ILawyerViewModel.cs b/LawCompany/LawCompanyContracts/ViewModels/ILawyerViewModel.cs index 375d861..37baf6b 100644 --- a/LawCompany/LawCompanyContracts/ViewModels/ILawyerViewModel.cs +++ b/LawCompany/LawCompanyContracts/ViewModels/ILawyerViewModel.cs @@ -13,6 +13,6 @@ namespace LawCompanyContracts.ViewModels public string Phone { get; set; } = string.Empty; [DisplayName("E-mail юриста")] public string Email { get; set; } = string.Empty; - public int GuarantorId { get; set; } + public int? GuarantorId { get; set; } } } \ No newline at end of file diff --git a/LawCompany/LawCompanyDataModels/Models/ILawyerModel.cs b/LawCompany/LawCompanyDataModels/Models/ILawyerModel.cs index 1cdf57b..4c72d32 100644 --- a/LawCompany/LawCompanyDataModels/Models/ILawyerModel.cs +++ b/LawCompany/LawCompanyDataModels/Models/ILawyerModel.cs @@ -11,6 +11,6 @@ namespace LawCompanyDataModels.Models string FIO { get; } string Phone { get; } string Email { get; } - public int GuarantorId { get; } + public int? GuarantorId { get; } } } diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/Guarantor.cs b/LawCompany/LawCompanyDatabaseImplement/Models/Guarantor.cs index dee00dd..feda3a9 100644 --- a/LawCompany/LawCompanyDatabaseImplement/Models/Guarantor.cs +++ b/LawCompany/LawCompanyDatabaseImplement/Models/Guarantor.cs @@ -1,71 +1,71 @@ -using LawCompanyContracts.BindingModels; +using LawCompanyDataModels.Models; +using LawCompanyContracts.BindingModels; using LawCompanyContracts.ViewModels; -using LawCompanyDatabaseImplement.Models; -using LawCompanyDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using LawFirmDatabaseImplement.Models; -namespace LawFirmDatabaseImplement.Models +namespace LawCompanyDatabaseImplement.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; - [ForeignKey("GuarantorId")] - public virtual List Hearings { get; set; } = new(); - [ForeignKey("GuarantorId")] - public virtual List Lawyers { get; set; } = new(); - [ForeignKey("GuarantorId")] - public virtual List Consultations { get; set; } = new(); + 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; + [ForeignKey("GuarantorId")] + public virtual List Hearings { get; set; } = new(); + [ForeignKey("GuarantorId")] + public virtual List Lawyers { get; set; } = new(); + [ForeignKey("GuarantorId")] + public virtual List Consultations { get; set; } = new(); - 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(LawCompanyDatabase context, 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 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 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 + public GuarantorViewModel GetViewModel => new() + { + Id = Id, + FIO = FIO, + Email = Email, + Password = Password, + }; + } +} diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/Lawyer.cs b/LawCompany/LawCompanyDatabaseImplement/Models/Lawyer.cs index 8a927bd..c7b638a 100644 --- a/LawCompany/LawCompanyDatabaseImplement/Models/Lawyer.cs +++ b/LawCompany/LawCompanyDatabaseImplement/Models/Lawyer.cs @@ -1,71 +1,72 @@ -using LawCompanyContracts.BindingModels; +using LawCompanyDataModels.Models; +using LawCompanyContracts.BindingModels; using LawCompanyContracts.ViewModels; -using LawCompanyDatabaseImplement.Models; -using LawCompanyDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.Design; -namespace LawFirmDatabaseImplement.Models +namespace LawCompanyDatabaseImplement.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 ConsultationLawyers { get; set; } = new(); - public int GuarantorId { get; set; } + 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 ConsultationLawyers { 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 + public static Lawyer? Create(LawCompanyDatabase context, 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; + if (!model.GuarantorId.HasValue) GuarantorId = model.GuarantorId; + } + public LawyerViewModel GetViewModel => new() + { + Id = Id, + FIO = FIO, + Email = Email, + Phone = Phone, + GuarantorId = GuarantorId + }; + } +}