From 93a6f9fd9e9bbbae9343564dfcb3ee185ae9b450 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Wed, 6 Aug 2025 16:03:42 +0700 Subject: [PATCH] Stop all bot for a user --- src/Managing.Api/Controllers/BotController.cs | 37 +++++++++++++ .../Commands/StopAllUserBotsCommand.cs | 15 +++++ .../StopAllUserBotsCommandHandler.cs | 55 +++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 src/Managing.Application/ManageBot/Commands/StopAllUserBotsCommand.cs create mode 100644 src/Managing.Application/ManageBot/StopAllUserBotsCommandHandler.cs 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