Refactor message service and move tests to Application.Tests

Renamed IMessageService to IMessageCommandService and updated all usages accordingly. Moved and renamed test files from Govor.API.Tests to the new Govor.Application.Tests project, updating namespaces to match. Refactored message-related services and controllers to use the new naming and structure. Added Govor.Application.Tests project to the solution.
This commit is contained in:
Artemy
2025-07-10 18:05:10 +07:00
parent a43a26b307
commit 65a43c09d3
33 changed files with 157 additions and 45 deletions
@@ -1,3 +1,5 @@
using Govor.Application.Interfaces.Friends;
using Govor.Application.Interfaces.Infrastructure.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -8,5 +10,18 @@ namespace Govor.API.Controllers.Friends;
[Route("api/friends")]
public class FriendsRequestQueryController : Controller
{
private readonly ILogger<FriendsController> _logger;
private readonly IFriendRequestQueryService _friendsService;
private readonly ICurrentUserService _currentUserService;
public FriendsRequestQueryController(ILogger<FriendsController> logger,
IFriendRequestQueryService friendsService,
ICurrentUserService currentUserService)
{
_logger = logger;
_friendsService = friendsService;
_currentUserService = currentUserService;
}
}
@@ -1,6 +1,59 @@
using Govor.Application.Exceptions.FriendsService;
using Govor.Application.Interfaces.Friends;
using Govor.Application.Interfaces.Infrastructure.Extensions;
using Govor.Contracts.DTOs;
using Govor.Core.Models;
using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers.Friends;
public class FriendshipController
[Route("api/friends")]
public class FriendshipController : Controller
{
private readonly ILogger<FriendsController> _logger;
private readonly IFriendshipService _friendsService;
//private readonly IUserDtoBuilder _builder;
private readonly ICurrentUserService _currentUserService;
public FriendshipController(ILogger<FriendsController> logger,
IFriendshipService friendsService,
ICurrentUserService currentUserService)
{
_logger = logger;
_friendsService = friendsService;
_currentUserService = currentUserService;
}
[HttpGet("search")] // api/friends/search?query=
public async Task<IActionResult> Search(string query)
{
if (string.IsNullOrWhiteSpace(query))
return BadRequest("Query cannot be empty");
try
{
var result = await _friendsService.SearchUsersAsync(query, _currentUserService.GetCurrentUserId());
return Ok(BuildUserDtos(result));
}
catch (SearchUsersException ex)
{
_logger.LogWarning(ex, ex.Message);
return NotFound(new { error = ex.Message });
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500, new { error = "Internal error during user search." });
}
}
private List<UserDto> BuildUserDtos(IEnumerable<User> users) => users.Select(user => new UserDto
{
Id = user.Id,
Username = user.Username,
Description = user.Description,
WasOnline = user.WasOnline,
IconId = user.IconId
}).ToList();
}
@@ -1,5 +1,6 @@
using Govor.API.Services.AdminsStuff.Interfaces;
using Govor.API.Services.Authentication.Interfaces;
using Govor.Application.Infrastructure.AdminsStuff;
using Govor.Application.Infrastructure.Extensions;
using Govor.Application.Infrastructure.Validators;
using Govor.Application.Interfaces;
@@ -9,7 +10,9 @@ using Govor.Application.Interfaces.Friends;
using Govor.Application.Interfaces.Infrastructure.Extensions;
using Govor.Application.Interfaces.Messages;
using Govor.Application.Services;
using Govor.Application.Services.Authentication;
using Govor.Application.Services.Friends;
using Govor.Application.Services.Messages;
using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models;
@@ -57,7 +60,7 @@ public static class ConfigurationProgramExtensions
services.AddMemoryCache();
services.AddScoped<IPingHandlerService, PingHandlerService>();
services.AddScoped<IMessageService, MessageService>();
services.AddScoped<IMessageCommandService, MessageCommandService>();
services.AddScoped<IVerifyFriendship, VerifyFriendship>();
services.AddScoped<IUserGroupsService, UserGroupsService>();
services.AddScoped<IMessagesLoader, MessagesLoader>();
+4 -4
View File
@@ -14,13 +14,13 @@ namespace Govor.API.Hubs;
public class ChatsHub : Hub
{
private readonly ILogger<ChatsHub> _logger;
private readonly IMessageService _messageService;
private readonly IMessageCommandService _messageCommandService;
private readonly IUserGroupsService _userService;
public ChatsHub(ILogger<ChatsHub> logger, IMessageService messageService, IUserGroupsService userService)
public ChatsHub(ILogger<ChatsHub> logger, IMessageCommandService messageCommandService, IUserGroupsService userService)
{
_logger = logger;
_messageService = messageService;
_messageCommandService = messageCommandService;
_userService = userService;
}
@@ -105,7 +105,7 @@ public class ChatsHub : Hub
Media: request.MediaAttachments?.Select(f => new SendMedia(f.MediaId, f.EncryptedKey)) ??
Array.Empty<SendMedia>());
var result = await _messageService.SendMessageAsync(sendMessageParams);
var result = await _messageCommandService.SendMessageAsync(sendMessageParams);
if (!result.IsSuccess || result.Message.Id == Guid.Empty)
{
+2 -2
View File
@@ -1,7 +1,7 @@
using System.Text;
using Govor.API.Extensions;
using Govor.API.Hubs;
using Govor.Application.Services;
using Govor.Application.Services.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
@@ -19,7 +19,7 @@ builder.Services.AddCors(options =>
{
options.AddPolicy("AllowFrontend", policy =>
{
policy.WithOrigins("http://localhost:5000", "https://5.129.212.144:5000") // Укажите ваш публичный IP
policy.WithOrigins("http://localhost:5000", "https://5.129.212.144:5000")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();