ComputerHardwareStore_YouAr.../ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/PurchaseLogic.cs
2024-04-29 21:02:41 +04:00

106 lines
3.6 KiB
C#

using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.BusinessLogicsContracts;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace ComputerHardwareStoreBusinessLogic.BusinessLogic
{
public class PurchaseLogic : IPurchaseLogic
{
private readonly ILogger _logger;
private readonly IPurchaseStorage _purchaseStorage;
public PurchaseLogic(ILogger<PurchaseLogic> logger, IPurchaseStorage purchaseStorage)
{
_logger = logger;
_purchaseStorage = purchaseStorage;
}
public List<PurchaseViewModel>? ReadList(PurchaseSearchModel? model)
{
_logger.LogInformation("ReadList Purchase. Id:{ Id}", model?.Id);
var list = model == null ? _purchaseStorage.GetFullList() :
_purchaseStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList Purchase return null list");
return null;
}
_logger.LogInformation("ReadList Purchase. Count:{Count}", list.Count);
return list;
}
public PurchaseViewModel? ReadElement(PurchaseSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement Purchase. Id:{ Id}", model.Id);
var element = _purchaseStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement Purchase element not found");
return null;
}
_logger.LogInformation("ReadElement Purchase find. Id:{Id}", element.Id);
return element;
}
public bool Create(PurchaseBindingModel model)
{
CheckModel(model);
if (_purchaseStorage.Insert(model) == null)
{
_logger.LogWarning("Insert Purchase operation failed");
return false;
}
return true;
}
public bool Update(PurchaseBindingModel model)
{
CheckModel(model);
if (_purchaseStorage.Update(model) == null)
{
_logger.LogWarning("Update Purchase operation failed");
return false;
}
return true;
}
public bool Delete(PurchaseBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_purchaseStorage.Delete(model) == null)
{
_logger.LogWarning("Delete Purchase operation failed");
return false;
}
return true;
}
private void CheckModel(PurchaseBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.DateCreate == null) // TODO чего блин, всмысле нет
{
throw new ArgumentNullException("Нет даты создания покупки", nameof(model.Date));
}
if (model.Cost <= 0)
{
throw new ArgumentNullException("Стоимость покупки должна быть больше 0", nameof(model.Cost));
}
}
}
}