Enhance DataController and BotService with new configuration and bot name checks

- Added IConfiguration dependency to DataController for environment variable access.
- Updated GetPaginatedAgentSummariesCommand to include a flag for filtering profitable agents.
- Implemented HasUserBotWithNameAsync method in IBotService and BotService to check for existing bots by name.
- Modified StartBotCommandHandler and StartCopyTradingCommandHandler to prevent duplicate bot names during strategy creation.
This commit is contained in:
2025-11-22 13:34:26 +07:00
parent 269bbfaab0
commit e69dd43ace
9 changed files with 65 additions and 7 deletions

View File

@@ -73,4 +73,12 @@ public interface IBotService
/// <param name="ticker">The ticker to check</param>
/// <returns>True if the user has a bot on this ticker, false otherwise</returns>
Task<bool> HasUserBotOnTickerAsync(int userId, Ticker ticker);
/// <summary>
/// Checks if the user already has a bot (Running or Saved) with the specified name
/// </summary>
/// <param name="userId">The user ID</param>
/// <param name="name">The bot name to check</param>
/// <returns>True if the user has a bot with this name, false otherwise</returns>
Task<bool> HasUserBotWithNameAsync(int userId, string name);
}

View File

@@ -559,5 +559,13 @@ namespace Managing.Application.ManageBot
bot.Ticker == ticker &&
(bot.Status == BotStatus.Running || bot.Status == BotStatus.Saved));
}
public async Task<bool> HasUserBotWithNameAsync(int userId, string name)
{
var userBots = await _botRepository.GetBotsByUserIdAsync(userId);
return userBots.Any(bot =>
bot.Name.Equals(name, StringComparison.OrdinalIgnoreCase) &&
(bot.Status == BotStatus.Running || bot.Status == BotStatus.Saved));
}
}
}

View File

@@ -34,18 +34,25 @@ namespace Managing.Application.ManageBot.Commands
/// </summary>
public IEnumerable<string>? AgentNames { get; }
/// <summary>
/// Whether to show only profitable agents (ROI > 0)
/// </summary>
public bool ShowOnlyProfitableAgent { get; }
public GetPaginatedAgentSummariesCommand(
int page = 1,
int pageSize = 10,
SortableFields sortBy = SortableFields.NetPnL,
string sortOrder = "desc",
IEnumerable<string>? agentNames = null)
IEnumerable<string>? agentNames = null,
bool showOnlyProfitableAgent = false)
{
Page = page;
PageSize = pageSize;
SortBy = sortBy;
SortOrder = sortOrder;
AgentNames = agentNames;
ShowOnlyProfitableAgent = showOnlyProfitableAgent;
}
}
}

View File

@@ -27,7 +27,8 @@ namespace Managing.Application.ManageBot
request.PageSize,
request.SortBy,
request.SortOrder,
request.AgentNames);
request.AgentNames,
request.ShowOnlyProfitableAgent);
}
}
}

View File

@@ -54,6 +54,15 @@ namespace Managing.Application.ManageBot
"You cannot create multiple strategies on the same ticker.");
}
// Check if user already has a bot with this name
var hasExistingBotWithName = await _botService.HasUserBotWithNameAsync(request.User.Id, request.Config.Name);
if (hasExistingBotWithName)
{
throw new InvalidOperationException(
$"You already have a strategy running or saved with the name '{request.Config.Name}'. " +
"You cannot create multiple strategies with the same name.");
}
Account account;
if (string.IsNullOrEmpty(request.Config.AccountName))
{

View File

@@ -95,6 +95,15 @@ namespace Managing.Application.ManageBot
"You cannot create multiple strategies on the same ticker.");
}
// Check if user already has a bot with the same name as the master bot
var hasExistingBotWithName = await _botService.HasUserBotWithNameAsync(request.User.Id, masterConfig.Name);
if (hasExistingBotWithName)
{
throw new InvalidOperationException(
$"You already have a strategy running or saved with the name '{masterConfig.Name}'. " +
"You cannot create multiple strategies with the same name.");
}
// Get account information from the requesting user's accounts
var userAccounts = await _accountService.GetAccountsByUserAsync(request.User, true, true);
var firstAccount = userAccounts.FirstOrDefault();