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

@@ -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;
}
}