115 lines
4.8 KiB
C#
115 lines
4.8 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace UniversityBusinessLogics.BusinessLogic
|
|||
|
{
|
|||
|
public class PaymentLogic : IPaymentLogic
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly IPaymentStorage _paymentStorage;
|
|||
|
private readonly IOperationStorage _carStorage;
|
|||
|
public PaymentLogic(ILogger<PaymentLogic> logger, IPaymentStorage paymentStorage, IOperationStorage carStorage)
|
|||
|
{
|
|||
|
_logger = logger;
|
|||
|
_paymentStorage = paymentStorage;
|
|||
|
_carStorage = carStorage;
|
|||
|
}
|
|||
|
|
|||
|
private void CheckModel(PaymentBindingModel model, bool withParams = true)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
if (!withParams)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (model.PaidPrice < 0)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model.PaidPrice), "Оплаченная стоимость отрицательна");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public bool Create(PaymentBindingModel model)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
var result = _paymentStorage.Insert(model);
|
|||
|
if (result == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException($"Оплата не создалась");
|
|||
|
}
|
|||
|
_logger.LogInformation("Создана сущность: {@PaymentViewModel}", result);
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
_logger.LogError(e, "Ошибка при попытки создать элемент по {@PaymentBindingModel} модели", model);
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public PaymentViewModel ReadElement(PaymentSearchModel model)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var result = _paymentStorage.GetElement(model);
|
|||
|
if (result == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException($"Не получилось получить эдемент с id {model.Id}");
|
|||
|
}
|
|||
|
_logger.LogInformation("Извлечение оплаты {@PaymentViewModel} по {@PaymentSearchModel} модели", result, model);
|
|||
|
return result;
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
_logger.LogError(e, "Произошла ошибка при попытки получить элемент по {@PaymentSearchModel} модели", model);
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public List<PaymentViewModel> ReadList(PaymentSearchModel model)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var results = model != null ? _paymentStorage.GetFilteredList(model) : _paymentStorage.GetFullList();
|
|||
|
_logger.LogDebug("Список оплат {@payments}", results);
|
|||
|
_logger.LogInformation("Извлечение списка в количестве {Count} c оплатой по модели: {@PaymentSearchModel}", results.Count, model);
|
|||
|
return results;
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
_logger.LogError(e, "Произошла ошибка при попытки получить список по {@PaymentSearchModel} модели", model);
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
public bool GetPaymentInfo(PaymentSearchModel model, out double fullPrice, out double paidPrice)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var payments = ReadList(model); // Вызываем метод из бизнес логики, чтобы залогировать доп информацию
|
|||
|
if (payments == null || payments.Count == 0)
|
|||
|
{
|
|||
|
fullPrice = _carStorage.GetElement(new() { Id = model.OperationId })?.Price ?? throw new InvalidOperationException("Не получена операция для оплаты"); ;
|
|||
|
paidPrice = 0;
|
|||
|
return true;
|
|||
|
}
|
|||
|
fullPrice = payments[0].FullPrice;
|
|||
|
paidPrice = payments.Sum(x => x.PaidPrice);
|
|||
|
_logger.LogInformation("По покупке({Id}) и операцийе({Id}) получена полная стоимостиь {fullPrice} и оплаченная стоимость {paidPrice}", model.PurchaseId, model.OperationId, fullPrice, paidPrice);
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
_logger.LogError(e, "При попытке получения оплат по {@searchModel} произошла ошибка", model);
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|