Add GetBotByUserIdAndNameAsync method to IBotService and BotService

- Implemented GetBotByUserIdAndNameAsync in IBotService and BotService to retrieve a bot by user ID and name.
- Updated GetUserStrategyCommandHandler to utilize the new method for fetching strategies based on user ID.
- Added corresponding method in IBotRepository and PostgreSqlBotRepository for database access.
This commit is contained in:
2025-11-22 10:46:07 +07:00
parent 476bcebfe9
commit 269bbfaab0
5 changed files with 31 additions and 3 deletions

View File

@@ -14,6 +14,7 @@ public interface IBotRepository
Task<IEnumerable<Bot>> GetBotsByUserIdAsync(int id);
Task<IEnumerable<Bot>> GetBotsByStatusAsync(BotStatus status);
Task<Bot> GetBotByNameAsync(string name);
Task<Bot> GetBotByUserIdAndNameAsync(int userId, string name);
/// <summary>
/// Gets paginated bots with filtering and sorting

View File

@@ -19,6 +19,7 @@ public interface IBotService
Task<IEnumerable<Bot>> GetBotsByUser(int id);
Task<IEnumerable<Bot>> GetBotsByIdsAsync(IEnumerable<Guid> botIds);
Task<Bot> GetBotByName(string name);
Task<Bot> GetBotByUserIdAndNameAsync(int userId, string name);
Task<Bot> GetBotByIdentifier(Guid identifier);
Task<LightSignal> CreateManualSignalAsync(Guid identifier, TradeDirection direction);
Task<Position> ClosePositionAsync(Guid identifier, Guid positionId);

View File

@@ -289,6 +289,11 @@ namespace Managing.Application.ManageBot
return await _botRepository.GetBotByNameAsync(name);
}
public async Task<Bot> GetBotByUserIdAndNameAsync(int userId, string name)
{
return await _botRepository.GetBotByUserIdAndNameAsync(userId, name);
}
public async Task<Bot> GetBotByIdentifier(Guid identifier)
{
return await _botRepository.GetBotByIdentifierAsync(identifier);

View File

@@ -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<GetUserStrategyCommand, Bot>
{
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<Bot> 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;
}
}

View File

@@ -162,6 +162,17 @@ public class PostgreSqlBotRepository : IBotRepository
return PostgreSqlMappers.Map(entity);
}
public async Task<Bot> 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<IEnumerable<Bot>> GetBotsByIdsAsync(IEnumerable<Guid> identifiers)
{
try