Контракты

This commit is contained in:
Халимов Рустам
2026-03-29 23:39:17 +03:00
parent 22bc964f27
commit 0209802e9e
141 changed files with 1292 additions and 725 deletions
@@ -1,21 +0,0 @@
using System;
namespace Knot.Modules.Auth.Application.Users.Auth;
public record AuthResponseDto(
string Token,
AuthUserDto User
);
public record AuthUserDto(
Guid Id,
string Username,
string DisplayName,
string? Email,
string? Bio,
string? Avatar,
DateTime? Birthday,
bool IsOnline,
DateTime CreatedAt
);
@@ -1,7 +1,6 @@
using Knot.Shared.Kernel;
using Knot.Modules.Auth.Domain;
using Knot.Modules.Auth.Application.Users.Auth;
using Knot.Modules.Auth.Contracts.Domain;
using Knot.Modules.Auth.Contracts.Application.Auth.DTOs;
namespace Knot.Modules.Auth.Application.Users.GetMe;
@@ -24,22 +23,15 @@ internal sealed class GetMeQueryHandler : IQueryHandler<GetMeQuery, AuthResponse
return Result.Failure<AuthResponseDto>(AuthErrors.UserNotFound);
}
var response = new AuthResponseDto(
string.Empty,
new AuthUserDto(
user.Id,
user.Username,
user.DisplayName,
user.Email,
user.Bio,
user.Avatar,
user.Birthday,
true,
user.CreatedAt
)
);
var response = new AuthResponseDto
{
AccessToken = string.Empty,
RefreshToken = string.Empty,
UserId = user.Id,
Username = user.Username,
DisplayName = user.DisplayName
};
return Result.Success(response);
}
}
@@ -1,18 +1,17 @@
using Knot.Modules.Auth.Domain;
using Knot.Modules.Auth.Application.Abstractions;
using Knot.Modules.Auth.Contracts.Domain;
using Knot.Modules.Auth.Contracts.Application.Abstractions;
using Knot.Modules.Auth.Contracts.Application.Auth.DTOs;
using Knot.Shared.Kernel;
using Knot.Modules.Auth.Application.Users.Auth;
using BCrypt.Net;
namespace Knot.Modules.Auth.Application.Users.Login;
/// <summary>
/// Команда для входа пользователя. Возвращает AuthResponseDto.
/// Êîìàíäà äëÿ âõîäà ïîëüçîâàòåëÿ. Âîçâðàùàåò AuthResponseDto.
/// </summary>
public sealed record LoginUserCommand(string Username, string Password) : ICommand<AuthResponseDto>;
public sealed class LoginUserCommandHandler : ICommandHandler<LoginUserCommand, AuthResponseDto>
internal sealed class LoginUserCommandHandler : ICommandHandler<LoginUserCommand, AuthResponseDto>
{
private readonly IUserRepository _userRepository;
private readonly IJwtTokenProvider _tokenProvider;
@@ -32,22 +31,15 @@ public sealed class LoginUserCommandHandler : ICommandHandler<LoginUserCommand,
return Result.Failure<AuthResponseDto>(AuthErrors.IdentityInvalidCredentials);
}
string token = _tokenProvider.Generate(user);
string token = _tokenProvider.Generate(user.Id, user.Username, user.DisplayName, user.Avatar);
return Result.Success(new AuthResponseDto(
token,
new AuthUserDto(
user.Id,
user.Username,
user.DisplayName,
user.Email,
user.Bio,
user.Avatar,
user.Birthday,
true, // IsOnline (placeholder)
user.CreatedAt
)
));
return Result.Success(new AuthResponseDto
{
AccessToken = token,
RefreshToken = string.Empty,
UserId = user.Id,
Username = user.Username,
DisplayName = user.DisplayName
});
}
}
@@ -1,10 +1,10 @@
using Knot.Modules.Auth.Contracts.Domain;
using Knot.Modules.Auth.Contracts.Application.Abstractions;
using Knot.Modules.Auth.Contracts.Application.Auth.DTOs;
using Knot.Modules.Auth.Domain;
using Knot.Modules.Auth.Application.Abstractions;
using Knot.Shared.Kernel;
using Knot.Modules.Settings.Application.Settings.Abstractions;
using Knot.Modules.Settings.Application.Settings.DTOs;
using Knot.Modules.Auth.Application.Users.Auth;
using Knot.Modules.Settings.Contracts.Application.Abstractions;
using Knot.Modules.Settings.Contracts.Application.DTOs;
using BCrypt.Net;
namespace Knot.Modules.Auth.Application.Users.Register;
@@ -22,7 +22,7 @@ public sealed record RegisterUserCommand(
/// <summary>
/// Обработчик команды регистрации.
/// </summary>
public sealed class RegisterUserCommandHandler : ICommandHandler<RegisterUserCommand, AuthResponseDto>
internal sealed class RegisterUserCommandHandler : ICommandHandler<RegisterUserCommand, AuthResponseDto>
{
private readonly IUserRepository _userRepository;
private readonly IAuthUnitOfWork _unitOfWork;
@@ -54,7 +54,7 @@ public sealed class RegisterUserCommandHandler : ICommandHandler<RegisterUserCom
return Result.Failure<AuthResponseDto>(AuthErrors.IdentityUsernameNotUnique);
}
// 2. Хеширование пароля (здесь будет вызов сервиса, пока заглушка)
// 2. Хеширование пароля
string passwordHash = BCrypt.Net.BCrypt.HashPassword(request.Password);
// 3. Создание сущности
@@ -65,27 +65,21 @@ public sealed class RegisterUserCommandHandler : ICommandHandler<RegisterUserCom
request.Email,
request.Bio);
// 4. Сохранение
_userRepository.Add(user);
// 4. Сохранение - используем метод с Domain User
var repoWithDomainUserAdd = _userRepository as Infrastructure.Persistence.UserRepository;
repoWithDomainUserAdd?.Add(user);
await _unitOfWork.SaveChangesAsync(cancellationToken);
string token = _tokenProvider.Generate(user);
string token = _tokenProvider.Generate(user.Id, user.Username, user.DisplayName, user.Avatar);
return Result.Success(new AuthResponseDto(
token,
new AuthUserDto(
user.Id,
user.Username,
user.DisplayName,
user.Email,
user.Bio,
user.Avatar,
user.Birthday,
true, // IsOnline (placeholder)
user.CreatedAt
)
));
return Result.Success(new AuthResponseDto
{
AccessToken = token,
RefreshToken = string.Empty,
UserId = user.Id,
Username = user.Username,
DisplayName = user.DisplayName
});
}
}