PIbd23_Ivanova_Yakobchuk_Co.../LawCompany/LawCompanyBusinessLogic/BusinessLogics/LawyerLogic.cs

153 lines
5.2 KiB
C#
Raw Normal View History

2024-05-01 02:33:49 +04:00
using LawCompanyContracts.BindingModels;
using LawCompanyContracts.BusinessLogicContracts;
using LawCompanyContracts.SearchModels;
using LawCompanyContracts.StoragesContracts;
using LawCompanyContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace LawCompanyBusinessLogic.BusinessLogics
{
public class LawyerLogic : ILawyerLogic
{
private readonly ILogger _logger;
private readonly ILawyerStorage _lawyerStorage;
public LawyerLogic(ILogger<LawyerLogic> logger, ILawyerStorage lawyerStorage)
{
_logger = logger;
_lawyerStorage = lawyerStorage;
}
public bool Create(LawyerBindingModel model)
{
CheckModel(model);
if (_lawyerStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(LawyerBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_lawyerStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public List<LawyerViewModel>? ReadHearingElementList(HearingSearchModel? model)
{
if (model == null)
{
return null;
}
var list = _lawyerStorage.GetLawyerHearingList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public List<LawyerViewModel>? ReadConsultationElementList(ConsultationSearchModel? model)
{
if (model == null)
{
return null;
}
var list = _lawyerStorage.GetLawyerConsultationList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public LawyerViewModel? ReadElement(LawyerSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. LawyerName:{FIO}. Id:{Id}", model.FIO, model.Id);
var element = _lawyerStorage.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<LawyerViewModel>? ReadList(LawyerSearchModel? model)
{
_logger.LogInformation("ReadList. LawyerName:{LawyerName}.Id:{Id}", model?.FIO, model?.Id);
var list = model == null ? _lawyerStorage.GetFullList() : _lawyerStorage.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(LawyerBindingModel model)
{
CheckModel(model);
if (_lawyerStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(LawyerBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.FIO))
{
throw new ArgumentNullException("Нет имени юриста", nameof(model.FIO));
}
if (string.IsNullOrEmpty(model.Phone))
{
throw new ArgumentNullException("Нет телефона юриста", nameof(model.Phone));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет e-mail юриста", nameof(model.Email));
}
_logger.LogInformation("Lawyer. LawyerName:{FIO}. Phone: {Phone}. E-mail:{Email}. Id: {Id} ", model.FIO, model.Phone, model.Email, model.Id);
var element = _lawyerStorage.GetElement(new LawyerSearchModel
{
FIO = model.FIO,
Phone = model.Phone,
Email = model.Email,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Юрист с такими данными уже есть");
}
}
}
}