PIbd23_Ivanova_Yakobchuk_Co.../LawCompany/LawCompanyBusinessLogic/BusinessLogics/ConsultationLogic.cs

140 lines
4.9 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 LawCompanyDataModels.Models;
using Microsoft.Extensions.Logging;
namespace LawCompanyBusinessLogic.BusinessLogics
{
public class ConsultationLogic : IConsultationLogic
{
private readonly ILogger _logger;
private readonly IConsultationStorage _consultationStorage;
public ConsultationLogic(ILogger<ConsultationLogic> logger, IConsultationStorage consultationStorage)
{
_logger = logger;
_consultationStorage = consultationStorage;
}
public bool Create(ConsultationBindingModel model)
{
CheckModel(model);
if (_consultationStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ConsultationBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_consultationStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ConsultationViewModel? ReadElement(ConsultationSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ConsultationDate:{ConsultationDate}. Id:{Id}", model.ConsultationDate, model.Id);
var element = _consultationStorage.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<ConsultationViewModel>? ReadList(ConsultationSearchModel? model)
{
_logger.LogInformation("ReadList. ConsultationDate:{ConsultationDate}.Id:{Id}", model?.ConsultationDate, model?.Id);
var list = model == null ? _consultationStorage.GetFullList() : _consultationStorage.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(ConsultationBindingModel model)
{
CheckModel(model);
if (_consultationStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool AddLawyerToConsultation(ConsultationSearchModel model, ILawyerModel lawyer)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _consultationStorage.GetElement(model);
if (element == null)
{
return false;
}
element.ConsultationLawyers[lawyer.Id] = lawyer;
_consultationStorage.Update(new()
{
Id = element.Id,
Cost = element.Cost,
ConsultationDate = element.ConsultationDate,
CaseId = element.CaseId,
ConsultationLawyers = element.ConsultationLawyers,
GuarantorId = element.GuarantorId,
});
return true;
}
private void CheckModel(ConsultationBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty((model.ConsultationDate).ToString()))
{
throw new ArgumentNullException("Не поставлено время", nameof(model.ConsultationDate));
}
_logger.LogInformation("Consultation. ConsultationDate:{ConsultationDate}. Id: {Id} ", model.ConsultationDate, model.Id);
var element = _consultationStorage.GetElement(new ConsultationSearchModel
{
ConsultationDate = model.ConsultationDate
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("На данное время уже назначена консультация");
}
}
}
}