115 lines
4.2 KiB
C#
Raw Normal View History

2024-04-13 01:58:54 +04:00
using LawFirmContracts.BindingModels;
using LawFirmContracts.BusinessLogicsContracts;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StoragesContracts;
using LawFirmContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LawFirmBusinessLogic.BusinessLogic
{
2024-05-10 23:48:48 +04:00
public class DocumentLogic : IDocumentLogic
2024-04-13 01:58:54 +04:00
{
private readonly ILogger _logger;
2024-05-10 23:48:48 +04:00
private readonly IDocumentStorage _documentStorage;
public DocumentLogic(ILogger<DocumentLogic> logger, IDocumentStorage
documentStorage)
2024-04-13 01:58:54 +04:00
{
_logger = logger;
2024-05-10 23:48:48 +04:00
_documentStorage = documentStorage;
2024-04-13 01:58:54 +04:00
}
2024-05-10 23:48:48 +04:00
public List<DocumentViewModel>? ReadList(DocumentSearchModel? model)
2024-04-13 01:58:54 +04:00
{
2024-05-10 23:48:48 +04:00
_logger.LogInformation("ReadList. DocumentName:{DocumentName}.Id:{ Id}", model?.DocumentName, model?.Id);
var list = model == null ? _documentStorage.GetFullList() : _documentStorage.GetFilteredList(model);
2024-04-13 01:58:54 +04:00
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
2024-05-10 23:48:48 +04:00
public DocumentViewModel? ReadElement(DocumentSearchModel model)
2024-04-13 01:58:54 +04:00
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2024-05-10 23:48:48 +04:00
_logger.LogInformation("ReadElement. DocumentName:{DocumentName}.Id:{ Id}", model.DocumentName, model.Id);
var element = _documentStorage.GetElement(model);
2024-04-13 01:58:54 +04:00
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
2024-05-10 23:48:48 +04:00
public bool Create(DocumentBindingModel model)
2024-04-13 01:58:54 +04:00
{
CheckModel(model);
2024-05-10 23:48:48 +04:00
if (_documentStorage.Insert(model) == null)
2024-04-13 01:58:54 +04:00
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
2024-05-10 23:48:48 +04:00
public bool Update(DocumentBindingModel model)
2024-04-13 01:58:54 +04:00
{
CheckModel(model);
2024-05-10 23:48:48 +04:00
if (_documentStorage.Update(model) == null)
2024-04-13 01:58:54 +04:00
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
2024-05-10 23:48:48 +04:00
public bool Delete(DocumentBindingModel model)
2024-04-13 01:58:54 +04:00
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
2024-05-10 23:48:48 +04:00
if (_documentStorage.Delete(model) == null)
2024-04-13 01:58:54 +04:00
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
2024-05-10 23:48:48 +04:00
private void CheckModel(DocumentBindingModel model, bool withParams = true)
2024-04-13 01:58:54 +04:00
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
2024-05-10 23:48:48 +04:00
if (string.IsNullOrEmpty(model.DocumentName))
2024-04-13 01:58:54 +04:00
{
2024-05-10 23:48:48 +04:00
throw new ArgumentNullException("Нет названия пакета документов",
nameof(model.DocumentName));
2024-04-13 01:58:54 +04:00
}
if (model.Price <= 0)
{
2024-05-10 23:48:48 +04:00
throw new ArgumentNullException("Цена пакета документов должна быть больше 0", nameof(model.Price));
2024-04-13 01:58:54 +04:00
}
2024-05-10 23:48:48 +04:00
_logger.LogInformation("Document. DocumentName:{DocumentName}.Cost:{ Cost}. Id: { Id}", model.DocumentName, model.Price, model.Id);
var element = _documentStorage.GetElement(new DocumentSearchModel
2024-04-13 01:58:54 +04:00
{
2024-05-10 23:48:48 +04:00
DocumentName = model.DocumentName
2024-04-13 01:58:54 +04:00
});
if (element != null && element.Id != model.Id)
{
2024-05-10 23:48:48 +04:00
throw new InvalidOperationException("Пакет документов с таким названием уже есть");
2024-04-13 01:58:54 +04:00
}
}
}
}