Add username validation and initial friends feature

Introduced a UsernameValidator with Cyrillic and length checks, integrated into AuthService and registration flow. Added InvalidUsernameException and related interface. Updated User model to support friend requests, added FriendsController (stub), and configured Friendship entity in EF Core. Adjusted UserValidator max name length and removed navigation properties from PrivateChat.
This commit is contained in:
Artemy
2025-06-27 16:36:56 +07:00
parent 9b9cd73a96
commit ec44347bbe
14 changed files with 201 additions and 6 deletions
@@ -0,0 +1,8 @@
using Govor.Core;
namespace Govor.Application.Exceptions.AuthService;
public class InvalidUsernameException(string message) : GovorCoreException(message)
{
}
@@ -0,0 +1,8 @@
using Govor.Core.Infrastructure.Validators;
namespace Govor.Application.Interfaces.Authentication;
public interface IUsernameValidator : IObjectValidator<string>
{
}
+8 -1
View File
@@ -1,9 +1,11 @@
using System.Text.RegularExpressions;
using Govor.API.Services.Authentication.Interfaces;
using Govor.Application.Exceptions.AuthService;
using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Models;
using Govor.Core.Repositories.Users;
using Govor.Application.Interfaces.Authentication;
using Govor.Core.Infrastructure.Validators;
using Govor.Core.Repositories.Admins;
namespace Govor.Application.Services;
@@ -14,21 +16,26 @@ public class AuthService : IAccountService
private readonly IJwtService _jwtService;
private readonly IUsersRepository _usersRepository;
private readonly IAdminsRepository _adminsRepository;
private readonly IUsernameValidator _usernameValidator;
public AuthService(IUsersRepository usersRepository,
IJwtService jwtService,
IPasswordHasher passwordHasher,
IAdminsRepository adminsRepository
IAdminsRepository adminsRepository,
IUsernameValidator usernameValidator
)
{
_usersRepository = usersRepository;
_jwtService = jwtService;
_passwordHasher = passwordHasher;
_adminsRepository = adminsRepository;
_usernameValidator = usernameValidator;
}
public async Task<string> RegistrationAsync(string name, string password, Invitation invitation)
{
_usernameValidator.Validate(name);
if (await _usersRepository.ExistsUsernameAsync(name))
throw new UserAlreadyExistException(name);
@@ -0,0 +1,35 @@
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using Govor.Application.Exceptions.AuthService;
using Govor.Application.Interfaces.Authentication;
using Govor.Core.Infrastructure.Validators;
namespace Govor.Application.Validators;
public class UsernameValidator : IUsernameValidator
{
private readonly Regex _usernameRegex = new(@"^[А-Яа-яЁё]+$", RegexOptions.Compiled);
public void Validate(string username)
{
if(username.Length < UserValidator.MIN_LENGHT_OF_NAME || username.Length > UserValidator.MAX_LENGHT_OF_NAME)
throw new InvalidUsernameException($"Username must be between {UserValidator.MIN_LENGHT_OF_NAME} and {UserValidator.MAX_LENGHT_OF_NAME} characters.");
if (!_usernameRegex.IsMatch(username))
throw new InvalidUsernameException("The username must be in Cyrillic and start with a letter.");
}
public bool TryValidate(string username)
{
try
{
Validate(username);
return true;
}
catch
{
return false;
}
}
}