diff --git a/src/Managing.Api/Controllers/BotController.cs b/src/Managing.Api/Controllers/BotController.cs
index 105e210..e0d95d6 100644
--- a/src/Managing.Api/Controllers/BotController.cs
+++ b/src/Managing.Api/Controllers/BotController.cs
@@ -195,6 +195,43 @@ public class BotController : BaseController
}
}
+ ///
+ /// Stops all active bots for the connected user.
+ ///
+ /// A boolean indicating the result of the stop all operation.
+ [HttpGet]
+ [Route("StopAll")]
+ public async Task> StopAll()
+ {
+ try
+ {
+ var user = await GetUser();
+ if (user == null)
+ {
+ return Unauthorized("User not found");
+ }
+
+ var result = await _mediator.Send(new StopAllUserBotsCommand(user));
+
+ if (result)
+ {
+ await NotifyBotSubscriberAsync();
+ _logger.LogInformation($"All bots stopped successfully for user {user.Name}");
+ }
+ else
+ {
+ _logger.LogWarning($"Failed to stop all bots for user {user.Name}");
+ }
+
+ return Ok(result);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Error stopping all bots");
+ return StatusCode(500, $"Error stopping all bots: {ex.Message}");
+ }
+ }
+
///
/// Deletes a bot specified by name.
///
diff --git a/src/Managing.Application/ManageBot/Commands/StopAllUserBotsCommand.cs b/src/Managing.Application/ManageBot/Commands/StopAllUserBotsCommand.cs
new file mode 100644
index 0000000..86f582b
--- /dev/null
+++ b/src/Managing.Application/ManageBot/Commands/StopAllUserBotsCommand.cs
@@ -0,0 +1,15 @@
+using Managing.Domain.Users;
+using MediatR;
+
+namespace Managing.Application.ManageBot.Commands
+{
+ public class StopAllUserBotsCommand : IRequest
+ {
+ public User User { get; }
+
+ public StopAllUserBotsCommand(User user)
+ {
+ User = user;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Managing.Application/ManageBot/StopAllUserBotsCommandHandler.cs b/src/Managing.Application/ManageBot/StopAllUserBotsCommandHandler.cs
new file mode 100644
index 0000000..4abc478
--- /dev/null
+++ b/src/Managing.Application/ManageBot/StopAllUserBotsCommandHandler.cs
@@ -0,0 +1,55 @@
+using Managing.Application.Abstractions;
+using Managing.Application.ManageBot.Commands;
+using MediatR;
+using Microsoft.Extensions.Logging;
+using static Managing.Common.Enums;
+
+namespace Managing.Application.ManageBot
+{
+ public class StopAllUserBotsCommandHandler : IRequestHandler
+ {
+ private readonly IBotService _botService;
+ private readonly ILogger _logger;
+
+ public StopAllUserBotsCommandHandler(IBotService botService, ILogger logger)
+ {
+ _botService = botService;
+ _logger = logger;
+ }
+
+ public async Task Handle(StopAllUserBotsCommand request, CancellationToken cancellationToken)
+ {
+ try
+ {
+ // Get all bots for the user
+ var userBots = await _botService.GetBotsByUser(request.User.Id);
+
+ // Filter only active bots (status Up)
+ var activeBots = userBots.Where(bot => bot.Status == BotStatus.Up).ToList();
+
+ if (!activeBots.Any())
+ {
+ _logger.LogInformation("No active bots found for user {UserName}", request.User.Name);
+ return true;
+ }
+
+ _logger.LogInformation("Stopping {BotCount} active bots for user {UserName}",
+ activeBots.Count, request.User.Name);
+
+ // Stop each active bot
+ var stopTasks = activeBots.Select(bot => _botService.StopBot(bot.Identifier));
+ await Task.WhenAll(stopTasks);
+
+ _logger.LogInformation("Successfully stopped {BotCount} bots for user {UserName}",
+ activeBots.Count, request.User.Name);
+
+ return true;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Error stopping all bots for user {UserName}", request.User.Name);
+ return false;
+ }
+ }
+ }
+}
\ No newline at end of file