Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 23 additions & 30 deletions TestHosts/TestHosts/Controllers/DeveloperController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,50 @@

namespace TestHosts.Controllers
{
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Database.PataPawa;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Shared.EntityFramework;
using Shared.General;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

[Route("api/developer")]
[ApiController]
public class DeveloperController : ControllerBase
{
private readonly Func<String, PataPawaContext> ContextResolver;
private readonly IDbContextResolver<PataPawaContext> ContextResolver;

public DeveloperController(Func<String, PataPawaContext> contextResolver) {
public DeveloperController(IDbContextResolver<PataPawaContext> contextResolver) {
this.ContextResolver = contextResolver;
}

[HttpPost]
[Route("patapawaprepay/createuser")]
public async Task<IActionResult> CreatePrepayUser([FromBody] CreatePatapawaPrePayUser request, CancellationToken cancellationToken){
String connectionString = ConfigurationReader.GetConnectionString("PataPawaReadModel");
PataPawaContext context = this.ContextResolver(connectionString);


using ResolvedDbContext<PataPawaContext>? resolvedContext = this.ContextResolver.Resolve("PataPawaReadModel");

Guid userId = Guid.NewGuid();

PrePayUser user = await context.PrePayUsers.SingleOrDefaultAsync(u => u.UserName == request.UserName, cancellationToken);
PrePayUser user = await resolvedContext.Context.PrePayUsers.SingleOrDefaultAsync(u => u.UserName == request.UserName, cancellationToken);

if (user == null){

byte[] bytes = System.Text.Encoding.UTF8.GetBytes(userId.ToString());
string base64String = Convert.ToBase64String(bytes);

// Create the user
await context.PrePayUsers.AddAsync(new PrePayUser
await resolvedContext.Context.PrePayUsers.AddAsync(new PrePayUser
{
Balance = 0,
Key = base64String,
Password = request.Password,
UserId = userId,
UserName = request.UserName,
}, cancellationToken);
await context.SaveChangesAsync(cancellationToken);
await resolvedContext.Context.SaveChangesAsync(cancellationToken);
}

return this.Ok();
Expand All @@ -57,46 +57,40 @@ await context.PrePayUsers.AddAsync(new PrePayUser
[Route("patapawaprepay/adduserdebt")]
public async Task<IActionResult> AddUserDebt([FromBody] AddPatapawaPrePayUserDebt request, CancellationToken cancellationToken)
{
String connectionString = ConfigurationReader.GetConnectionString("PataPawaReadModel");
PataPawaContext context = this.ContextResolver(connectionString);


Guid userId = Guid.NewGuid();

PrePayUser user = await context.PrePayUsers.SingleOrDefaultAsync(u => u.UserName == request.UserName, cancellationToken);
using ResolvedDbContext<PataPawaContext>? resolvedContext = this.ContextResolver.Resolve("PataPawaReadModel");

PrePayUser user = await resolvedContext.Context.PrePayUsers.SingleOrDefaultAsync(u => u.UserName == request.UserName, cancellationToken);

if (user == null){
return this.NotFound();
}

user.Balance += request.DebtAmount;

await context.SaveChangesAsync(cancellationToken);
await resolvedContext.Context.SaveChangesAsync(cancellationToken);

return this.Ok();
}

[HttpPost]
[Route("patapawaprepay/createmeter")]
public async Task<IActionResult> CreatePrepayMeter([FromBody] CreatePatapawaPrePayMeter request, CancellationToken cancellationToken){
String connectionString = ConfigurationReader.GetConnectionString("PataPawaReadModel");
PataPawaContext context = this.ContextResolver(connectionString);


using ResolvedDbContext<PataPawaContext>? resolvedContext = this.ContextResolver.Resolve("PataPawaReadModel");

Guid meterId = Guid.NewGuid();

PrePayMeter meter = await context.PrePayMeters.SingleOrDefaultAsync(m => m.MeterNumber == request.MeterNumber, cancellationToken);
PrePayMeter meter = await resolvedContext.Context.PrePayMeters.SingleOrDefaultAsync(m => m.MeterNumber == request.MeterNumber, cancellationToken);

if (meter == null)
{
// Create the meter
await context.PrePayMeters.AddAsync(new PrePayMeter
await resolvedContext.Context.PrePayMeters.AddAsync(new PrePayMeter
{
MeterNumber = request.MeterNumber,
CustomerName = request.CustomerName,
MeterId = meterId
}, cancellationToken);
await context.SaveChangesAsync(cancellationToken);
await resolvedContext.Context.SaveChangesAsync(cancellationToken);
}

return this.Ok();
Expand All @@ -107,14 +101,13 @@ await context.PrePayMeters.AddAsync(new PrePayMeter
public async Task<IActionResult> CreateHostConfiguration([FromBody] CreatePataPawaPostPayBill request,
CancellationToken cancellationToken)
{
String connectionString = ConfigurationReader.GetConnectionString("PataPawaReadModel");
PataPawaContext context = this.ContextResolver(connectionString);
using ResolvedDbContext<PataPawaContext>? resolvedContext = this.ContextResolver.Resolve("PataPawaReadModel");

Guid billIdentifier = Guid.NewGuid();

// TODO: check for a duplicate bill??

await context.PostPaidBills.AddAsync(new PostPaidBill {
await resolvedContext.Context.PostPaidBills.AddAsync(new PostPaidBill {
Amount = request.Amount,
AccountNumber = request.AccountNumber,
DueDate = request.DueDate,
Expand All @@ -124,7 +117,7 @@ await context.PostPaidBills.AddAsync(new PostPaidBill {
},
cancellationToken);

await context.SaveChangesAsync(cancellationToken);
await resolvedContext.Context.SaveChangesAsync(cancellationToken);

return this.Ok(new
{
Expand Down
53 changes: 24 additions & 29 deletions TestHosts/TestHosts/Controllers/PataPawaPrePaidController.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
namespace TestHosts.Controllers{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Database.PataPawa;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Shared.EntityFramework;
using Shared.General;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

[Route("api/patapawaprepay")]
[ApiController]
public class PataPawaPrePaidController : ControllerBase{
#region Fields

private readonly Func<String, PataPawaContext> ContextResolver;
private readonly IDbContextResolver<PataPawaContext> ContextResolver;

private List<(String username, String password, String key, String balance)> users = new();
#region Fields

//private List<(String username, String password, String key, String balance)> users = new();

#endregion

#region Constructors

public PataPawaPrePaidController(Func<String, PataPawaContext> contextResolver){
public PataPawaPrePaidController(IDbContextResolver<PataPawaContext> contextResolver) {
this.ContextResolver = contextResolver;
}

Expand Down Expand Up @@ -168,21 +169,15 @@ private VendResponse CreateVendResponse(Database.PataPawa.Transaction transactio

return response;
}

private PataPawaContext GetPataPawaContext(){
String connectionString = ConfigurationReader.GetConnectionString("PataPawaReadModel");
PataPawaContext context = this.ContextResolver(connectionString);
return context;
}


private async Task<IActionResult> HandleBalanceRequest(IFormCollection requestForm, CancellationToken cancellationToken){
String username = requestForm["username"].ToString();
String key = requestForm["key"].ToString();
String meter = requestForm["meter"].ToString();

PataPawaContext context = this.GetPataPawaContext();
using ResolvedDbContext<PataPawaContext>? resolvedContext = this.ContextResolver.Resolve("PataPawaReadModel");

PrePayUser user = await context.PrePayUsers.SingleOrDefaultAsync(u => u.UserName == username && u.Key == key, cancellationToken);
PrePayUser user = await resolvedContext.Context.PrePayUsers.SingleOrDefaultAsync(u => u.UserName == username && u.Key == key, cancellationToken);

var response = new BalanceResponse{
status = 0,
Expand All @@ -201,9 +196,9 @@ private async Task<IActionResult> HandleLastVendRequest(RequestType xlatedReques
if (meterValidation.result != null)
return meterValidation.result;

PataPawaContext context = this.GetPataPawaContext();
using ResolvedDbContext<PataPawaContext>? resolvedContext = this.ContextResolver.Resolve("PataPawaReadModel");

IQueryable<Database.PataPawa.Transaction> transactions = context.Transactions.Where(t => t.MeterNumber == meter).AsQueryable();
IQueryable<Database.PataPawa.Transaction> transactions = resolvedContext.Context.Transactions.Where(t => t.MeterNumber == meter).AsQueryable();

transactions = xlatedRequestType switch{
RequestType.lastvendfull => transactions.Where(t => t.Status == 0),
Expand All @@ -228,9 +223,9 @@ private async Task<IActionResult> HandleLoginRequest(IFormCollection requestForm
String username = requestForm["username"].ToString();
String password = requestForm["password"].ToString();

PataPawaContext context = this.GetPataPawaContext();
using ResolvedDbContext<PataPawaContext>? resolvedContext = this.ContextResolver.Resolve("PataPawaReadModel");

PrePayUser user = await context.PrePayUsers.SingleOrDefaultAsync(u => u.UserName == username && u.Password == password, cancellationToken);
PrePayUser user = await resolvedContext.Context.PrePayUsers.SingleOrDefaultAsync(u => u.UserName == username && u.Password == password, cancellationToken);

if (user == default){
LoginResponse errorResponse = new LoginResponse{
Expand Down Expand Up @@ -277,16 +272,16 @@ private async Task<IActionResult> HandleVendRequest(IFormCollection requestForm,
if (meterValidation.result != null)
return meterValidation.result;

PataPawaContext context = this.GetPataPawaContext();
using ResolvedDbContext<PataPawaContext>? resolvedContext = this.ContextResolver.Resolve("PataPawaReadModel");

Database.PataPawa.Transaction transaction = this.CreateTransactionRecord(amount, meterValidation.meterDetails);

await context.Transactions.AddAsync(transaction, cancellationToken);
await resolvedContext.Context.Transactions.AddAsync(transaction, cancellationToken);
if (transaction.Charges != null){
await context.TransactionCharges.AddRangeAsync(transaction.Charges, cancellationToken);
await resolvedContext.Context.TransactionCharges.AddRangeAsync(transaction.Charges, cancellationToken);
}

await context.SaveChangesAsync(cancellationToken);
await resolvedContext.Context.SaveChangesAsync(cancellationToken);

// Now build the response object
VendResponse response = this.CreateVendResponse(transaction);
Expand Down Expand Up @@ -325,9 +320,9 @@ private RequestType TranslateRequestType(String formRequest){
}));
}

PataPawaContext context = this.GetPataPawaContext();
using ResolvedDbContext<PataPawaContext>? resolvedContext = this.ContextResolver.Resolve("PataPawaReadModel");

PrePayMeter meterDetails = await context.PrePayMeters.SingleOrDefaultAsync(m => m.MeterNumber == meterNumber, cancellationToken);
PrePayMeter meterDetails = await resolvedContext.Context.PrePayMeters.SingleOrDefaultAsync(m => m.MeterNumber == meterNumber, cancellationToken);

if (meterDetails == default){
MeterResponse errorReponse = new MeterResponse{
Expand Down
42 changes: 21 additions & 21 deletions TestHosts/TestHosts/Controllers/TestBankController.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
namespace TestHosts.Controllers
{
using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Database.TestBank;
using DataTransferObjects.TestBank;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Shared.EntityFramework;
using Shared.General;
using Shared.Logger;
using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TestHosts.Database.PataPawa;
using Deposit = Database.TestBank.Deposit;

[Route("api/testbank")]
[ApiController]
public class TestBankController : ControllerBase
{
private readonly IDbContextResolver<TestBankContext> ContextResolver;

#region Fields

private readonly Func<String, TestBankContext> ContextFactory;

#endregion

#region Constructors

public TestBankController(Func<String, TestBankContext> contextFactory)
{
this.ContextFactory = contextFactory;
public TestBankController(IDbContextResolver<TestBankContext> contextResolver) {
this.ContextResolver = contextResolver;
}

#endregion
Expand All @@ -40,12 +42,11 @@
public async Task<IActionResult> CreateHostConfiguration([FromBody] CreateHostConfigurationRequest createHostConfigurationRequest,
CancellationToken cancellationToken)
{
var connectionString = ConfigurationReader.GetConnectionString("TestBankReadModel");
var context = this.ContextFactory(connectionString);
using ResolvedDbContext<TestBankContext>? resolvedContext = this.ContextResolver.Resolve("TestBankReadModel");

Check warning on line 45 in TestHosts/TestHosts/Controllers/TestBankController.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Guid hostIdentifier = Guid.NewGuid();

var host = context.HostConfigurations.SingleOrDefault(h => h.AccountNumber == createHostConfigurationRequest.AccountNumber &&
var host = resolvedContext.Context.HostConfigurations.SingleOrDefault(h => h.AccountNumber == createHostConfigurationRequest.AccountNumber &&
h.SortCode == createHostConfigurationRequest.SortCode);

if (host != null)
Expand All @@ -60,8 +61,8 @@
HostIdentifier = hostIdentifier,
SortCode = createHostConfigurationRequest.SortCode
};
await context.HostConfigurations.AddAsync(hostConfiguration, cancellationToken);
await context.SaveChangesAsync(cancellationToken);
await resolvedContext.Context.HostConfigurations.AddAsync(hostConfiguration, cancellationToken);
await resolvedContext.Context.SaveChangesAsync(cancellationToken);

return this.Ok(new
{
Expand All @@ -76,9 +77,8 @@
{
Logger.LogInformation(JsonConvert.SerializeObject(makeDepositRequest));

String connectionString = ConfigurationReader.GetConnectionString("TestBankReadModel");
TestBankContext context = this.ContextFactory(connectionString);
HostConfiguration host = context.HostConfigurations.SingleOrDefault(h => h.AccountNumber == makeDepositRequest.ToAccountNumber &&
using ResolvedDbContext<TestBankContext>? resolvedContext = this.ContextResolver.Resolve("TestBankReadModel");

Check warning on line 80 in TestHosts/TestHosts/Controllers/TestBankController.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
HostConfiguration host = resolvedContext.Context.HostConfigurations.SingleOrDefault(h => h.AccountNumber == makeDepositRequest.ToAccountNumber &&
h.SortCode == makeDepositRequest.ToSortCode);
Guid depositId = Guid.Empty;
if (host == null)
Expand All @@ -98,8 +98,8 @@
HostIdentifier = host.HostIdentifier,
SentToHost = false
};
await context.Deposits.AddAsync(deposit, cancellationToken);
await context.SaveChangesAsync(cancellationToken);
await resolvedContext.Context.Deposits.AddAsync(deposit, cancellationToken);
await resolvedContext.Context.SaveChangesAsync(cancellationToken);

// Send to the call back Url (if specificed)
if (host.CallbackUri != null)
Expand All @@ -123,7 +123,7 @@
if (response.IsSuccessStatusCode)
{
deposit.SentToHost = true;
await context.SaveChangesAsync(cancellationToken);
await resolvedContext.Context.SaveChangesAsync(cancellationToken);
}
}

Expand Down
Loading
Loading