Часть готовых контроллеров.

This commit is contained in:
Programmist73 2023-05-14 21:23:18 +04:00
parent c7fa6e2237
commit 55147dbf29
9 changed files with 71 additions and 18 deletions

View File

@ -65,6 +65,40 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics
return list;
}
//метод, отвечающий за изменение баланса счёта
public bool ChangeBalance(AccountSearchModel? model, int sum)
{
try
{
//ищем счёт
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)
{
return false;
}
}
public bool Create(AccountBindingModel model)
{
CheckModel(model);

View File

@ -24,7 +24,7 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics
_accountStorage = accountStorage;
}
public bool Create(CardBindingModel model)
public bool Create(CardBindingModel model)
{
CheckModel(model);
if (_cardStorage.Insert(model) == null)

View File

@ -16,21 +16,28 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics
{
private readonly ILogger _logger;
private readonly ICreditingStorage _creditingStorage;
private readonly ICardLogic _cardLogic;
private readonly IAccountLogic _accountLogic;
public CreditingLogic(ILogger<CreditingLogic> logger, ICreditingStorage creditingStorage) {
public CreditingLogic(ILogger<CreditingLogic> logger, ICreditingStorage creditingStorage, ICardLogic cardLogic, IAccountLogic accountLogic) {
_logger = logger;
_creditingStorage = creditingStorage;
_cardLogic = cardLogic;
_accountLogic = accountLogic;
}
public bool Create(CreditingBindingModel model)
{
CheckModel(model);
if (_creditingStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
return true;
}
public bool Delete(CreditingBindingModel model)

View File

@ -15,6 +15,8 @@ namespace BankYouBankruptContracts.BusinessLogicsContracts
AccountViewModel? ReadElement(AccountSearchModel model);
bool ChangeBalance(AccountSearchModel? model, int sum);
bool Create(AccountBindingModel model);
bool Update(AccountBindingModel model);

View File

@ -9,16 +9,16 @@ using System.Threading.Tasks;
namespace BankYouBankruptContracts.BusinessLogicsContracts
{
public interface IClientLogic
public interface ICardLogic
{
List<ClientViewModel>? ReadList(ClientSearchModel? model);
List<CardViewModel>? ReadList(CardSearchModel? model);
ClientViewModel? ReadElement(ClientSearchModel model);
CardViewModel? ReadElement(CardSearchModel model);
bool Create(ClientBindingModel model);
bool Create(CardBindingModel model);
bool Update(ClientBindingModel model);
bool Update(CardBindingModel model);
bool Delete(ClientBindingModel model);
bool Delete(CardBindingModel model);
}
}

View File

@ -9,16 +9,16 @@ using System.Threading.Tasks;
namespace BankYouBankruptContracts.BusinessLogicsContracts
{
public interface ICardLogic
public interface IClientLogic
{
List<CardViewModel>? ReadList(CardSearchModel? model);
List<ClientViewModel>? ReadList(ClientSearchModel? model);
CardViewModel? ReadElement(CardSearchModel model);
ClientViewModel? ReadElement(ClientSearchModel model);
bool Create(CardBindingModel model);
bool Create(ClientBindingModel model);
bool Update(CardBindingModel model);
bool Update(ClientBindingModel model);
bool Delete(CardBindingModel model);
bool Delete(ClientBindingModel model);
}
}

View File

@ -112,7 +112,10 @@ namespace BankYouBankruptDatabaseImplement.Implements
context.SaveChanges();
transaction.Commit();
return account.GetViewModel;
return context.Accounts
.Include(x => x.Client)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
catch
{

View File

@ -70,7 +70,6 @@ namespace BankYouBankruptDatabaseImplement.Models
public void Update(AccountBindingModel model)
{
Balance = model.Balance;
PasswordAccount = model.PasswordAccount;
}
public AccountViewModel GetViewModel => new()

View File

@ -20,12 +20,15 @@ namespace BankYouBankruptRestApi.Controllers
private readonly ICreditingLogic _creditingLogic;
public CardController(ICardLogic cardLogic, IDebitingLogic debitingLogic, ICreditingLogic creditingLogic, ILogger<ClientController> logger)
private readonly IAccountLogic _accountLogic;
public CardController(ICardLogic cardLogic, IDebitingLogic debitingLogic, ICreditingLogic creditingLogic, IAccountLogic accountLogic, ILogger<ClientController> logger)
{
_logger = logger;
_cardLogic = cardLogic;
_debitingLogic = debitingLogic;
_creditingLogic = creditingLogic;
_accountLogic = accountLogic;
}
[HttpGet]
@ -73,6 +76,11 @@ namespace BankYouBankruptRestApi.Controllers
public void CreateCreditingOperation(CreditingBindingModel model) {
try {
_creditingLogic.Create(model);
_accountLogic.ChangeBalance(new AccountSearchModel
{
Id = _cardLogic.ReadElement(new CardSearchModel { Id = model.CardId }).AccountId
}, model.Sum);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка создания операции на пополнение");