2023-04-01 15:14:54 +04:00
|
|
|
|
using BankYouBankruptContracts.BindingModels;
|
|
|
|
|
using BankYouBankruptContracts.BusinessLogicsContracts;
|
|
|
|
|
using BankYouBankruptContracts.SearchModels;
|
|
|
|
|
using BankYouBankruptContracts.StoragesContracts;
|
|
|
|
|
using BankYouBankruptContracts.ViewModels;
|
2023-05-16 23:06:28 +04:00
|
|
|
|
using BankYouBankruptDataModels.Enums;
|
2023-04-01 15:14:54 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BankYouBankruptBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class MoneyTransferLogic : IMoneyTransferLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly IMoneyTransferStorage _moneyTransferStorage;
|
|
|
|
|
|
2023-05-16 23:06:28 +04:00
|
|
|
|
public readonly ICreditingStorage _creditingStorage;
|
|
|
|
|
|
|
|
|
|
public MoneyTransferLogic(ILogger<MoneyTransferLogic> logger, IMoneyTransferStorage moneyTransferStorage, ICreditingStorage creditingStorage)
|
2023-04-01 15:14:54 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_moneyTransferStorage = moneyTransferStorage;
|
2023-05-16 23:06:28 +04:00
|
|
|
|
_creditingStorage = creditingStorage;
|
2023-04-01 15:14:54 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MoneyTransferViewModel? ReadElement(MoneyTransferSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. AccountSenderId:{AccountSenderId}. AccountPayeeId:{AccountPayeeId}. Sum:{Sum}. Id:{Id}",
|
|
|
|
|
model.AccountSenderId, model.AccountPayeeId, model.Sum, model?.Id);
|
|
|
|
|
|
|
|
|
|
var element = _moneyTransferStorage.GetElement(model);
|
|
|
|
|
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
|
|
|
|
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<MoneyTransferViewModel>? ReadList(MoneyTransferSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadElement. AccountSenderId:{AccountSenderId}. AccountPayeeId:{AccountPayeeId}. Sum:{Sum}. Id:{Id}",
|
|
|
|
|
model.AccountSenderId, model.AccountPayeeId, model.Sum, model?.Id);
|
|
|
|
|
|
|
|
|
|
//list хранит весь список в случае, если model пришло со значением null на вход метода
|
|
|
|
|
var list = model == null ? _moneyTransferStorage.GetFullList() : _moneyTransferStorage.GetFilteredList(model);
|
|
|
|
|
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Create(MoneyTransferBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_moneyTransferStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 22:13:56 +04:00
|
|
|
|
//проверка на то, что это зачисление на карту, а не перевод между счетами
|
|
|
|
|
if (model.CreditingId.HasValue)
|
2023-05-16 23:06:28 +04:00
|
|
|
|
{
|
2023-05-17 22:13:56 +04:00
|
|
|
|
_creditingStorage.Update(new CreditingBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = model.CreditingId.Value,
|
|
|
|
|
DateClose = DateTime.Now,
|
|
|
|
|
Status = StatusEnum.Закрыта
|
|
|
|
|
});
|
2023-05-16 23:06:28 +04:00
|
|
|
|
|
2023-05-17 22:13:56 +04:00
|
|
|
|
}
|
2023-05-16 23:06:28 +04:00
|
|
|
|
|
2023-04-01 15:14:54 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(MoneyTransferBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_moneyTransferStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(MoneyTransferBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_moneyTransferStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка входного аргумента для методов Insert, Update и Delete
|
|
|
|
|
private void CheckModel(MoneyTransferBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//так как при удалении передаём как параметр false
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка корректности Id счёта отправителя
|
|
|
|
|
if (model.AccountSenderId < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Отсутствие Id у счёта отправителя", nameof(model.AccountSenderId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка корректности Id счёта получателя
|
|
|
|
|
if (model.AccountPayeeId < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Отсутствие Id у счёта получателя", nameof(model.AccountPayeeId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на корректную сумму перевода
|
|
|
|
|
if (model.Sum <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Сумма перевода не может раняться нулю или быть меньше его", nameof(model.Sum));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на корректную дату открытия счёта
|
|
|
|
|
if (model.DateOperation > DateTime.Now)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Дата операции не может быть ранее текущей", nameof(model.DateOperation));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. AccountSenderId:{AccountSenderId}. AccountPayeeId:{AccountPayeeId}. Sum:{Sum}. DateOperation:{DateOperation}. Id:{Id}",
|
|
|
|
|
model.AccountSenderId, model.AccountPayeeId, model.Sum, model.DateOperation, model?.Id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|