PIbd-22_Petrushin_E.A._LawFirm/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/DocumentLogic.cs
2024-02-27 19:44:44 +04:00

115 lines
4.2 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 AbstractLawFirmContracts.BindingModels;
using AbstractLawFirmContracts.BusinessLogicsContracts;
using AbstractLawFirmContracts.SearchModels;
using AbstractLawFirmContracts.StoragesContracts;
using AbstractLawFirmContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractLawFirmBusinessLogic.BusinessLogic
{
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. 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 Create(DocumentBindingModel model)
{
CheckModel(model);
if (_documentStorage.Insert(model) == null)
{
_logger.LogWarning("Insert 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;
}
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;
}
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}.Cost:{ Cost}. 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("Пакет документов с таким названием уже есть");
}
}
}
}