diff --git a/src/Managing.Application.Abstractions/Repositories/IBotRepository.cs b/src/Managing.Application.Abstractions/Repositories/IBotRepository.cs index 942e6f57..ed81f9a0 100644 --- a/src/Managing.Application.Abstractions/Repositories/IBotRepository.cs +++ b/src/Managing.Application.Abstractions/Repositories/IBotRepository.cs @@ -14,6 +14,7 @@ public interface IBotRepository Task> GetBotsByUserIdAsync(int id); Task> GetBotsByStatusAsync(BotStatus status); Task GetBotByNameAsync(string name); + Task GetBotByUserIdAndNameAsync(int userId, string name); /// /// Gets paginated bots with filtering and sorting diff --git a/src/Managing.Application/Abstractions/IBotService.cs b/src/Managing.Application/Abstractions/IBotService.cs index 7262e611..79c01a15 100644 --- a/src/Managing.Application/Abstractions/IBotService.cs +++ b/src/Managing.Application/Abstractions/IBotService.cs @@ -19,6 +19,7 @@ public interface IBotService Task> GetBotsByUser(int id); Task> GetBotsByIdsAsync(IEnumerable botIds); Task GetBotByName(string name); + Task GetBotByUserIdAndNameAsync(int userId, string name); Task GetBotByIdentifier(Guid identifier); Task CreateManualSignalAsync(Guid identifier, TradeDirection direction); Task ClosePositionAsync(Guid identifier, Guid positionId); diff --git a/src/Managing.Application/ManageBot/BotService.cs b/src/Managing.Application/ManageBot/BotService.cs index 9e352ea7..2398f52c 100644 --- a/src/Managing.Application/ManageBot/BotService.cs +++ b/src/Managing.Application/ManageBot/BotService.cs @@ -289,6 +289,11 @@ namespace Managing.Application.ManageBot return await _botRepository.GetBotByNameAsync(name); } + public async Task GetBotByUserIdAndNameAsync(int userId, string name) + { + return await _botRepository.GetBotByUserIdAndNameAsync(userId, name); + } + public async Task GetBotByIdentifier(Guid identifier) { return await _botRepository.GetBotByIdentifierAsync(identifier); diff --git a/src/Managing.Application/ManageBot/GetUserStrategyCommandHandler.cs b/src/Managing.Application/ManageBot/GetUserStrategyCommandHandler.cs index 208acb1f..e70f909f 100644 --- a/src/Managing.Application/ManageBot/GetUserStrategyCommandHandler.cs +++ b/src/Managing.Application/ManageBot/GetUserStrategyCommandHandler.cs @@ -1,4 +1,5 @@ using Managing.Application.Abstractions; +using Managing.Application.Abstractions.Services; using Managing.Application.ManageBot.Commands; using Managing.Domain.Bots; using MediatR; @@ -11,19 +12,28 @@ namespace Managing.Application.ManageBot public class GetUserStrategyCommandHandler : IRequestHandler { private readonly IBotService _botService; + private readonly IUserService _userService; - public GetUserStrategyCommandHandler(IBotService botService) + public GetUserStrategyCommandHandler(IBotService botService, IUserService userService) { _botService = botService; + _userService = userService; } public async Task Handle(GetUserStrategyCommand request, CancellationToken cancellationToken) { - var strategy = await _botService.GetBotByName(request.StrategyName); + var user = await _userService.GetUserByAgentName(request.AgentName); + if (user == null) + { + throw new Exception($"User with agent name {request.AgentName} not found"); + } + + var strategy = await _botService.GetBotByUserIdAndNameAsync(user.Id, request.StrategyName); if (strategy == null) { - throw new Exception($"Strategy with name {request.StrategyName} not found"); + throw new Exception($"Strategy with name {request.StrategyName} not found for agent {request.AgentName}"); } + return strategy; } } diff --git a/src/Managing.Infrastructure.Database/PostgreSql/PostgreSqlBotRepository.cs b/src/Managing.Infrastructure.Database/PostgreSql/PostgreSqlBotRepository.cs index 970e3104..15e11c8a 100644 --- a/src/Managing.Infrastructure.Database/PostgreSql/PostgreSqlBotRepository.cs +++ b/src/Managing.Infrastructure.Database/PostgreSql/PostgreSqlBotRepository.cs @@ -162,6 +162,17 @@ public class PostgreSqlBotRepository : IBotRepository return PostgreSqlMappers.Map(entity); } + public async Task GetBotByUserIdAndNameAsync(int userId, string name) + { + var entity = await _context.Bots + .AsNoTracking() + .Include(m => m.User) + .Include(m => m.MasterBotUser) + .FirstOrDefaultAsync(b => b.UserId == userId && b.Name == name) + .ConfigureAwait(false); + return PostgreSqlMappers.Map(entity); + } + public async Task> GetBotsByIdsAsync(IEnumerable identifiers) { try