CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/PayLogic.cs

81 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace ElectronicsShopBusinessLogic.BusinessLogic
{
public class PayLogic : IPaymentLogic
{
private readonly ILogger _logger;
private readonly IPaymentStorage _storage;
public bool CreatePay(PaymentBindingModel model)
{
CheckModel(model);
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public PaymentViewModel? ReadElement(PaymentSearchModel model)
{
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;
}
public List<PaymentViewModel>? ReadList(PaymentSearchModel? model)
{
_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("Продукт с таким названием уже есть");
}
}
}
}