mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
Refactor: migrate Core -> Domain and reorganize projects
Large refactor that renames/moves core types into a new Govor.Domain surface and reorganizes the Application layer. Models, configurations, migrations and many files moved from Govor.Core/Govor.Data to Govor.Domain; numerous Application services, interfaces and implementations were relocated or added (authentication, friends, messages, medias, push notifications, user sessions, storage, synching, private chats, etc.). Tests updated to use Govor.Domain namespaces and adjusted project references (removed Govor.Data reference from API tests). Also updated API, Hub and mapping code and project files to reflect the new structure and naming. This is primarily a codebase-wide namespace and module reorganization to establish a Domain project and restructure application services.
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
using Govor.Domain.Common;
|
||||
using Govor.Domain.Models;
|
||||
|
||||
namespace Govor.Application.Infrastructure.AdminsStuff;
|
||||
|
||||
public interface IInvitationGetter
|
||||
{
|
||||
Task<List<Invitation>> GetAllAsync();
|
||||
Task<Result<Invitation>> FindByIdAsync(Guid id);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Govor.Application.Infrastructure.AdminsStuff;
|
||||
|
||||
public interface IInvitationGenerator
|
||||
{
|
||||
public Task<string> GenerateInvitationCode(DateTime time, int maxUsers, bool isAdmin, string description = "");
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Govor.Domain.Models.Users;
|
||||
|
||||
namespace Govor.Application.Infrastructure.AdminsStuff;
|
||||
|
||||
public interface IUsersAdministration
|
||||
{
|
||||
Task<List<User>> GetAllUsersAsync();
|
||||
Task<User> GetUserById(Guid userId);
|
||||
Task SetPasswordAsync(Guid userId, string password);
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Repositories.Invaites;
|
||||
using Govor.Domain;
|
||||
using Govor.Domain.Models;
|
||||
|
||||
namespace Govor.Application.Infrastructure.AdminsStuff;
|
||||
|
||||
public class InvitationGenerator(IInvitesRepository repository) : IInvitationGenerator
|
||||
public class InvitationGenerator(GovorDbContext context) : IInvitationGenerator
|
||||
{
|
||||
public async Task<string> GenerateInvitationCode(DateTime time, int maxUsers, bool isAdmin, string description = "")
|
||||
{
|
||||
@@ -19,7 +18,9 @@ public class InvitationGenerator(IInvitesRepository repository) : IInvitationGen
|
||||
IsAdmin = isAdmin
|
||||
};
|
||||
|
||||
await repository.AddAsync(newInvitation);
|
||||
await context.Invitations.AddAsync(newInvitation);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
return newInvitation.Code;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using Govor.Domain;
|
||||
using Govor.Domain.Common;
|
||||
using Govor.Domain.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Govor.Application.Infrastructure.AdminsStuff;
|
||||
|
||||
public class InvitationGetter : IInvitationGetter
|
||||
{
|
||||
private readonly ILogger<InvitationGetter> _logger;
|
||||
private readonly GovorDbContext _context;
|
||||
|
||||
public InvitationGetter(ILogger<InvitationGetter> logger, GovorDbContext context)
|
||||
{
|
||||
_logger = logger;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<List<Invitation>> GetAllAsync()
|
||||
{
|
||||
return await _context.Invitations
|
||||
.AsNoTracking()
|
||||
.Where(iv => iv.IsActive)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Result<Invitation>> FindByIdAsync(Guid id)
|
||||
{
|
||||
var res = await _context.Invitations.AsNoTracking()
|
||||
.FirstOrDefaultAsync(iv => iv.Id == id);
|
||||
|
||||
if (res is null)
|
||||
return Result<Invitation>.Failure(new Error(
|
||||
nameof(InvalidOperationException),
|
||||
"Invitation not found.")
|
||||
);
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -1,54 +1,45 @@
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Core.Infrastructure.Extensions;
|
||||
using Govor.Core.Models.Users;
|
||||
using Govor.Core.Repositories.Users;
|
||||
using Govor.Data.Repositories.Exceptions;
|
||||
using Govor.Application.Authentication;
|
||||
using Govor.Domain;
|
||||
using Govor.Domain.Models.Users;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Govor.Application.Infrastructure.AdminsStuff;
|
||||
|
||||
public class UsersService : IUsersAdministration
|
||||
{
|
||||
private readonly IUsersRepository _usersRepository;
|
||||
private readonly GovorDbContext _context;
|
||||
private readonly IPasswordHasher _passwordHasher;
|
||||
|
||||
public UsersService(IUsersRepository usersRepository, IPasswordHasher passwordHasher)
|
||||
public UsersService(GovorDbContext context, IPasswordHasher passwordHasher)
|
||||
{
|
||||
_usersRepository = usersRepository;
|
||||
_context = context;
|
||||
_passwordHasher = passwordHasher;
|
||||
}
|
||||
|
||||
public async Task<List<User>> GetAllUsersAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var results = await _usersRepository.GetAllAsync();
|
||||
return results;
|
||||
}
|
||||
catch (NotFoundException ex)
|
||||
{
|
||||
return new List<User>();
|
||||
}
|
||||
var results = await _context.Users
|
||||
.AsNoTracking()
|
||||
.Take(50)
|
||||
.ToListAsync();
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task SetPasswordAsync(Guid userId, string password)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = await _usersRepository.FindByIdAsync(userId);
|
||||
|
||||
user.PasswordHash = _passwordHasher.Hash(password);
|
||||
|
||||
await _usersRepository.UpdateAsync(user);
|
||||
}
|
||||
catch (NotFoundException ex)
|
||||
{
|
||||
throw new NotFoundException(ex.Message);
|
||||
}
|
||||
var user = await GetUserById(userId);
|
||||
|
||||
if (user is null)
|
||||
return;
|
||||
|
||||
user.PasswordHash = _passwordHasher.Hash(password);
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<User> GetUserById(Guid userId)
|
||||
{
|
||||
var result = await _usersRepository.FindByIdAsync(userId);
|
||||
var result = await _context.Users.FirstOrDefaultAsync(user => user.Id == userId);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
using System.Security.Claims;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Govor.Application.Infrastructure.Extensions;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Govor.Application.Infrastructure.Extensions;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Govor.Application.Infrastructure.Extensions;
|
||||
|
||||
public interface IConnectionStore
|
||||
{
|
||||
void AddConnection(Guid userId, string connectionId);
|
||||
void RemoveConnection(Guid userId, string connectionId);
|
||||
IEnumerable<string> GetConnections(Guid userId);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Govor.Application.Infrastructure.Extensions;
|
||||
|
||||
public interface ICurrentUserService
|
||||
{
|
||||
Guid GetCurrentUserId();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Govor.Application.Infrastructure.Extensions;
|
||||
|
||||
public interface ICurrentUserSessionService
|
||||
{
|
||||
Guid GetUserSessionId();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Govor.Domain.Models;
|
||||
using Govor.Domain.Models.Users;
|
||||
|
||||
namespace Govor.Application.Infrastructure.Extensions;
|
||||
|
||||
public interface IFriendsService
|
||||
{
|
||||
Task<List<User>> SearchUsersAsync(string query, Guid currentId);
|
||||
Task SendFriendRequestAsync(Guid fromUserId, Guid toUserId);
|
||||
Task AcceptFriendRequestAsync(Guid requestId, Guid currentUserId);
|
||||
Task RejectFriendRequestAsync(Guid requestId, Guid currentUserId);
|
||||
Task<List<User>> GetFriendsAsync(Guid userId);
|
||||
Task<List<Friendship>> GetResponsesAsync(Guid userId);
|
||||
Task<List<Friendship>> GetIncomingRequestsAsync(Guid userId);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using Govor.Domain.Common;
|
||||
|
||||
namespace Govor.Application.Infrastructure.Validators;
|
||||
|
||||
public interface IUsernameValidator
|
||||
{
|
||||
Result Validate(string username);
|
||||
bool TryValidate(string username);
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Govor.Application.Exceptions.AuthService;
|
||||
using Govor.Application.Interfaces.Authentication;
|
||||
using Govor.Core.Infrastructure.Validators;
|
||||
using Govor.Application.Authentication.Exceptions;
|
||||
using Govor.Domain.Common.Constants;
|
||||
using Govor.Domain.Common;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Govor.Application.Infrastructure.Validators;
|
||||
|
||||
public class UsernameValidator : IUsernameValidator
|
||||
{
|
||||
private const string ErrorCode = nameof(InvalidUsernameException);
|
||||
|
||||
private readonly Regex _usernameRegex = new(@"^[А-Яа-яЁё]+[А-Яа-яЁё0-9]*$", RegexOptions.Compiled);
|
||||
|
||||
private readonly HashSet<string> _blockedExact;
|
||||
@@ -37,43 +39,61 @@ public class UsernameValidator : IUsernameValidator
|
||||
?? throw new InvalidOperationException("Reserved not set");
|
||||
}
|
||||
|
||||
public void Validate(string username)
|
||||
public Result 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.");
|
||||
if (username.Length < UserConstants.MIN_LENGHT_OF_NAME || username.Length > UserConstants.MAX_LENGHT_OF_NAME)
|
||||
{
|
||||
return new Error(
|
||||
ErrorCode,
|
||||
$"Username must be between {UserConstants.MIN_LENGHT_OF_NAME} and {UserConstants.MAX_LENGHT_OF_NAME} characters.");
|
||||
}
|
||||
|
||||
if (!_usernameRegex.IsMatch(username))
|
||||
{
|
||||
return new Error(
|
||||
ErrorCode,
|
||||
"The username must be in Cyrillic and start with a letter.");
|
||||
}
|
||||
|
||||
if (Regex.IsMatch(username, @"(.)\1{4,}"))
|
||||
throw new InvalidUsernameException("Too many repeating characters.");
|
||||
{
|
||||
return new Error(
|
||||
ErrorCode,
|
||||
"Too many repeating characters.");
|
||||
}
|
||||
|
||||
var normalized = Normalize(username);
|
||||
|
||||
|
||||
if (_reserved.Contains(normalized))
|
||||
throw new InvalidUsernameException("This username is reserved.");
|
||||
|
||||
{
|
||||
return new Error(
|
||||
ErrorCode,
|
||||
"This username is reserved.");
|
||||
}
|
||||
|
||||
if (_blockedExact.Contains(normalized))
|
||||
throw new InvalidUsernameException("This username is not allowed.");
|
||||
|
||||
{
|
||||
return new Error(
|
||||
ErrorCode,
|
||||
"This username is not allowed.");
|
||||
}
|
||||
|
||||
foreach (var banned in _blockedContains)
|
||||
{
|
||||
if (normalized.Contains(banned))
|
||||
throw new InvalidUsernameException("Username contains prohibited content.");
|
||||
{
|
||||
return new Error(
|
||||
ErrorCode,
|
||||
"Username contains prohibited content.");
|
||||
}
|
||||
}
|
||||
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
public bool TryValidate(string username)
|
||||
{
|
||||
try
|
||||
{
|
||||
Validate(username);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Validate(username).IsSuccess;
|
||||
}
|
||||
|
||||
private static string Normalize(string username)
|
||||
@@ -87,4 +107,4 @@ public class UsernameValidator : IUsernameValidator
|
||||
.Replace("6", "б")
|
||||
.Replace("8", "в");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user