Skip to content

Commit

Permalink
Implement remaining GraphQL operations for bank accounts. Resolves #67
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTTY committed Oct 18, 2024
1 parent faab317 commit 7606718
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using HotChocolate.Types;
using RobinTTY.PersonalFinanceDashboard.Core.Models;
using RobinTTY.PersonalFinanceDashboard.Infrastructure.Repositories;

namespace RobinTTY.PersonalFinanceDashboard.Api.Resolvers.Mutations;

/// <summary>
/// <see cref="BankAccount"/> related mutation resolvers.
/// </summary>
[MutationType]
public class BankAccountMutations
{
/// <summary>
/// Create a new banking account.
/// </summary>
/// <param name="repository">The injected repository to use for data retrieval.</param>
/// <param name="bankAccount">The banking account to create.</param>
public async Task<BankAccount> CreateBankAccount(BankAccountRepository repository, BankAccount bankAccount)
{
return await repository.AddBankAccount(bankAccount);
}

/// <summary>
/// Update an existing banking account.
/// </summary>
/// <param name="repository">The injected repository to use for data retrieval.</param>
/// <param name="bankAccount">The banking account to update.</param>
/// <returns></returns>
public async Task<BankAccount> UpdateBankAccount(BankAccountRepository repository, BankAccount bankAccount)
{
return await repository.UpdateBankAccount(bankAccount);
}

/// <summary>
/// Delete an existing bank account.
/// </summary>
/// <param name="repository">The injected repository to use for data retrieval.</param>
/// <param name="bankAccountId">The id of the bank account to delete.</param>
public async Task<bool> DeleteBankAccount(BankAccountRepository repository,
Guid bankAccountId)
{
return await repository.DeleteBankAccount(bankAccountId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ public sealed class BankAccountQueryResolvers
/// </summary>
/// <param name="repository">The repository to use for data retrieval.</param>
/// <param name="accountId">The id of the account to lookup.</param>
public async Task<BankAccount?> GetAccount(BankAccountRepository repository, Guid accountId)
public async Task<BankAccount?> GetBankAccount(BankAccountRepository repository, Guid accountId)
{
return await repository.GetBankAccount(accountId);
}

// TODO: there is no input here like the comment indicates
/// <summary>
/// Look up accounts by a list of ids.
/// </summary>
/// <param name="repository">The injected repository to use for data retrieval.</param>
[UsePaging]
public async Task<IEnumerable<BankAccount>> GetAccounts(BankAccountRepository repository)
public async Task<IEnumerable<BankAccount>> GetBankAccounts(BankAccountRepository repository)
{
return await repository.GetBankAccounts();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class BankAccount : Account
/// <summary>
/// The banking institution this account belongs to.
/// </summary>
public BankingInstitution AssociatedInstitution { get; set; }
public BankingInstitution? AssociatedInstitution { get; set; }

/// <summary>
/// Creates a new instance of <see cref="BankAccount"/>.
Expand All @@ -49,7 +49,7 @@ public class BankAccount : Account
/// <param name="accountType">Specifies the nature, or use, of the account.</param>
/// <param name="associatedInstitution">The banking institution this account belongs to.</param>
public BankAccount(Guid id, string? name, string? description, decimal? balance, string? currency, List<Transaction> transactions,
string? iban, string? bic, string? bban, string? ownerName, string? accountType, BankingInstitution associatedInstitution)
string? iban, string? bic, string? bban, string? ownerName, string? accountType, BankingInstitution? associatedInstitution)
: base(id, name, description, balance, currency, transactions)
{
Iban = iban;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,42 @@ public async Task<List<BankAccount>> GetBankAccounts()
var accountEntities = await _dbContext.BankAccounts.ToListAsync();
return accountEntities.Select(_bankAccountMapper.EntityToModel).ToList();
}

/// <summary>
/// Adds a new <see cref="BankAccount"/>.
/// </summary>
/// <param name="bankAccount">The <see cref="BankAccount"/>s to add.</param>
public async Task<BankAccount> AddBankAccount(BankAccount bankAccount)
{
var bankAccountEntity = _bankAccountMapper.ModelToEntity(bankAccount);
var entityEntry = _dbContext.BankAccounts.Add(bankAccountEntity);
await _dbContext.SaveChangesAsync();

return _bankAccountMapper.EntityToModel(entityEntry.Entity);
}

/// <summary>
/// Updates an existing <see cref="BankAccount"/>.
/// </summary>
/// <param name="bankAccount">The bank account to update.</param>
/// <returns>The updated <see cref="BankAccount"/>.</returns>
public async Task<BankAccount> UpdateBankAccount(BankAccount bankAccount)
{
var accountEntity = _bankAccountMapper.ModelToEntity(bankAccount);
var entityEntry = _dbContext.BankAccounts.Update(accountEntity);
await _dbContext.SaveChangesAsync();

return _bankAccountMapper.EntityToModel(entityEntry.Entity);
}

/// <summary>
/// Deletes an existing <see cref="BankAccount"/>.
/// </summary>
/// <param name="bankAccountId">The id of the <see cref="BankAccount"/> to delete.</param>
/// <returns>Boolean value indicating whether the operation was successful.</returns>
public async Task<bool> DeleteBankAccount(Guid bankAccountId)
{
var result = await _dbContext.BankAccounts.Where(t => t.Id == bankAccountId).ExecuteDeleteAsync();
return Convert.ToBoolean(result);
}
}

0 comments on commit 7606718

Please sign in to comment.