CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/PaymeantLogic.cs

97 lines
3.2 KiB
C#

using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums;
using Microsoft.Extensions.Logging;
namespace ElectronicsShopBusinessLogic.BusinessLogic
{
public class PaymeantLogic : IPaymeantLogic
{
private readonly ILogger _logger;
private readonly IPaymeantStorage _storage;
public PaymeantLogic(ILogger<PaymeantLogic> logger, IPaymeantStorage storage) {
_logger = logger;
_storage = storage;
}
public bool CreatePay(PaymeantBindingModel model)
{
CheckModel(model);
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public PaymeantViewModel? ReadElement(PaymeantSearchModel 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<PaymeantViewModel>? ReadList(PaymeantSearchModel? 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 list;
}
private void CheckModel(PaymeantBindingModel model, bool withParams = true)
{
if (!withParams)
{
return;
}
if (model.SumPayment <= 0)
{
throw new ArgumentNullException("Сумма оплаты должна быть больше 0", nameof(model.SumPayment));
}
_logger.LogInformation($"Payment. ID:{model.ID}.Sum:{model.SumPayment}.OrderID:{model.OrderID}" +
$".PayOption{model.PayOption}");
}
public bool SetFullPayment(PaymeantBindingModel model) {
throw new NotImplementedException();
}
public bool SetPartialPayemnt(PaymeantBindingModel model) {
throw new NotImplementedException();
}
public bool SetStatus(PaymeantBindingModel model, PaymeantOption paymeantOption) {
CheckModel(model, false);
model.PayOption = paymeantOption;
if (_storage.UpdatePay == null) {
_logger.LogWarning("update operation failed");
return false;
}
return true;
}
}
}