Another fix.

This commit is contained in:
Programmist73 2023-05-16 23:06:28 +04:00
parent 2b6f447621
commit 48abf8eb93
6 changed files with 58 additions and 35 deletions

View File

@ -68,35 +68,28 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics
//метод, отвечающий за изменение баланса счёта
public bool ChangeBalance(AccountSearchModel? model, int sum)
{
try
//ищем счёт
var account = ReadElement(model);
if (account == null)
{
//ищем счёт
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;
throw new ArgumentNullException("Счёт не найден", nameof(account));
}
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)

View File

@ -3,6 +3,7 @@ using BankYouBankruptContracts.BusinessLogicsContracts;
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.StoragesContracts;
using BankYouBankruptContracts.ViewModels;
using BankYouBankruptDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
@ -18,10 +19,13 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics
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;
_moneyTransferStorage = moneyTransferStorage;
_creditingStorage = creditingStorage;
}
public MoneyTransferViewModel? ReadElement(MoneyTransferSearchModel model)
@ -78,6 +82,18 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics
return false;
}
if (!model.CreditingId.HasValue)
{
throw new Exception();
}
_creditingStorage.Update(new CreditingBindingModel
{
Id = model.CreditingId.Value,
DateClose = DateTime.Now,
Status = StatusEnum.Закрыта
});
return true;
}

View File

@ -20,6 +20,6 @@ namespace BankYouBankruptContracts.BindingModels
public DateTime? DateClose { get; set; }
public StatusEnum Status { get; set; } = StatusEnum.Открыта;
public StatusEnum Status { get; set; }
}
}

View File

@ -34,6 +34,15 @@ namespace BankYouBankruptDatabaseImplement.Implements
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
.Include(x => x.Card)
.Select(x => x.GetViewModel)
@ -90,8 +99,11 @@ namespace BankYouBankruptDatabaseImplement.Implements
context.SaveChanges();
transaction.Commit();
return crediting.GetViewModel;
}
return context.Creditings
.Include(x => x.Card)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
catch
{
transaction.Rollback();

View File

@ -43,7 +43,9 @@ namespace BankYouBankruptDatabaseImplement.Models
CardId = CardId,
CardNumber = Card.Number,
Sum = Sum,
DateOpen = DateOpen
DateOpen = DateOpen,
DateClose = DateClose,
Status = Status
};
public static Crediting Create(BankYouBancruptDatabase context, CreditingBindingModel model)

View File

@ -187,11 +187,6 @@ namespace BankYouBankruptRestApi.Controllers
{
_moneyTransferLogic.Create(moneyTransfer);
_accountLogic.ChangeBalance(new AccountSearchModel
{
Id = moneyTransfer.AccountPayeeId
}, moneyTransfer.Sum);
//если нет отправителя, т. е. операция на перевод денег из нала в виртуал на карту
if (moneyTransfer.AccountSenderId.HasValue)
{
@ -200,6 +195,11 @@ namespace BankYouBankruptRestApi.Controllers
Id = moneyTransfer.AccountSenderId
}, moneyTransfer.Sum * -1);
}
_accountLogic.ChangeBalance(new AccountSearchModel
{
Id = moneyTransfer.AccountPayeeId
}, moneyTransfer.Sum);
}
catch (Exception ex)
{