Files
managing-apps/src/Managing.Application/ManageBot/GetUserStrategyCommandHandler.cs
cryptooda 269bbfaab0 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.
2025-11-22 10:46:07 +07:00

40 lines
1.4 KiB
C#

using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Services;
using Managing.Application.ManageBot.Commands;
using Managing.Domain.Bots;
using MediatR;
namespace Managing.Application.ManageBot
{
/// <summary>
/// Handler for retrieving a specific strategy owned by a user
/// </summary>
public class GetUserStrategyCommandHandler : IRequestHandler<GetUserStrategyCommand, Bot>
{
private readonly IBotService _botService;
private readonly IUserService _userService;
public GetUserStrategyCommandHandler(IBotService botService, IUserService userService)
{
_botService = botService;
_userService = userService;
}
public async Task<Bot> Handle(GetUserStrategyCommand request, CancellationToken cancellationToken)
{
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 for agent {request.AgentName}");
}
return strategy;
}
}
}