MediatR added

This commit is contained in:
2026-01-13 00:18:29 +01:00
Unverified
parent 6587ecfa0c
commit 03e28c6681
5 changed files with 22 additions and 10 deletions

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http.HttpResults;
using MediatR;
using Microsoft.AspNetCore.Http.HttpResults;
using TimetableDesigner.Backend.Services.Authentication.Application.Commands.Register;
using TimetableDesigner.Backend.Services.Authentication.DTO.API;
@@ -18,10 +19,10 @@ public static class Endpoints
return app;
}
public static async Task<Results<Created<RegisterResponse>, ProblemHttpResult>> Register(RegisterCommand command, RegisterHandler handler)
public static async Task<Results<Created<RegisterResponse>, ProblemHttpResult>> Register(RegisterCommand command, IMediator mediator)
{
RegisterDto data = await handler.HandleAsync(command);
return Results.Created($"accounts/{data.Id}", data);
await mediator.Send(command);
return Results.Created($"accounts/0", null);
}
public static async Task<Results<Ok<AuthenticateResponse>, ProblemHttpResult>> AuthenticatePassword(AuthenticatePasswordRequest request)

View File

@@ -1,3 +1,5 @@
namespace TimetableDesigner.Backend.Services.Authentication.Application.Commands.Register;
using MediatR;
public record RegisterCommand(string Email, string Password);
namespace TimetableDesigner.Backend.Services.Authentication.Application.Commands.Register;
public record RegisterCommand(string Email, string Password) : IRequest;

View File

@@ -1,8 +1,9 @@
using TimetableDesigner.Backend.Services.Authentication.Database;
using MediatR;
using TimetableDesigner.Backend.Services.Authentication.Database;
namespace TimetableDesigner.Backend.Services.Authentication.Application.Commands.Register;
public class RegisterHandler
public class RegisterHandler : IRequestHandler<RegisterCommand>
{
private readonly DatabaseContext _databaseContext;
@@ -11,8 +12,8 @@ public class RegisterHandler
_databaseContext = databaseContext;
}
public async Task<RegisterDto> HandleAsync(RegisterCommand command)
public async Task Handle(RegisterCommand command, CancellationToken cancellationToken)
{
return null;
}
}

View File

@@ -17,6 +17,7 @@ public static class Program
.SetupSecurity()
.SetupDatabase()
.SetupValidation()
.SetupMediatR()
.Build();
if (app.Environment.IsDevelopment())
@@ -52,6 +53,12 @@ public static class Program
return builder;
}
private static WebApplicationBuilder SetupMediatR(this WebApplicationBuilder builder)
{
builder.Services.AddMediatR(x => x.RegisterServicesFromAssembly(typeof(Program).Assembly));
return builder;
}
private static WebApplication InitializeDatabase(this WebApplication app)
{
using (IServiceScope scope = app.Services.CreateScope())

View File

@@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="FluentValidation" Version="12.1.1" />
<PackageReference Include="MediatR" Version="14.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.9">
<PrivateAssets>all</PrivateAssets>