PIbd-22_Petrushin_E.A._LawFirm/LawFirm/LawFirmBusinessLogic/BusinessLogics/DocumentLogic.cs

107 lines
3.8 KiB
C#
Raw Normal View History

2024-02-14 00:11:21 +04:00
using LawFirmContracts.BindingModels;
using LawFirmContracts.BusinessLogicsContracts;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StoragesContracts;
using LawFirmContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace LawFirmBusinessLogic.BusinessLogics
{
public class DocumentLogic : IDocumentLogic
{
private readonly ILogger _logger;
private readonly IDocumentStorage _documentStorage;
public DocumentLogic(ILogger<DocumentLogic> logger, IDocumentStorage documentStorage)
{
_logger = logger;
_documentStorage = documentStorage;
}
public List<DocumentViewModel>? ReadList(DocumentSearchModel? model)
{
_logger.LogInformation("ReadList. DocumentName:{DocumentName}. Id:{Id}", model?.DocumentName, model?.Id);
var list = model == null ? _documentStorage.GetFullList() : _documentStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public DocumentViewModel? ReadElement(DocumentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ComponentName:{DocumentName}. Id:{Id}", model.DocumentName, model.Id);
var element = _documentStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(DocumentBindingModel model)
{
CheckModel(model);
if (_documentStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(DocumentBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_documentStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool Update(DocumentBindingModel model)
{
CheckModel(model);
if (_documentStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(DocumentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.DocumentName))
{
throw new ArgumentNullException("Нет названия компонента", nameof(model.DocumentName));
}
_logger.LogInformation("Component. ComponentName:{ComponentName}. Id:{Id}", model.DocumentName, model.Id);
var element = _documentStorage.GetElement(new DocumentSearchModel
{
DocumentName = model.DocumentName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}
}