Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create payments api #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
1 change: 1 addition & 0 deletions PaymentsApi.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
30 changes: 30 additions & 0 deletions PaymentsApi.Tests/PaymentsApi.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PaymentsApi\PaymentsApi.csproj" />
</ItemGroup>

</Project>
56 changes: 56 additions & 0 deletions PaymentsApi.Tests/PaymentsApiShould.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Net.Http.Headers;
using dotNetTesting.Payments;
using dotNetTesting.Payments.Model;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace PaymentsApi.Tests;

public class FakeCreatePaymentUseCase : ICreatePaymentUseCase
{
public Task<PaymentId> Invoke(CreatePaymentRequest request)
{
return new Task<PaymentId>(() => new PaymentId("1234"));
}
}

public class PaymentsApiShould
{
private readonly HttpClient _testClient;
public PaymentsApiShould()
{
var builder = WebApplication.CreateBuilder();
builder.AddDependencies();
builder.Services.Replace(new ServiceDescriptor(typeof(ICreatePaymentUseCase), new FakeCreatePaymentUseCase()));
builder.WebHost.UseTestServer();
var app = builder.Build();
app.UseEndpoints();
app.Start();
_testClient = app.GetTestClient();
_testClient.Timeout = TimeSpan.FromSeconds(10);
}

[Fact]
public async void Test1()
{
var request = new CreatePaymentRequest(
new MonetaryAmount(10, Currency.Eur), new EntityId("Halcon"), new EntityId("Hotel")
);
var jsonRequest = JsonSerializer.Serialize(request);
var content = new StringContent(jsonRequest);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await _testClient.PostAsync("/payments", content);
var responseContent = await response.Content.ReadAsStringAsync();
var paymentId = JsonConvert.DeserializeObject<PaymentId>(responseContent);


//response.EnsureSuccessStatusCode();
Assert.Equal(new PaymentId("1234"), paymentId);

}
}
21 changes: 21 additions & 0 deletions PaymentsApi/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using dotNetTesting.Payments;
using dotNetTesting.Payments.DataAccess;
using dotNetTesting.Services;
using Microsoft.EntityFrameworkCore;

namespace PaymentsApi;

public static class DependencyInjection
{
public static void AddDependencies(this WebApplicationBuilder builder)
{
builder.Services.AddDbContextPool<PaymentsDbContext>(options =>
{
options.UseNpgsql("Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=postgres;");
});
builder.Services.AddScoped<IEntitiesRepository, EntitiesRepository>();
builder.Services.AddScoped<IPaymentsRepository, PaymentsRepository>();
builder.Services.AddScoped<IGuidGenerator, GuidGenerator>();
builder.Services.AddScoped<ICreatePaymentUseCase, CreatePaymentUseCase>();
}
}
20 changes: 20 additions & 0 deletions PaymentsApi/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["PaymentsApi/PaymentsApi.csproj", "PaymentsApi/"]
RUN dotnet restore "PaymentsApi/PaymentsApi.csproj"
COPY . .
WORKDIR "/src/PaymentsApi"
RUN dotnet build "PaymentsApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "PaymentsApi.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "PaymentsApi.dll"]
17 changes: 17 additions & 0 deletions PaymentsApi/Endpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using dotNetTesting.Payments;

namespace PaymentsApi;

public static class Endpoints
{
public static void UseEndpoints(this WebApplication webApplication)
{
webApplication.MapPost("/payments", async (CreatePaymentRequest request, ICreatePaymentUseCase useCase) =>
{
var res = await useCase.Invoke(request);
return Results.Created($"/payments/{res.Value}", res.Value);
});

webApplication.MapGet("/", () => Results.Ok("Hello world"));
}
}
25 changes: 25 additions & 0 deletions PaymentsApi/PaymentsApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\dotNetTesting\dotNetTesting.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions PaymentsApi/PaymentsApi.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@PaymentsApi_HostAddress = http://localhost:5183

POST {{PaymentsApi_HostAddress}}/payments/
Accept: application/json

###
17 changes: 17 additions & 0 deletions PaymentsApi/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using PaymentsApi;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();


builder.AddDependencies();
var app = builder.Build();
app.UseHttpsRedirection();

app.UseEndpoints();


app.Run();
41 changes: 41 additions & 0 deletions PaymentsApi/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:46945",
"sslPort": 44388
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5183",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7019;http://localhost:5183",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions PaymentsApi/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions PaymentsApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
3 changes: 2 additions & 1 deletion dotNetTesting.Tests/dotNetTesting.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0</TargetFrameworks>

<IsPackable>false</IsPackable>

<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions dotNetTesting.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotNetTesting", "dotNetTest
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotNetTesting.Tests", "dotNetTesting.Tests\dotNetTesting.Tests.csproj", "{A36D0609-9572-44AF-95AD-01FECE04C0C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PaymentsApi", "PaymentsApi\PaymentsApi.csproj", "{4575AA41-DAE8-47F0-B90A-A6FDA0103DBB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PaymentsApi.Tests", "PaymentsApi.Tests\PaymentsApi.Tests.csproj", "{746D49C0-1EF3-4EB1-A8B8-B59F8439E2AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -18,5 +22,13 @@ Global
{A36D0609-9572-44AF-95AD-01FECE04C0C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A36D0609-9572-44AF-95AD-01FECE04C0C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A36D0609-9572-44AF-95AD-01FECE04C0C8}.Release|Any CPU.Build.0 = Release|Any CPU
{4575AA41-DAE8-47F0-B90A-A6FDA0103DBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4575AA41-DAE8-47F0-B90A-A6FDA0103DBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4575AA41-DAE8-47F0-B90A-A6FDA0103DBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4575AA41-DAE8-47F0-B90A-A6FDA0103DBB}.Release|Any CPU.Build.0 = Release|Any CPU
{746D49C0-1EF3-4EB1-A8B8-B59F8439E2AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{746D49C0-1EF3-4EB1-A8B8-B59F8439E2AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{746D49C0-1EF3-4EB1-A8B8-B59F8439E2AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{746D49C0-1EF3-4EB1-A8B8-B59F8439E2AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
11 changes: 6 additions & 5 deletions dotNetTesting.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<s:String x:Key="/Default/Environment/UnitTesting/CreateUnitTestDialog/TestProjectMapping/=C5961CBC_002DC1B6_002D41C1_002D9136_002D592B8938B0BC/@EntryIndexedValue">A36D0609-9572-44AF-95AD-01FECE04C0C8</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/CreateUnitTestDialog/TestTemplateMapping/=MSTest/@EntryIndexedValue">d6790ab7-33c2-4425-b2c9-51480cd1a852</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/CreateUnitTestDialog/TestTemplateMapping/=xUnit/@EntryIndexedValue">33ff99d8-b1ad-4ed5-a4fd-376b97c2c841</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=218e9e6b_002D00d6_002D47fd_002D816f_002D68604b4b26ff/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="CreatePaymentUseCaseTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;xUnit::A36D0609-9572-44AF-95AD-01FECE04C0C8::net7.0::dotNetTesting.Tests.Payments.DataAccess.EntitiesRepositoryTest&lt;/TestId&gt;
&lt;/TestAncestor&gt;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=af87a751_002D1e96_002D4590_002Daa7e_002D6970b60a7a01/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from PaymentsApiShould.cs" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;ProjectFile&gt;746D49C0-1EF3-4EB1-A8B8-B59F8439E2AE/f:PaymentsApiShould.cs&lt;/ProjectFile&gt;
&lt;/SessionState&gt;</s:String>


</wpf:ResourceDictionary>
6 changes: 5 additions & 1 deletion dotNetTesting/Payments/Domain/CreatePaymentUseCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace dotNetTesting.Payments;

public class CreatePaymentUseCase


public class CreatePaymentUseCase : ICreatePaymentUseCase
{
private readonly IGuidGenerator _guidGenerator;
private readonly IPaymentsRepository _paymentsRepository;
Expand Down Expand Up @@ -37,6 +39,8 @@ public async Task<PaymentId> Invoke(CreatePaymentRequest request)
}
}



public record CreatePaymentRequest(MonetaryAmount Amount, EntityId Payer, EntityId Payee);

public class PayerDoesNotExist : Exception
Expand Down
8 changes: 8 additions & 0 deletions dotNetTesting/Payments/Domain/ICreatePaymentUseCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using dotNetTesting.Payments.Model;

namespace dotNetTesting.Payments;

public interface ICreatePaymentUseCase
{
public Task<PaymentId> Invoke(CreatePaymentRequest request);
}
2 changes: 1 addition & 1 deletion dotNetTesting/dotNetTesting.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
7 changes: 7 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "8.0.0",
"rollForward": "latestMajor",
"allowPrerelease": false
}
}