- 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.
40 lines
1.4 KiB
C#
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;
|
|
}
|
|
}
|
|
} |