128 lines
4.9 KiB
C#
128 lines
4.9 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace UniversityBusinessLogics.BusinessLogic
|
|||
|
{
|
|||
|
public class PurchaseLogic : IPurchaseLogic
|
|||
|
{
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|