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

140 lines
3.9 KiB
C#
Raw Normal View History

2024-05-01 23:48:28 +04:00
using LawCompanyDataModels.Models;
using LawCompanyContracts.BindingModels;
2024-05-01 02:33:49 +04:00
using LawCompanyContracts.BusinessLogicContracts;
using LawCompanyContracts.SearchModels;
using LawCompanyContracts.StoragesContracts;
using LawCompanyContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace LawCompanyBusinessLogic.BusinessLogics
{
2024-05-01 23:48:28 +04:00
public class ConsultationLogic : IConsultationLogic
{
private readonly ILogger _logger;
private readonly IConsultationStorage _consultationStorage;
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
public ConsultationLogic(ILogger<ConsultationLogic> logger, IConsultationStorage consultationStorage)
{
_logger = logger;
_consultationStorage = consultationStorage;
}
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
public bool Create(ConsultationBindingModel model)
{
CheckModel(model);
if (_consultationStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
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;
}
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
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;
}
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
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;
}
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
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));
}
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
var element = _consultationStorage.GetElement(model);
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
if (element == null)
{
return false;
}
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
element.ConsultationLawyers[lawyer.Id] = lawyer;
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
_consultationStorage.Update(new()
{
Id = element.Id,
Cost = element.Cost,
ConsultationDate = element.ConsultationDate,
CaseId = element.CaseId,
ConsultationLawyers = element.ConsultationLawyers,
GuarantorId = element.GuarantorId,
});
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
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));
}
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
_logger.LogInformation("Consultation. ConsultationDate:{ConsultationDate}. Id: {Id} ", model.ConsultationDate, model.Id);
var element = _consultationStorage.GetElement(new ConsultationSearchModel
{
ConsultationDate = model.ConsultationDate
2024-05-01 02:33:49 +04:00
2024-05-01 23:48:28 +04:00
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("На данное время уже назначена консультация");
}
}
}
2024-05-01 02:33:49 +04:00
}