dto refactoring

This commit is contained in:
2026-01-12 01:38:55 +01:00
Unverified
parent 56e66a2bdd
commit 6587ecfa0c
16 changed files with 95 additions and 32 deletions

View File

@@ -1,4 +1,4 @@
namespace TimetableDesigner.Backend.Services.Authentication.DTO;
namespace TimetableDesigner.Backend.Services.Authentication.DTO.API;
public class AuthenticatePasswordRequest
{

View File

@@ -1,4 +1,4 @@
namespace TimetableDesigner.Backend.Services.Authentication.DTO;
namespace TimetableDesigner.Backend.Services.Authentication.DTO.API;
public class AuthenticateResponse
{

View File

@@ -1,4 +1,4 @@
namespace TimetableDesigner.Backend.Services.Authentication.DTO;
namespace TimetableDesigner.Backend.Services.Authentication.DTO.API;
public class AuthenticateTokenRequest
{

View File

@@ -1,4 +1,4 @@
namespace TimetableDesigner.Backend.Services.Authentication.DTO;
namespace TimetableDesigner.Backend.Services.Authentication.DTO.API;
public class RegisterRequest
{

View File

@@ -1,4 +1,4 @@
namespace TimetableDesigner.Backend.Services.Authentication.DTO;
namespace TimetableDesigner.Backend.Services.Authentication.DTO.API;
public class RegisterResponse
{

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -2,7 +2,11 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimetableDesigner.Backend.Services.Authentication", "TimetableDesigner.Backend.Services.Authentication\TimetableDesigner.Backend.Services.Authentication.csproj", "{F8C0AEF3-B53F-4904-90F7-EE4A8587F023}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimetableDesigner.Backend.Services.Authentication.DTO", "TimetableDesigner.Backend.Services.Authentication.DTO\TimetableDesigner.Backend.Services.Authentication.DTO.csproj", "{384C8036-ACA7-40EB-924D-5E0271BEDB09}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TimetableDesigner.Backend.Services.Authentication.DTO", "TimetableDesigner.Backend.Services.Authentication.DTO", "{B610971F-EF8F-45D2-B869-41617ED447C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimetableDesigner.Backend.Services.Authentication.DTO.API", "TimetableDesigner.Backend.Services.Authentication.DTO\TimetableDesigner.Backend.Services.Authentication.DTO.API\TimetableDesigner.Backend.Services.Authentication.DTO.API.csproj", "{0639DAB9-E1B4-454E-A3E9-F7B712A697E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimetableDesigner.Backend.Services.Authentication.DTO.Events", "TimetableDesigner.Backend.Services.Authentication.DTO\TimetableDesigner.Backend.Services.Authentication.DTO.Events\TimetableDesigner.Backend.Services.Authentication.DTO.Events.csproj", "{3E39EDEE-621F-49BC-B777-174BB047BCA7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -14,9 +18,17 @@ Global
{F8C0AEF3-B53F-4904-90F7-EE4A8587F023}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8C0AEF3-B53F-4904-90F7-EE4A8587F023}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8C0AEF3-B53F-4904-90F7-EE4A8587F023}.Release|Any CPU.Build.0 = Release|Any CPU
{384C8036-ACA7-40EB-924D-5E0271BEDB09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{384C8036-ACA7-40EB-924D-5E0271BEDB09}.Debug|Any CPU.Build.0 = Debug|Any CPU
{384C8036-ACA7-40EB-924D-5E0271BEDB09}.Release|Any CPU.ActiveCfg = Release|Any CPU
{384C8036-ACA7-40EB-924D-5E0271BEDB09}.Release|Any CPU.Build.0 = Release|Any CPU
{0639DAB9-E1B4-454E-A3E9-F7B712A697E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0639DAB9-E1B4-454E-A3E9-F7B712A697E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0639DAB9-E1B4-454E-A3E9-F7B712A697E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0639DAB9-E1B4-454E-A3E9-F7B712A697E8}.Release|Any CPU.Build.0 = Release|Any CPU
{3E39EDEE-621F-49BC-B777-174BB047BCA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E39EDEE-621F-49BC-B777-174BB047BCA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E39EDEE-621F-49BC-B777-174BB047BCA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E39EDEE-621F-49BC-B777-174BB047BCA7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0639DAB9-E1B4-454E-A3E9-F7B712A697E8} = {B610971F-EF8F-45D2-B869-41617ED447C8}
{3E39EDEE-621F-49BC-B777-174BB047BCA7} = {B610971F-EF8F-45D2-B869-41617ED447C8}
EndGlobalSection
EndGlobal

View File

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

View File

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

View File

@@ -0,0 +1,6 @@
namespace TimetableDesigner.Backend.Services.Authentication.Application.Commands.Register;
public class RegisterDto
{
}

View File

@@ -1,7 +1,6 @@
using TimetableDesigner.Backend.Services.Authentication.Database;
using TimetableDesigner.Backend.Services.Authentication.DTO;
namespace TimetableDesigner.Backend.Services.Authentication.Application.Register;
namespace TimetableDesigner.Backend.Services.Authentication.Application.Commands.Register;
public class RegisterHandler
{
@@ -12,7 +11,7 @@ public class RegisterHandler
_databaseContext = databaseContext;
}
public async Task<RegisterResponse> HandleAsync(RegisterCommand command)
public async Task<RegisterDto> HandleAsync(RegisterCommand command)
{
return null;
}

View File

@@ -1,8 +0,0 @@
using TimetableDesigner.Backend.Services.Authentication.DTO;
namespace TimetableDesigner.Backend.Services.Authentication.Application.Register;
public static class RegisterRequestMapper
{
public static RegisterCommand ToCommand(this RegisterRequest request) => new RegisterCommand(request.Email, request.Password);
}

View File

@@ -0,0 +1,33 @@
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using TimetableDesigner.Backend.Services.Authentication.Database;
using TimetableDesigner.Backend.Services.Authentication.DTO.API;
namespace TimetableDesigner.Backend.Services.Authentication.DTO.Validators;
public class RegisterRequestValidator : AbstractValidator<RegisterRequest>
{
private readonly DatabaseContext _databaseContext;
public RegisterRequestValidator(DatabaseContext databaseContext)
{
_databaseContext = databaseContext;
RuleFor(x => x.Email)
.NotEmpty()
.EmailAddress()
.MustAsync(EmailNotUsed).WithMessage("Email already used");
RuleFor(x => x.Password)
.NotEmpty()
.MinimumLength(8)
.Must(x => x.Any(char.IsUpper)).WithMessage("Password must contain at least one uppercase letter")
.Must(x => x.Any(char.IsLower)).WithMessage("Password must contain at least one lowercase letter")
.Must(x => x.Any(char.IsDigit)).WithMessage("Password must contain at least one digit");
RuleFor(x => x.PasswordConfirmation)
.NotEmpty()
.Equal(x => x.Password);
}
private Task<bool> EmailNotUsed(string value, CancellationToken cancellationToken) =>
_databaseContext.Accounts.AnyAsync(x => x.Email == value, cancellationToken);
}

View File

@@ -1,6 +1,10 @@
using System.Reflection;
using FluentValidation;
using Microsoft.AspNetCore.Identity.Data;
using Microsoft.EntityFrameworkCore;
using TimetableDesigner.Backend.Services.Authentication.API;
using TimetableDesigner.Backend.Services.Authentication.Database;
using TimetableDesigner.Backend.Services.Authentication.DTO.Validators;
namespace TimetableDesigner.Backend.Services.Authentication;
@@ -12,6 +16,7 @@ public static class Program
.SetupOpenApi()
.SetupSecurity()
.SetupDatabase()
.SetupValidation()
.Build();
if (app.Environment.IsDevelopment())
@@ -40,6 +45,12 @@ public static class Program
builder.Services.AddDbContext<DatabaseContext>(x => x.UseNpgsql(builder.Configuration.GetConnectionString("Database")), ServiceLifetime.Transient);
return builder;
}
private static WebApplicationBuilder SetupValidation(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<IValidator<TimetableDesigner.Backend.Services.Authentication.DTO.API.RegisterRequest>, RegisterRequestValidator>();
return builder;
}
private static WebApplication InitializeDatabase(this WebApplication app)
{

View File

@@ -1,13 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<LangVersion>14</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentValidation" Version="12.1.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.9">
<PrivateAssets>all</PrivateAssets>
@@ -17,11 +19,11 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Workers\" />
<Folder Include="DTO\Mappers\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TimetableDesigner.Backend.Services.Authentication.DTO\TimetableDesigner.Backend.Services.Authentication.DTO.csproj" />
<ProjectReference Include="..\TimetableDesigner.Backend.Services.Authentication.DTO\TimetableDesigner.Backend.Services.Authentication.DTO.API\TimetableDesigner.Backend.Services.Authentication.DTO.API.csproj" />
</ItemGroup>
</Project>