CourseWork_BankYouBankrupt/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/CreditingLogic.cs
2023-05-20 04:09:55 +04:00

145 lines
4.5 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 Microsoft.Extensions.Logging;
using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.BusinessLogicsContracts;
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.StoragesContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BankYouBankruptContracts.ViewModels.Client.Default;
using BankYouBankruptDataModels.Enums;
namespace BankYouBankruptBusinessLogic.BusinessLogics
{
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 bool Create(CreditingBindingModel model)
{
CheckModel(model);
if (!CheckCardPeriod(model))
{
model.Status = StatusEnum.Карта_просрочена;
}
else
{
model.Status = StatusEnum.Открыта;
}
if (_creditingStorage.Insert(model) == null)
{
_logger.LogWarning("Insert 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;
}
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);
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 Update(CreditingBindingModel model)
{
CheckModel(model);
if (_creditingStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(CreditingBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.Sum <= 0)
{
throw new ArgumentNullException("Сумма операции должна быть больше 0", nameof(model.Sum));
}
if (model.DateOpen > DateTime.Now)
{
throw new ArgumentNullException("Дата не может быть меньше текущего времени", nameof(model.DateOpen));
}
_logger.LogInformation("Crediting. Sum:{Sum}.CardId:{CardId}.Date:{date}.Id:{Id}",
model.Sum, model.CardId, model.DateOpen.ToString(), model.Id);
}
//проверка карты на просроченность
bool CheckCardPeriod(CreditingBindingModel model)
{
var card = _cardStorage.GetElement(new CardSearchModel
{
Id = model.CardId
});
//если карта просрочена
if (card.Period < DateTime.Now)
{
return false;
}
return true;
}
}
}