PIbd-22_Fedorenko_Puchkina_.../LawFim/LawFirmBusinessLogic/BusinessLogics/ConsultationLogic.cs
2024-04-27 19:02:25 +04:00

141 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using LawFimDataModels.Models;
using LawFirmContracts.BindingModels;
using LawFirmContracts.BusinessLogicContracts;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StoragesContracts;
using LawFirmContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace LawFirmBusinessLogic.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("На данное время уже назначена консультация");
}
}
}
}