178 lines
6.0 KiB
C#
Raw Normal View History

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
{
public class CashWithdrawalLogic : ICashWithdrawalLogic
{
private readonly ILogger _logger;
private readonly ICashWithdrawalStorage _cashWithdrawalStorage;
private readonly IDebitingStorage _debitingStorage;
// Конструктор
public CashWithdrawalLogic(ILogger<CashWithdrawalLogic> logger, ICashWithdrawalStorage cashWithdrawalStorage,
IDebitingStorage debitingStorage)
{
_logger = logger;
_cashWithdrawalStorage = cashWithdrawalStorage;
_debitingStorage = debitingStorage;
}
// Вывод конкретной операции
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;
}
// Вывод всего списка операций
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);
// list хранит весь список в случае, если model пришло со значением null на вход метода
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;
}
// Создание операции
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;
}
// Обновление операции
public bool Update(CashWithdrawalBindingModel model)
{
CheckModel(model);
if (_cashWithdrawalStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
// Удаление операции
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;
}
// Проверка входного аргумента для методов Insert, Update и Delete
private void CheckModel(CashWithdrawalBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
// Так как при удалении передаём как параметр false
if (!withParams)
{
return;
}
// Проверка на корректность Id счёта
if (model.AccountId < 0)
{
throw new ArgumentNullException("Некорректный Id счёта", nameof(model.AccountId));
}
// Проверка на корректность снимаемой суммы
if (model.Sum <= 0)
{
throw new ArgumentNullException("Снимаемая сумма не может раняться нулю или быть меньше его", nameof(model.Sum));
}
// Проверка на корректную дату операции
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);
}
}
}