CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/PayLogic.cs

81 lines
2.9 KiB
C#
Raw Normal View History

2024-05-19 18:43:00 +04:00
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace ElectronicsShopBusinessLogic.BusinessLogic
{
2024-05-22 23:00:35 +04:00
public class PayLogic : IPaymentLogic
2024-05-19 18:43:00 +04:00
{
private readonly ILogger _logger;
2024-05-22 23:00:35 +04:00
private readonly IPaymentStorage _storage;
2024-05-19 18:43:00 +04:00
2024-05-22 23:00:35 +04:00
public bool CreatePay(PaymentBindingModel model)
2024-05-19 18:43:00 +04:00
{
CheckModel(model);
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
2024-05-19 18:43:00 +04:00
}
2024-05-22 23:00:35 +04:00
public PaymentViewModel? ReadElement(PaymentSearchModel model)
2024-05-19 18:43:00 +04:00
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation($"ReadElement.ProductID:{model.ProductID}.ID:{model.ID}");
var element = _storage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement. element not fount");
return null;
}
_logger.LogInformation($"ReadElement.find.ID:{element.ID}");
return element;
2024-05-19 18:43:00 +04:00
}
2024-05-22 23:00:35 +04:00
public List<PaymentViewModel>? ReadList(PaymentSearchModel? model)
2024-05-19 18:43:00 +04:00
{
_logger.LogInformation($"ReadList. ClientID:{model?.ID}");
/*var list = model == null ? _storage.GetFullList() : _storage.GetFillteredList(model); ;
if (list == null)
{
_logger.LogWarning("ReadList. return null list");
return null;
}
_logger.LogInformation($"ReadList.Count:{list.Count}");*/
return null;
}
private void CheckModel(PaymentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.SumPayment <= 0)
{
throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.SumPayment));
}
_logger.LogInformation($"Payment. ID:{model.ID}.ProductID:{model.ProductID}.Sum:{model.SumPayment}.OrderID:{model.OrderID}.PayOption{model.PayOption}");
var element = _storage.GetElement(new PaymentSearchModel { ID = model.ID });
if (element != null && element.PayOption != model.PayOption)
{
throw new InvalidOperationException("Продукт с таким названием уже есть");
}
2024-05-19 18:43:00 +04:00
}
}
}