PIbd-22_Petrushin_E.A._LawFirm/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/DocumentLogic.cs

115 lines
4.2 KiB
C#
Raw Normal View History

2024-02-27 19:44:44 +04:00
using AbstractLawFirmContracts.BindingModels;
using AbstractLawFirmContracts.BusinessLogicsContracts;
using AbstractLawFirmContracts.SearchModels;
using AbstractLawFirmContracts.StoragesContracts;
using AbstractLawFirmContracts.ViewModels;
2024-02-14 00:11:21 +04:00
using Microsoft.Extensions.Logging;
2024-02-27 19:44:44 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-02-14 00:11:21 +04:00
2024-02-27 19:44:44 +04:00
namespace AbstractLawFirmBusinessLogic.BusinessLogic
2024-02-14 00:11:21 +04:00
{
public class DocumentLogic : IDocumentLogic
{
private readonly ILogger _logger;
private readonly IDocumentStorage _documentStorage;
2024-02-27 19:44:44 +04:00
public DocumentLogic(ILogger<DocumentLogic> logger, IDocumentStorage
documentStorage)
2024-02-14 00:11:21 +04:00
{
_logger = logger;
_documentStorage = documentStorage;
}
public List<DocumentViewModel>? ReadList(DocumentSearchModel? model)
{
2024-02-27 19:44:44 +04:00
_logger.LogInformation("ReadList. DocumentName:{DocumentName}.Id:{ Id}", model?.DocumentName, model?.Id);
2024-02-14 00:11:21 +04:00
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));
}
2024-02-27 19:44:44 +04:00
_logger.LogInformation("ReadElement. DocumentName:{DocumentName}.Id:{ Id}", model.DocumentName, model.Id);
2024-02-14 00:11:21 +04:00
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;
}
2024-02-27 19:44:44 +04:00
public bool Update(DocumentBindingModel model)
2024-02-14 00:11:21 +04:00
{
2024-02-27 19:44:44 +04:00
CheckModel(model);
if (_documentStorage.Update(model) == null)
2024-02-14 00:11:21 +04:00
{
2024-02-27 19:44:44 +04:00
_logger.LogWarning("Update operation failed");
2024-02-14 00:11:21 +04:00
return false;
}
return true;
}
2024-02-27 19:44:44 +04:00
public bool Delete(DocumentBindingModel model)
2024-02-14 00:11:21 +04:00
{
2024-02-27 19:44:44 +04:00
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_documentStorage.Delete(model) == null)
2024-02-14 00:11:21 +04:00
{
2024-02-27 19:44:44 +04:00
_logger.LogWarning("Delete operation failed");
2024-02-14 00:11:21 +04:00
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))
{
2024-02-27 19:44:44 +04:00
throw new ArgumentNullException("Нет названия пакета документов",
nameof(model.DocumentName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена пакета документов должна быть больше 0", nameof(model.Price));
2024-02-14 00:11:21 +04:00
}
2024-02-27 19:44:44 +04:00
_logger.LogInformation("Document. DocumentName:{DocumentName}.Cost:{ Cost}. Id: { Id}", model.DocumentName, model.Price, model.Id);
2024-02-14 00:11:21 +04:00
var element = _documentStorage.GetElement(new DocumentSearchModel
{
DocumentName = model.DocumentName
});
if (element != null && element.Id != model.Id)
{
2024-02-27 19:44:44 +04:00
throw new InvalidOperationException("Пакет документов с таким названием уже есть");
2024-02-14 00:11:21 +04:00
}
}
}
2024-02-27 19:44:44 +04:00
}