Added all projects
We are almost working.
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
@FinancialApi_HostAddress = https://localhost:7010
|
||||
|
||||
GET {{FinancialApi_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
@@ -1,45 +0,0 @@
|
||||
using FinancialApi.ServiceDefaults;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
builder.AddServiceDefaults();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
var summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
|
||||
app.MapGet("/weatherforecast", () =>
|
||||
{
|
||||
var forecast = Enumerable.Range(1, 5).Select(index =>
|
||||
new WeatherForecast
|
||||
(
|
||||
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
Random.Shared.Next(-20, 55),
|
||||
summaries[Random.Shared.Next(summaries.Length)]
|
||||
))
|
||||
.ToArray();
|
||||
return forecast;
|
||||
})
|
||||
.WithName("GetWeatherForecast");
|
||||
|
||||
app.Run();
|
||||
|
||||
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
||||
{
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace FinancialApi.Application;
|
||||
|
||||
public record Account(int Id, decimal Balance)
|
||||
{
|
||||
public Account ApplyFee(decimal amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
throw new ArgumentException("Fee must be positive.");
|
||||
|
||||
if (Balance - amount < 0)
|
||||
throw new InvalidOperationException("Insufficient funds.");
|
||||
|
||||
// Returns a brand new record with the updated balance
|
||||
return this with { Balance = Balance - amount };
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,24 @@
|
||||
using FinancialApi.Application;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FinancialApi.Infrastructure;
|
||||
|
||||
public class AccountDbContext : DbContext
|
||||
{
|
||||
public AccountDbContext(DbContextOptions<AccountDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public DbSet<Account> Accounts => Set<Account>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<Account>()
|
||||
.HasKey(x => x.Id);
|
||||
|
||||
modelBuilder.Entity<Account>().HasData(
|
||||
new Account(1, 100.00m),
|
||||
new Account(2, 500.00m)
|
||||
);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FinancialApi.Application\FinancialApi.Application.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.9" />
|
||||
<PackageReference Include="WolverineFx" Version="6.17.3" />
|
||||
<PackageReference Include="WolverineFx.EntityFrameworkCore" Version="6.17.3" />
|
||||
<PackageReference Include="WolverineFx.Sqlite" Version="6.17.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
// <auto-generated />
|
||||
using FinancialApi.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace FinancialApi.Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(AccountDbContext))]
|
||||
[Migration("20260713152041_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "10.0.9");
|
||||
|
||||
modelBuilder.Entity("FinancialApi.Application.Account", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<decimal>("Balance")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Accounts");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
{
|
||||
Id = 1,
|
||||
Balance = 100.00m
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 2,
|
||||
Balance = 500.00m
|
||||
});
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
|
||||
|
||||
namespace FinancialApi.Infrastructure.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialCreate : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Accounts",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
Balance = table.Column<decimal>(type: "TEXT", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Accounts", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.InsertData(
|
||||
table: "Accounts",
|
||||
columns: new[] { "Id", "Balance" },
|
||||
values: new object[,]
|
||||
{
|
||||
{ 1, 100.00m },
|
||||
{ 2, 500.00m }
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Accounts");
|
||||
}
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
// <auto-generated />
|
||||
using FinancialApi.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace FinancialApi.Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(AccountDbContext))]
|
||||
partial class AccountDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "10.0.9");
|
||||
|
||||
modelBuilder.Entity("FinancialApi.Application.Account", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<decimal>("Balance")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Accounts");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
{
|
||||
Id = 1,
|
||||
Balance = 100.00m
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 2,
|
||||
Balance = 500.00m
|
||||
});
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using FinancialApi.Application;
|
||||
using FinancialApi.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FinancialApi;
|
||||
|
||||
public static class ApplyFeeHandler
|
||||
{
|
||||
public static Task<Account?> LoadAsync(ApplyFeeCommand cmd, AccountDbContext db)
|
||||
=> db.Set<Account>().AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.Id == cmd.AccountId);
|
||||
|
||||
public static (Account UpdatedAccount, FeeAppliedEvent Event) Handle(ApplyFeeCommand cmd, Account account)
|
||||
{
|
||||
var updatedAccount = account.ApplyFee(cmd.Amount);
|
||||
|
||||
var @event = new FeeAppliedEvent(account.Id, cmd.Amount);
|
||||
|
||||
return (updatedAccount, @event);
|
||||
}
|
||||
}
|
||||
|
||||
public record ApplyFeeCommand(int AccountId, decimal Amount);
|
||||
|
||||
public record FeeAppliedEvent(int AccountId, decimal Amount);
|
||||
+5
@@ -8,9 +8,14 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.9"/>
|
||||
<PackageReference Include="Microsoft.OpenApi" Version="2.10.0" />
|
||||
<PackageReference Include="WolverineFx" Version="6.17.3" />
|
||||
<PackageReference Include="WolverineFx.Http" Version="6.17.3" />
|
||||
<PackageReference Include="WolverineFx.RuntimeCompilation" Version="6.17.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FinancialApi.Infrastructure\FinancialApi.Infrastructure.csproj" />
|
||||
<ProjectReference Include="..\FinancialApi.ServiceDefaults\FinancialApi.ServiceDefaults.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
@FinancialApi_HostAddress = https://localhost:7010
|
||||
|
||||
GET {{FinancialApi_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
|
||||
POST {{FinancialApi_HostAddress}}/accounts/apply-fee
|
||||
Accept: application/json
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"accountId": 1,
|
||||
"amount": 5.0
|
||||
}
|
||||
|
||||
###
|
||||
@@ -0,0 +1,50 @@
|
||||
using FinancialApi;
|
||||
using FinancialApi.Infrastructure;
|
||||
using FinancialApi.ServiceDefaults;
|
||||
using JasperFx.CodeGeneration;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Wolverine;
|
||||
using Wolverine.EntityFrameworkCore;
|
||||
using Wolverine.Sqlite;
|
||||
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddDbContext<AccountDbContext>(options =>
|
||||
options.UseSqlite("Data Source=demo.db"));
|
||||
|
||||
// Add services to the container.
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
builder.AddServiceDefaults();
|
||||
|
||||
builder.Host.UseWolverine(opts =>
|
||||
{
|
||||
opts.PersistMessagesWithSqlite("Data Source=demo.db");
|
||||
opts.UseEntityFrameworkCoreTransactions();
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<AccountDbContext>();
|
||||
await db.Database.MigrateAsync();
|
||||
}
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.MapPost("/accounts/apply-fee", async (ApplyFeeCommand cmd, IMessageBus bus) =>
|
||||
{
|
||||
await bus.InvokeAsync(cmd);
|
||||
return Results.Accepted();
|
||||
});
|
||||
|
||||
app.Run();
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -2,7 +2,11 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FinancialApi.ServiceDefaults", "FinancialApi.ServiceDefaults\FinancialApi.ServiceDefaults.csproj", "{D9767D77-0B2D-4653-9B9C-A0E6A3850EA3}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FinacialApi", "FinacialApi\FinacialApi.csproj", "{9EFD8279-869B-485C-BA02-C983E96348A0}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FinancialApi", "FinancialApi\FinancialApi.csproj", "{9EFD8279-869B-485C-BA02-C983E96348A0}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FinancialApi.Application", "FinancialApi.Application\FinancialApi.Application.csproj", "{B7EB1ECE-9F7F-440B-8818-28A127A99915}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FinancialApi.Infrastructure", "FinancialApi.Infrastructure\FinancialApi.Infrastructure.csproj", "{6AB36AB5-05BD-41C8-9498-63EBDFAF8114}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -18,5 +22,13 @@ Global
|
||||
{9EFD8279-869B-485C-BA02-C983E96348A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9EFD8279-869B-485C-BA02-C983E96348A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9EFD8279-869B-485C-BA02-C983E96348A0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{B7EB1ECE-9F7F-440B-8818-28A127A99915}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B7EB1ECE-9F7F-440B-8818-28A127A99915}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B7EB1ECE-9F7F-440B-8818-28A127A99915}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B7EB1ECE-9F7F-440B-8818-28A127A99915}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{6AB36AB5-05BD-41C8-9498-63EBDFAF8114}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6AB36AB5-05BD-41C8-9498-63EBDFAF8114}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6AB36AB5-05BD-41C8-9498-63EBDFAF8114}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6AB36AB5-05BD-41C8-9498-63EBDFAF8114}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
Reference in New Issue
Block a user