Another fix.
This commit is contained in:
parent
2b6f447621
commit
48abf8eb93
@ -68,35 +68,28 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics
|
|||||||
//метод, отвечающий за изменение баланса счёта
|
//метод, отвечающий за изменение баланса счёта
|
||||||
public bool ChangeBalance(AccountSearchModel? model, int sum)
|
public bool ChangeBalance(AccountSearchModel? model, int sum)
|
||||||
{
|
{
|
||||||
try
|
//ищем счёт
|
||||||
|
var account = ReadElement(model);
|
||||||
|
|
||||||
|
if (account == null)
|
||||||
{
|
{
|
||||||
//ищем счёт
|
throw new ArgumentNullException("Счёт не найден", nameof(account));
|
||||||
var account = ReadElement(model);
|
|
||||||
|
|
||||||
if (account == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException("Счёт не найден", nameof(account));
|
|
||||||
}
|
|
||||||
|
|
||||||
//проверяем возможность операции снятия (sum может быть отрицательной)
|
|
||||||
if (sum + account.Balance < 0)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException("Операция невозможна. Недостаточно средств", nameof(account));
|
|
||||||
}
|
|
||||||
|
|
||||||
//обновляем балланс счёта
|
|
||||||
_accountStorage.Update(new AccountBindingModel
|
|
||||||
{
|
|
||||||
Id = account.Id,
|
|
||||||
Balance = account.Balance + sum
|
|
||||||
});
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
|
//проверяем возможность операции снятия (sum может быть отрицательной)
|
||||||
|
if (sum + account.Balance < 0)
|
||||||
{
|
{
|
||||||
return false;
|
throw new ArgumentNullException("Операция невозможна. Недостаточно средств", nameof(account));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//обновляем балланс счёта
|
||||||
|
_accountStorage.Update(new AccountBindingModel
|
||||||
|
{
|
||||||
|
Id = account.Id,
|
||||||
|
Balance = account.Balance + sum
|
||||||
|
});
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Create(AccountBindingModel model)
|
public bool Create(AccountBindingModel model)
|
||||||
|
@ -3,6 +3,7 @@ using BankYouBankruptContracts.BusinessLogicsContracts;
|
|||||||
using BankYouBankruptContracts.SearchModels;
|
using BankYouBankruptContracts.SearchModels;
|
||||||
using BankYouBankruptContracts.StoragesContracts;
|
using BankYouBankruptContracts.StoragesContracts;
|
||||||
using BankYouBankruptContracts.ViewModels;
|
using BankYouBankruptContracts.ViewModels;
|
||||||
|
using BankYouBankruptDataModels.Enums;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -18,10 +19,13 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics
|
|||||||
|
|
||||||
private readonly IMoneyTransferStorage _moneyTransferStorage;
|
private readonly IMoneyTransferStorage _moneyTransferStorage;
|
||||||
|
|
||||||
public MoneyTransferLogic(ILogger<MoneyTransferLogic> logger, IMoneyTransferStorage moneyTransferStorage)
|
public readonly ICreditingStorage _creditingStorage;
|
||||||
|
|
||||||
|
public MoneyTransferLogic(ILogger<MoneyTransferLogic> logger, IMoneyTransferStorage moneyTransferStorage, ICreditingStorage creditingStorage)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_moneyTransferStorage = moneyTransferStorage;
|
_moneyTransferStorage = moneyTransferStorage;
|
||||||
|
_creditingStorage = creditingStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MoneyTransferViewModel? ReadElement(MoneyTransferSearchModel model)
|
public MoneyTransferViewModel? ReadElement(MoneyTransferSearchModel model)
|
||||||
@ -78,6 +82,18 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!model.CreditingId.HasValue)
|
||||||
|
{
|
||||||
|
throw new Exception();
|
||||||
|
}
|
||||||
|
|
||||||
|
_creditingStorage.Update(new CreditingBindingModel
|
||||||
|
{
|
||||||
|
Id = model.CreditingId.Value,
|
||||||
|
DateClose = DateTime.Now,
|
||||||
|
Status = StatusEnum.Закрыта
|
||||||
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,6 @@ namespace BankYouBankruptContracts.BindingModels
|
|||||||
|
|
||||||
public DateTime? DateClose { get; set; }
|
public DateTime? DateClose { get; set; }
|
||||||
|
|
||||||
public StatusEnum Status { get; set; } = StatusEnum.Открыта;
|
public StatusEnum Status { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,15 @@ namespace BankYouBankruptDatabaseImplement.Implements
|
|||||||
|
|
||||||
using var context = new BankYouBancruptDatabase();
|
using var context = new BankYouBancruptDatabase();
|
||||||
|
|
||||||
|
if (model.Status.HasValue)
|
||||||
|
{
|
||||||
|
return context.Creditings
|
||||||
|
.Include(x => x.Card)
|
||||||
|
.Where(x => x.Status == model.Status)
|
||||||
|
.Select(x => x.GetViewModel)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
return context.Creditings
|
return context.Creditings
|
||||||
.Include(x => x.Card)
|
.Include(x => x.Card)
|
||||||
.Select(x => x.GetViewModel)
|
.Select(x => x.GetViewModel)
|
||||||
@ -90,8 +99,11 @@ namespace BankYouBankruptDatabaseImplement.Implements
|
|||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
transaction.Commit();
|
transaction.Commit();
|
||||||
|
|
||||||
return crediting.GetViewModel;
|
return context.Creditings
|
||||||
}
|
.Include(x => x.Card)
|
||||||
|
.FirstOrDefault(x => x.Id == model.Id)
|
||||||
|
?.GetViewModel;
|
||||||
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
transaction.Rollback();
|
transaction.Rollback();
|
||||||
|
@ -43,7 +43,9 @@ namespace BankYouBankruptDatabaseImplement.Models
|
|||||||
CardId = CardId,
|
CardId = CardId,
|
||||||
CardNumber = Card.Number,
|
CardNumber = Card.Number,
|
||||||
Sum = Sum,
|
Sum = Sum,
|
||||||
DateOpen = DateOpen
|
DateOpen = DateOpen,
|
||||||
|
DateClose = DateClose,
|
||||||
|
Status = Status
|
||||||
};
|
};
|
||||||
|
|
||||||
public static Crediting Create(BankYouBancruptDatabase context, CreditingBindingModel model)
|
public static Crediting Create(BankYouBancruptDatabase context, CreditingBindingModel model)
|
||||||
|
@ -187,11 +187,6 @@ namespace BankYouBankruptRestApi.Controllers
|
|||||||
{
|
{
|
||||||
_moneyTransferLogic.Create(moneyTransfer);
|
_moneyTransferLogic.Create(moneyTransfer);
|
||||||
|
|
||||||
_accountLogic.ChangeBalance(new AccountSearchModel
|
|
||||||
{
|
|
||||||
Id = moneyTransfer.AccountPayeeId
|
|
||||||
}, moneyTransfer.Sum);
|
|
||||||
|
|
||||||
//если нет отправителя, т. е. операция на перевод денег из нала в виртуал на карту
|
//если нет отправителя, т. е. операция на перевод денег из нала в виртуал на карту
|
||||||
if (moneyTransfer.AccountSenderId.HasValue)
|
if (moneyTransfer.AccountSenderId.HasValue)
|
||||||
{
|
{
|
||||||
@ -200,6 +195,11 @@ namespace BankYouBankruptRestApi.Controllers
|
|||||||
Id = moneyTransfer.AccountSenderId
|
Id = moneyTransfer.AccountSenderId
|
||||||
}, moneyTransfer.Sum * -1);
|
}, moneyTransfer.Sum * -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_accountLogic.ChangeBalance(new AccountSearchModel
|
||||||
|
{
|
||||||
|
Id = moneyTransfer.AccountPayeeId
|
||||||
|
}, moneyTransfer.Sum);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user