2024-04-28 22:16:14 +04:00
|
|
|
|
using BankContracts.BindingModels;
|
|
|
|
|
using BankContracts.BusinessLogicContracts;
|
|
|
|
|
using BankContracts.SearchModels;
|
|
|
|
|
using BankContracts.StoragesContracts;
|
|
|
|
|
using BankContracts.ViewModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-04-28 21:55:25 +04:00
|
|
|
|
|
|
|
|
|
namespace BankBusinessLogics.BusinessLogic
|
|
|
|
|
{
|
2024-04-28 22:16:14 +04:00
|
|
|
|
public class PurchaseLogic : IPurchaseLogic
|
2024-04-28 21:55:25 +04:00
|
|
|
|
{
|
2024-04-28 22:16:14 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IPurchaseStorage _purchaseVisitStorage;
|
|
|
|
|
|
|
|
|
|
public PurchaseLogic(ILogger<PurchaseLogic> logger, IPurchaseStorage purchaseVisitStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_purchaseVisitStorage = purchaseVisitStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CheckModel(PurchaseBindingModel model, bool checkParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (checkParams is false)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PurchaseViewModel> ReadList(PurchaseSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var results = model != null ? _purchaseVisitStorage.GetFilteredList(model) : _purchaseVisitStorage.GetFullList();
|
|
|
|
|
_logger.LogDebug("Полученные покупки: {@purchasesVisit}", results);
|
|
|
|
|
_logger.LogInformation("Извлечение списка в количестве {Count} c покупки по {@PurchaseSearchModel} модели", results.Count, model);
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Произошла ошибка при попытки получить список по {@PurchaseSearchModel} модели", model);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PurchaseViewModel ReadElement(PurchaseSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = _purchaseVisitStorage.GetElement(model);
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"Не получилось получить эдемент с id {model.Id}");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Извлечение элемента {@PurchaseViewModel} c покупки по {@PurchaseSearchModel} модели", result, model);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Произошла ошибка при попытки получить элемент по {@PurchaseSearchModel} модели", model);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Create(PurchaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
var result = _purchaseVisitStorage.Insert(model);
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"Не удалось создать покупку");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Создана сущность {@PurchaseViewModel}", result);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Произошла ошибка при попытки создать элемент по {@PurchaseBindingModel} модели", model);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(PurchaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
var result = _purchaseVisitStorage.Update(model);
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"Не удалось обновить покупку");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Была обновлена сущность на: {@PurchaseViewModel}", result);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Произошла ошибка при попытки обновить элемент по {@PurchaseBindingModel} модели", model);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(PurchaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
var result = _purchaseVisitStorage.Delete(model);
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"Ну удалось удалить покупку");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Сущность {@PurchaseViewModel} удалена ", result);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Произошла ошибка при попытки удалить элемент по {@PurchaseBindingModel} модели", model);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-28 21:55:25 +04:00
|
|
|
|
}
|
|
|
|
|
}
|