diff --git a/TestHosts/TestHosts/Controllers/TestBankController.cs b/TestHosts/TestHosts/Controllers/TestBankController.cs index 9124558..c531607 100644 --- a/TestHosts/TestHosts/Controllers/TestBankController.cs +++ b/TestHosts/TestHosts/Controllers/TestBankController.cs @@ -3,6 +3,7 @@ using System; using System.Linq; using System.Net.Http; + using System.Text; using System.Threading; using System.Threading.Tasks; using Database.TestBank; @@ -10,6 +11,7 @@ using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Shared.General; + using Shared.Logger; using Deposit = Database.TestBank.Deposit; [Route("api/testbank")] @@ -72,15 +74,19 @@ public async Task CreateHostConfiguration([FromBody] CreateHostCo public async Task MakeDeposit([FromBody] MakeDepositRequest makeDepositRequest, CancellationToken cancellationToken) { - // TODO: Store the deposits + 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 && h.SortCode == makeDepositRequest.ToSortCode); Guid depositId = Guid.Empty; - if (host != null) + if (host == null) { - depositId = Guid.NewGuid(); + return this.NotFound($"No host found"); + } + + depositId = Guid.NewGuid(); Deposit deposit = new Deposit { Amount = makeDepositRequest.Amount, @@ -112,7 +118,7 @@ public async Task MakeDeposit([FromBody] MakeDepositRequest makeD HttpClient client = new HttpClient(); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, host.CallbackUri); - requestMessage.Content = new StringContent(JsonConvert.SerializeObject(depositDto)); + requestMessage.Content = new StringContent(JsonConvert.SerializeObject(depositDto), Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.SendAsync(requestMessage, cancellationToken); if (response.IsSuccessStatusCode) { @@ -120,8 +126,7 @@ public async Task MakeDeposit([FromBody] MakeDepositRequest makeD await context.SaveChangesAsync(cancellationToken); } } - } - + return this.Ok(new { host.HostIdentifier, diff --git a/TestHosts/TestHosts/Startup.cs b/TestHosts/TestHosts/Startup.cs index f402b40..704b516 100644 --- a/TestHosts/TestHosts/Startup.cs +++ b/TestHosts/TestHosts/Startup.cs @@ -13,6 +13,8 @@ namespace TestHosts using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; + using Newtonsoft.Json; + using Newtonsoft.Json.Serialization; using Shared.EntityFramework; using Shared.Extensions; using Shared.General; @@ -40,7 +42,14 @@ public void ConfigureServices(IServiceCollection services) { ConfigurationReader.Initialise(Startup.Configuration); - services.AddControllers(); + services.AddControllers().AddNewtonsoftJson(options => + { + options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; + options.SerializerSettings.TypeNameHandling = TypeNameHandling.None; + options.SerializerSettings.Formatting = Formatting.Indented; + options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc; + options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); + }); // Register the Swagger generator, defining 1 or more Swagger documents services.AddSwaggerGen(c => @@ -51,14 +60,17 @@ public void ConfigureServices(IServiceCollection services) if (Startup.WebHostEnvironment.IsEnvironment("IntegrationTest") || Startup.Configuration.GetValue("ServiceOptions:UseInMemoryDatabase") == true) { services.AddDbContext(builder => builder.UseInMemoryDatabase("TestBankReadModel")); + DbContextOptionsBuilder builder = new DbContextOptionsBuilder(); + builder = builder.UseInMemoryDatabase("TestBankReadModel"); + + services.AddSingleton>(cont => (connectionString) => { return new TestBankContext(builder.Options);}); } else { var connString = ConfigurationReader.GetConnectionString("TestBankReadModel"); services.AddDbContext(builder => builder.UseSqlServer(connString)); + services.AddSingleton>(cont => (connectionString) => { return new TestBankContext(connectionString); }); } - - services.AddSingleton>(cont => (connectionString) => { return new TestBankContext(connectionString); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/TestHosts/TestHosts/TestHosts.csproj b/TestHosts/TestHosts/TestHosts.csproj index 042baf4..428eafc 100644 --- a/TestHosts/TestHosts/TestHosts.csproj +++ b/TestHosts/TestHosts/TestHosts.csproj @@ -10,11 +10,12 @@ - + - + + all runtime; build; native; contentfiles; analyzers; buildtransitive