2024-04-29 21:23:40 +04:00
|
|
|
|
using BankContracts.BindingModels.Cashier;
|
|
|
|
|
using BankContracts.BindingModels.Client;
|
|
|
|
|
using BankContracts.BusinessLogicsContracts.Cashier;
|
|
|
|
|
using BankContracts.SearchModels.Cashier;
|
|
|
|
|
using BankContracts.StoragesModels.Cashier;
|
|
|
|
|
using BankContracts.StoragesModels.Client;
|
|
|
|
|
using BankContracts.ViewModels.Cashier.ViewModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BankBusinessLogic.BusinessLogic.Cashier
|
|
|
|
|
{
|
2024-05-01 03:18:08 +04:00
|
|
|
|
// Класс, реализующий бизнес-логику для снятия наличных
|
|
|
|
|
public class CashWithdrawalLogic : ICashWithdrawalLogic
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly ICashWithdrawalStorage _cashWithdrawalStorage;
|
|
|
|
|
|
|
|
|
|
private readonly IDebitingStorage _debitingStorage;
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Конструктор
|
2024-05-01 03:18:08 +04:00
|
|
|
|
public CashWithdrawalLogic(ILogger<CashWithdrawalLogic> logger, ICashWithdrawalStorage cashWithdrawalStorage, IDebitingStorage debitingStorage)
|
2024-04-29 21:23:40 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_cashWithdrawalStorage = cashWithdrawalStorage;
|
|
|
|
|
_debitingStorage = debitingStorage;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Вывод конкретной операции
|
2024-04-29 21:23:40 +04:00
|
|
|
|
public CashWithdrawalViewModel? ReadElement(CashWithdrawalSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. AccountId:{AccountId}. Sum:{Sum}. DateWithdrawal:{DateWithdrawal}. Id:{Id}",
|
|
|
|
|
model.AccountId, model.Sum, model.DateWithdrawal, model?.Id);
|
|
|
|
|
|
|
|
|
|
var element = _cashWithdrawalStorage.GetElement(model);
|
|
|
|
|
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
|
|
|
|
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Вывод всего списка операций
|
2024-04-29 21:23:40 +04:00
|
|
|
|
public List<CashWithdrawalViewModel>? ReadList(CashWithdrawalSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadElement. AccountId:{AccountId}. Sum:{Sum}. DateWithdrawal:{DateWithdrawal}. Id:{Id}",
|
|
|
|
|
model?.AccountId, model?.Sum, model?.DateWithdrawal, model?.Id);
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// list хранит весь список в случае, если model пришло со значением null на вход метода
|
2024-04-29 21:23:40 +04:00
|
|
|
|
var list = model == null ? _cashWithdrawalStorage.GetFullList() : _cashWithdrawalStorage.GetFilteredList(model);
|
|
|
|
|
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Создание операции
|
2024-04-29 21:23:40 +04:00
|
|
|
|
public bool Create(CashWithdrawalBindingModel model, bool flag)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (flag)
|
|
|
|
|
{
|
|
|
|
|
if (_cashWithdrawalStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_debitingStorage.Update(new DebitingBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = model.DebitingId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_debitingStorage.Update(new DebitingBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = model.DebitingId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Обновление операции
|
2024-04-29 21:23:40 +04:00
|
|
|
|
public bool Update(CashWithdrawalBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_cashWithdrawalStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Удаление операции
|
2024-04-29 21:23:40 +04:00
|
|
|
|
public bool Delete(CashWithdrawalBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_cashWithdrawalStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Проверка входного аргумента для методов Insert, Update и Delete
|
2024-04-29 21:23:40 +04:00
|
|
|
|
private void CheckModel(CashWithdrawalBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Так как при удалении передаём как параметр false
|
2024-04-29 21:23:40 +04:00
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Проверка на корректность Id счёта
|
2024-04-29 21:23:40 +04:00
|
|
|
|
if (model.AccountId < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный Id счёта", nameof(model.AccountId));
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Проверка на корректность снимаемой суммы
|
2024-04-29 21:23:40 +04:00
|
|
|
|
if (model.Sum <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Снимаемая сумма не может раняться нулю или быть меньше его", nameof(model.Sum));
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 16:37:32 +04:00
|
|
|
|
// Проверка на корректную дату операции
|
2024-04-29 21:23:40 +04:00
|
|
|
|
if (model.DateWithdrawal > DateTime.Now)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Дата операции не может быть позднее текущей", nameof(model.DateWithdrawal));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("CashWithdrawal: AccountId:{AccountId}. Sum:{Sum}. DateWithdrawal:{DateWithdrawal}. Id:{Id}",
|
|
|
|
|
model.AccountId, model.Sum, model.DateWithdrawal, model?.Id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|