115 lines
4.2 KiB
C#
115 lines
4.2 KiB
C#
|
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 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 DocumentViewModel? ReadElement(DocumentSearchModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
_logger.LogInformation("ReadElement. DocumentName:{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 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));
|
|||
|
}
|
|||
|
if (model.Price <= 0)
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Цена Документа должна быть больше 0",
|
|||
|
nameof(model.Price));
|
|||
|
}
|
|||
|
_logger.LogInformation("Document. DocumentName:{DocumentName}.Price:{Price}. Id: {Id}",
|
|||
|
model.DocumentName, model.Price, model.Id);
|
|||
|
var element = _documentStorage.GetElement(
|
|||
|
new DocumentSearchModel
|
|||
|
{
|
|||
|
DocumentName = model.DocumentName
|
|||
|
}
|
|||
|
);
|
|||
|
if (element != null && element.Id != model.Id)
|
|||
|
{
|
|||
|
throw new InvalidOperationException("Документ с таким названием уже есть");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|