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:
Artemy
2026-07-16 19:27:45 +07:00
parent 1d35356c8c
commit 6d1c53beeb
371 changed files with 2729 additions and 6694 deletions
@@ -0,0 +1,126 @@
using Govor.Application.Exceptions.FriendsService;
using Govor.Application.Interfaces;
using Govor.Application.PrivateUserChats;
using Govor.Domain;
using Govor.Domain.Common;
using Govor.Domain.Models;
using Microsoft.EntityFrameworkCore;
namespace Govor.Application.Friends;
public class FriendRequestCommandService : IFriendRequestCommandService
{
private readonly GovorDbContext _context;
private readonly IUserPrivateChatsCreator _privateChatsCreator;
public FriendRequestCommandService(
GovorDbContext context,
IUserPrivateChatsCreator privateChatsCreator)
{
_context = context;
_privateChatsCreator = privateChatsCreator;
}
public async Task<Result<Friendship>> SendAsync(Guid fromUserId, Guid toUserId)
{
if (fromUserId == toUserId)
return Result<Friendship>.Failure(new Error(
"Friendship.Send",
"Cannot send a request to self user")
);
var friendship = await _context.Friendships
.FirstOrDefaultAsync(f => (f.RequesterId == fromUserId && f.AddresseeId == toUserId) ||
(f.RequesterId == toUserId && f.AddresseeId == fromUserId));
if (friendship is null)
{
friendship = new Friendship
{
Id = Guid.NewGuid(),
RequesterId = fromUserId,
AddresseeId = toUserId,
Status = FriendshipStatus.Pending
};
await _context.Friendships.AddAsync(friendship);
}
else
{
if (friendship.Status == FriendshipStatus.Pending ||
friendship.Status == FriendshipStatus.Accepted ||
friendship.Status == FriendshipStatus.Blocked)
{
return Result<Friendship>.Failure(new Error(
"Friendship.Send",
$"The request is already {friendship.Status}")
);
}
friendship.RequesterId = fromUserId;
friendship.AddresseeId = toUserId;
friendship.Status = FriendshipStatus.Pending;
}
await _context.SaveChangesAsync();
return friendship;
}
public async Task<Result<Friendship>> AcceptAsync(Guid requestId, Guid currentUserId)
{
var friendship = await _context.Friendships.FindAsync(requestId);
if (friendship is null)
return Result<Friendship>.Failure(new Error(
"Friendship.Accept",
"Friendship not found! You cant accept request!")
);
if (friendship.AddresseeId != currentUserId)
return Result<Friendship>.Failure(new Error(
"Friendship.Accept",
"You cannot accept this request!")
);
if (friendship.Status != FriendshipStatus.Pending)
return Result<Friendship>.Failure(new Error(
"Friendship.Accept",
"Request is already accepted!")
);
friendship.Status = FriendshipStatus.Accepted;
await _context.SaveChangesAsync();
await _privateChatsCreator.CreateAsync(friendship.AddresseeId, friendship.RequesterId);
return friendship;
}
public async Task<Result<Friendship>> RejectAsync(Guid requestId, Guid currentUserId)
{
var friendship = await _context.Friendships.FindAsync(requestId);
if (friendship == null)
return Result<Friendship>.Failure(new Error(
"Friendship.Reject",
"Friendship not found! You cant reject request!")
);
if (friendship.AddresseeId != currentUserId)
return Result<Friendship>.Failure(new Error(
"Friendship.Reject",
"You cannot reject this request!")
);
if (friendship.Status != FriendshipStatus.Pending && friendship.Status != FriendshipStatus.Rejected)
return Result<Friendship>.Failure(new Error(
"Friendship.Reject",
$"Request is already {friendship.Status}")
);
friendship.Status = FriendshipStatus.Rejected;
await _context.SaveChangesAsync();
return friendship;
}
}
@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore;
using Govor.Domain;
using Govor.Domain.Models;
namespace Govor.Application.Friends;
public class FriendRequestQueryService : IFriendRequestQueryService
{
private readonly GovorDbContext _context;
public FriendRequestQueryService(GovorDbContext context)
{
_context = context;
}
public async Task<List<Friendship>> GetIncomingAsync(Guid userId)
{
return await _context.Friendships
.AsNoTracking()
.Include(f => f.Requester)
.Include(f => f.Addressee)
.Where(f => f.AddresseeId == userId && f.Status == FriendshipStatus.Pending)
.ToListAsync();
}
public async Task<List<Friendship>> GetResponsesAsync(Guid userId)
{
return await _context.Friendships
.AsNoTracking()
.Include(f => f.Requester)
.Include(f => f.Addressee)
.Where(f => f.RequesterId == userId && f.Status != FriendshipStatus.Accepted)
.ToListAsync();
}
}
@@ -0,0 +1,14 @@
namespace Govor.Application.Friends;
public class FriendsBlockService : IFriendsBlockService
{
public Task BlockFriendRequestAsync(Guid userId, Guid currentUserId)
{
throw new NotImplementedException();
}
public Task UnblockFriendRequestAsync(Guid userId, Guid currentUserId)
{
throw new NotImplementedException();
}
}
@@ -0,0 +1,62 @@
using Microsoft.EntityFrameworkCore;
using Govor.Domain;
using Govor.Domain.Models;
using Govor.Domain.Models.Users;
namespace Govor.Application.Friends;
public class FriendshipService : IFriendshipService
{
private readonly GovorDbContext _context;
public FriendshipService(GovorDbContext context)
{
_context = context;
}
public async Task<List<User>> SearchUsersAsync(string query, Guid currentId)
{
if (string.IsNullOrWhiteSpace(query))
{
return [];
}
return await _context.Users
.AsNoTracking()
.Where(u => u.Id != currentId && u.Username.Contains(query))
.Take(5)
.ToListAsync();
}
public async Task<List<User>> GetPotentialFriendsAsync(Guid userId)
{
var pendingFriendships = await _context.Friendships
.AsNoTracking()
.Include(f => f.Requester)
.Include(f => f.Addressee)
.Where(f => (f.RequesterId == userId || f.AddresseeId == userId)
&& f.Status == FriendshipStatus.Pending)
.ToListAsync();
return pendingFriendships
.Select(f => f.RequesterId == userId ? f.Addressee : f.Requester)
.ToList();
}
public async Task<List<User>> GetFriendsAsync(Guid userId)
{
var acceptedFriendships = await _context.Friendships
.AsNoTracking()
.Include(f => f.Requester)
.Include(f => f.Addressee)
.Where(f => (f.RequesterId == userId || f.AddresseeId == userId)
&& f.Status == FriendshipStatus.Accepted)
.ToListAsync();
var friends = acceptedFriendships
.Select(f => f.RequesterId == userId ? f.Addressee : f.Requester)
.ToList();
return friends;
}
}
@@ -0,0 +1,11 @@
using Govor.Domain.Common;
using Govor.Domain.Models;
namespace Govor.Application.Friends;
public interface IFriendRequestCommandService
{
Task<Result<Friendship>> SendAsync(Guid fromUserId, Guid toUserId);
Task<Result<Friendship>> AcceptAsync(Guid requestId, Guid currentUserId);
Task<Result<Friendship>> RejectAsync(Guid requestId, Guid currentUserId);
}
@@ -0,0 +1,10 @@
using Govor.Domain.Models;
namespace Govor.Application.Friends;
public interface IFriendRequestQueryService
{
Task<List<Friendship>> GetIncomingAsync(Guid userId);
Task<List<Friendship>> GetResponsesAsync(Guid userId);
}
@@ -0,0 +1,7 @@
namespace Govor.Application.Friends;
public interface IFriendsBlockService
{
Task BlockFriendRequestAsync(Guid userId, Guid currentUserId);
Task UnblockFriendRequestAsync(Guid userId, Guid currentUserId);
}
@@ -0,0 +1,10 @@
using Govor.Domain.Models.Users;
namespace Govor.Application.Friends;
public interface IFriendshipService
{
Task<List<User>> GetFriendsAsync(Guid userId);
Task<List<User>> GetPotentialFriendsAsync(Guid userId);
Task<List<User>> SearchUsersAsync(string query, Guid currentId);
}
@@ -0,0 +1,7 @@
namespace Govor.Application.Friends;
public interface IVerifyFriendship
{
Task VerifyAsync(Guid targetUserId, Guid friendUserId);
Task<bool> TryVerifyAsync(Guid targetUserId, Guid friendUserId);
}
@@ -0,0 +1,59 @@
using Microsoft.EntityFrameworkCore;
using Govor.Application.Exceptions.VerifyFriendship;
using Govor.Domain;
using Govor.Domain.Models;
using Microsoft.Extensions.Logging;
namespace Govor.Application.Friends;
public class VerifyFriendship : IVerifyFriendship
{
private readonly GovorDbContext _dbContext;
private readonly ILogger<VerifyFriendship> _logger;
private const string FriendshipNotAcceptedError = "Friendship between user {0} and friend {1} does not exist or is not accepted.";
public VerifyFriendship(GovorDbContext dbContext, ILogger<VerifyFriendship> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task VerifyAsync(Guid targetUserId, Guid friendUserId)
{
if (targetUserId == Guid.Empty || friendUserId == Guid.Empty)
{
_logger.LogWarning("Invalid user IDs provided: targetUserId={TargetUserId}, friendUserId={FriendUserId}", targetUserId, friendUserId);
throw new ArgumentException("User IDs cannot be empty.");
}
var isFriendshipAccepted = await _dbContext.Friendships
.AsNoTracking()
.AnyAsync(f => f.Status == FriendshipStatus.Accepted &&
((f.RequesterId == targetUserId && f.AddresseeId == friendUserId) ||
(f.RequesterId == friendUserId && f.AddresseeId == targetUserId)));
if (!isFriendshipAccepted)
{
var errorMessage = string.Format(FriendshipNotAcceptedError, targetUserId, friendUserId);
_logger.LogError(errorMessage);
throw new FriendshipException(errorMessage);
}
_logger.LogInformation("Friendship verified successfully for targetUserId={TargetUserId}, friendUserId={FriendUserId}", targetUserId, friendUserId);
}
public async Task<bool> TryVerifyAsync(Guid targetUserId, Guid friendUserId)
{
if (targetUserId == Guid.Empty || friendUserId == Guid.Empty)
{
return false;
}
return await _dbContext.Friendships
.AsNoTracking()
.AnyAsync(f => f.Status == FriendshipStatus.Accepted &&
((f.RequesterId == targetUserId && f.AddresseeId == friendUserId) ||
(f.RequesterId == friendUserId && f.AddresseeId == targetUserId)));
}
}