2024-05-01 03:18:08 +04:00

156 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BankContracts.BindingModels.Client;
using BankContracts.BusinessLogicsContracts.Client;
using BankContracts.SearchModels.Client;
using BankContracts.StoragesModels.Client;
using BankContracts.ViewModels.Client.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankBusinessLogic.BusinessLogic.Client
{
// Класс, реализующий бизнес-логику для пополнения карты
public class CreditingLogic : ICreditingLogic
{
private readonly ILogger _logger;
private readonly ICreditingStorage _creditingStorage;
private readonly ICardStorage _cardStorage;
// Конструктор
public CreditingLogic(ILogger<CreditingLogic> logger, ICreditingStorage creditingStorage, ICardStorage cardStorage)
{
_logger = logger;
_creditingStorage = creditingStorage;
_cardStorage = cardStorage;
}
// Вывод конкретной операции на пополнение
public CreditingViewModel? ReadElement(CreditingSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. CreditingId:{ Id }", model.Id);
var element = _creditingStorage.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<CreditingViewModel>? ReadList(CreditingSearchModel? model)
{
_logger.LogInformation("ReadList. CreditingId:{Id}", model?.Id);
// list хранит весь список в случае, если model пришло со значением null на вход метода
var list = model == null ? _creditingStorage.GetFullList() : _creditingStorage.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(CreditingBindingModel model)
{
CheckModel(model);
if (_creditingStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
// Обновление операции на пополнение
public bool Update(CreditingBindingModel model)
{
CheckModel(model);
if (_creditingStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
// Удаление операции на пополнение
public bool Delete(CreditingBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_creditingStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
// Проверка входного аргумента для методов Insert, Update и Delete
private void CheckModel(CreditingBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
// Так как при удалении передаём как параметр false
if (!withParams)
{
return;
}
// Проверка на корректность Id клиента банковской карты
if (model.ClientId < 0)
{
throw new ArgumentNullException("Некорректный Id клиента", nameof(model.ClientId));
}
// Проверка на корректность суммы пополнения
if (model.Sum <= 0)
{
throw new ArgumentNullException("Сумма операции должна быть больше 0", nameof(model.Sum));
}
// Проверка на корректную дату операции
if (model.DateCredit > DateTime.Now)
{
throw new ArgumentNullException("Дата не может быть меньше текущего времени", nameof(model.DateCredit));
}
_logger.LogInformation("Crediting. Sum:{Sum}.CardId:{CardId}.Date:{date}.Id:{Id}",
model.Sum, model.CardId, model.DateCredit.ToString(), model.Id);
}
}
}