Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTTY committed Nov 18, 2023
2 parents 00dd7aa + f1e1ce4 commit 411b021
Show file tree
Hide file tree
Showing 21 changed files with 331 additions and 155 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace RobinTTY.PersonalFinanceDashboard.API.EfModels;
namespace RobinTTY.PersonalFinanceDashboard.API.EfModels;

/// <summary>
/// A tag can be used to add additional information to transactions.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace RobinTTY.PersonalFinanceDashboard.API.EfModels;
namespace RobinTTY.PersonalFinanceDashboard.API.EfModels;

/// <summary>
/// A transaction represents a monetary exchange between 2 parties.
Expand Down

This file was deleted.

This file was deleted.

25 changes: 14 additions & 11 deletions src/server/RobinTTY.PersonalFinanceDashboard.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
global using System;
global using System.Linq;
global using System.Threading;
global using System.Collections.Generic;
global using System.Threading.Tasks;
global using Serilog;
global using HotChocolate;

using HotChocolate;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -27,8 +31,12 @@
builder.Services.AddDbContextPool<ApplicationDbContext>(options =>
options.UseSqlite("Data Source=application.db"));

// TODO: automatic registration of repositories via codegen?
// General Services
builder.Services
.AddScoped<AccountRepository>()
.AddScoped<AuthenticationRequestRepository>()
.AddScoped<BankingInstitutionRepository>()
.AddScoped<TransactionRepository>()
.AddSingleton(new NordigenClientCredentials(appConfig.NordigenApi!.SecretId, appConfig.NordigenApi.SecretKey))
.AddSingleton<GoCardlessDataProvider>();
Expand All @@ -43,15 +51,11 @@
// HotChocolate GraphQL Setup
builder.Services
.AddGraphQLServer()
.AddQueryType()
.AddMutationType()
// TODO
// https://relay.dev/docs/v1.5.0/graphql-server-specification/
// 1. By convention, mutations are named as verbs (done)
// 2. their inputs are the name with "Input" appended at the end
// 3. they return an object that is the name with "Payload" appended
// .AddMutationConventions()
.AddApiTypes()
.AddTypes()
.AddMutationConventions()
.RegisterService<AccountRepository>(ServiceKind.Resolver)
.RegisterService<AuthenticationRequestRepository>(ServiceKind.Resolver)
.RegisterService<BankingInstitutionRepository>(ServiceKind.Resolver)
.RegisterService<TransactionRepository>(ServiceKind.Resolver);

var app = builder.Build();
Expand All @@ -65,6 +69,5 @@
//}

app.UseCors();
app.UseWebSockets();
app.MapGraphQL();
app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: Module("Types")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using RobinTTY.PersonalFinanceDashboard.Core.Models;
using RobinTTY.PersonalFinanceDashboard.ThirdPartyDataProviders;

namespace RobinTTY.PersonalFinanceDashboard.API.Repositories;

/// <summary>
/// Manages <see cref="Account"/> data retrieval.
/// </summary>
public class AccountRepository
{
private readonly ApplicationDbContext _dbContext;
private readonly GoCardlessDataProvider _dataProvider;

/// <summary>
/// Creates a new instance of <see cref="AccountRepository"/>.
/// </summary>
/// <param name="dbContext">The <see cref="ApplicationDbContext"/> to use for data retrieval.</param>
/// <param name="dataProvider">The data provider to use for data retrieval.</param>
public AccountRepository(ApplicationDbContext dbContext, GoCardlessDataProvider dataProvider)
{
_dbContext = dbContext;
_dataProvider = dataProvider;
}

/// <summary>
/// Gets the <see cref="BankAccount"/> matching the specified id.
/// </summary>
/// <param name="accountId">The id of the account to retrieve.</param>
/// <returns>The <see cref="BankAccount"/> if one ist matched otherwise <see langword="null"/>.</returns>
public async Task<BankAccount?> Get(string accountId)
{
var account = await _dataProvider.GetBankAccount(accountId);
return account.Result;
}

/// <summary>
/// Gets the <see cref="BankAccount"/>s matching the specified ids.
/// </summary>
/// <param name="accountIds">The ids of the accounts to retrieve.</param>
/// <returns>A list of the matched <see cref="BankAccount"/>s.</returns>
public async Task<IEnumerable<BankAccount>> Get(IEnumerable<string> accountIds)
{
var accounts = await _dataProvider.GetBankAccounts(accountIds);
return accounts.Select(query => query.Result)!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using RobinTTY.NordigenApiClient.Models.Responses;
using RobinTTY.PersonalFinanceDashboard.Core.Models;
using RobinTTY.PersonalFinanceDashboard.ThirdPartyDataProviders;

namespace RobinTTY.PersonalFinanceDashboard.API.Repositories;

/// <summary>
/// Manages <see cref="AuthenticationRequest"/> data retrieval.
/// </summary>
public class AuthenticationRequestRepository
{
private readonly ApplicationDbContext _dbContext;
private readonly GoCardlessDataProvider _dataProvider;

/// <summary>
/// Creates a new instance of <see cref="AuthenticationRequestRepository"/>.
/// </summary>
/// <param name="dbContext">The <see cref="ApplicationDbContext"/> to use for data retrieval.</param>
/// <param name="dataProvider">The data provider to use for data retrieval.</param>
public AuthenticationRequestRepository(ApplicationDbContext dbContext, GoCardlessDataProvider dataProvider)
{
_dbContext = dbContext;
_dataProvider = dataProvider;
}

/// <summary>
/// Gets the <see cref="AuthenticationRequest"/> matching the specified id.
/// </summary>
/// <param name="authenticationId">The id of the <see cref="AuthenticationRequest"/> to retrieve.</param>
/// <returns>The <see cref="AuthenticationRequest"/> if one ist matched otherwise <see langword="null"/>.</returns>
public async Task<AuthenticationRequest?> Get(string authenticationId)
{
var requests = await _dataProvider.GetAuthenticationRequest(authenticationId);
return requests.Result!;
}

/// <summary>
/// Gets all <see cref="AuthenticationRequest"/>s.
/// </summary>
/// <returns>A list of all <see cref="AuthenticationRequest"/>s.</returns>
public async Task<IEnumerable<AuthenticationRequest>> GetAll()
{
// TODO: limit
var requests = await _dataProvider.GetAuthenticationRequests(100);
return requests.Result!;
}

/// <summary>
/// Adds a new <see cref="AuthenticationRequest"/>.
/// </summary>
/// <param name="institutionId">The id of the institution to create a <see cref="AuthenticationRequest"/> for.</param>
/// <param name="redirectUri">The URI of the page to redirect to after successful authentication.</param>
/// <returns>The added <see cref="AuthenticationRequest"/>.</returns>
public async Task<AuthenticationRequest> Add(string institutionId, string redirectUri)
{
// TODO: URI validation (here or in resolver?)
var request = await _dataProvider.CreateAuthenticationRequest(institutionId, new Uri(redirectUri));
return request.Result!;
}

/// <summary>
/// Deletes an existing <see cref="AuthenticationRequest"/>.
/// </summary>
/// <param name="authenticationId">The id of the <see cref="AuthenticationRequest"/> to delete.</param>
/// <returns>TODO</returns>
public async Task<BasicResponse> Delete(string authenticationId)
{
var request = await _dataProvider.DeleteAuthenticationRequest(authenticationId);
return request.Result!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using RobinTTY.PersonalFinanceDashboard.Core.Models;
using RobinTTY.PersonalFinanceDashboard.ThirdPartyDataProviders;

namespace RobinTTY.PersonalFinanceDashboard.API.Repositories;

/// <summary>
/// Manages <see cref="BankingInstitution"/> data retrieval.
/// </summary>
public class BankingInstitutionRepository
{
private readonly ApplicationDbContext _dbContext;
private readonly GoCardlessDataProvider _dataProvider;

/// <summary>
/// Creates a new instance of <see cref="BankingInstitutionRepository"/>.
/// </summary>
/// <param name="dbContext">The <see cref="ApplicationDbContext"/> to use for data retrieval.</param>
/// <param name="dataProvider">The data provider to use for data retrieval.</param>
public BankingInstitutionRepository(ApplicationDbContext dbContext, GoCardlessDataProvider dataProvider)
{
_dbContext = dbContext;
_dataProvider = dataProvider;
}

/// <summary>
/// Gets the <see cref="BankingInstitution"/> matching the specified id.
/// </summary>
/// <param name="institutionId">The id of the <see cref="BankingInstitution"/> to retrieve.</param>
/// <returns>The <see cref="BankingInstitution"/> if one ist matched otherwise <see langword="null"/>.</returns>
public async Task<BankingInstitution?> Get(string institutionId)
{
var request = await _dataProvider.GetBankingInstitution(institutionId);
return request.Result!;
}

/// <summary>
/// Gets all <see cref="BankingInstitution"/>s.
/// </summary>
/// <returns>A list of all <see cref="BankingInstitution"/>s.</returns>
public async Task<IEnumerable<BankingInstitution>> GetAll(string? countryCode)
{
var request = await _dataProvider.GetBankingInstitutions(countryCode);
return request.Result!;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using RobinTTY.PersonalFinanceDashboard.API.EfModels;
using RobinTTY.PersonalFinanceDashboard.Core.Models;

Expand All @@ -23,6 +20,10 @@ public TransactionRepository(ApplicationDbContext dbContext)
_dbContext = dbContext;
}

/// <summary>
/// Gets all <see cref="Transaction"/>s.
/// </summary>
/// <returns>A list of all <see cref="Transaction"/>s.</returns>
public async Task<IEnumerable<Transaction>> GetAll()
{
// TODO
Expand All @@ -37,6 +38,11 @@ public async Task<IEnumerable<Transaction>> GetAll()
return transformed;
}

/// <summary>
/// Adds a new <see cref="Transaction"/>.
/// </summary>
/// <param name="transaction">The <see cref="Transaction"/> to add.</param>
/// <returns>The added <see cref="Transaction"/>.</returns>
public async Task<Transaction> Add(Transaction transaction)
{
// TODO: Automate mapping
Expand All @@ -48,13 +54,24 @@ public async Task<Transaction> Add(Transaction transaction)
return transaction;
}

/// <summary>
/// Updates an existing <see cref="Transaction"/>.
/// </summary>
/// <param name="transaction">The <see cref="Transaction"/> to update.</param>
/// <returns>The updated <see cref="Transaction"/>.</returns>
/// TODO: return Transaction not EfTransaction
public async Task<EfTransaction> Update(EfTransaction transaction)
{
_dbContext.Transactions.Update(transaction);
await _dbContext.SaveChangesAsync();
return transaction;
}

/// <summary>
/// Deletes an existing <see cref="Transaction"/>.
/// </summary>
/// <param name="transactionId">The id of the transaction to delete.</param>
/// <returns>TODO</returns>
public async Task<bool> Delete(string transactionId)
{
var result = await _dbContext.Transactions.Where(t => t.Id == transactionId).ExecuteDeleteAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RobinTTY.PersonalFinanceDashboard.API.Models;
namespace RobinTTY.PersonalFinanceDashboard.Api.Types;

/// <summary>
/// The configuration of the application server.
Expand Down
Loading

0 comments on commit 411b021

Please sign in to comment.