Stop all bot for a user
This commit is contained in:
@@ -195,6 +195,43 @@ public class BotController : BaseController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Stops all active bots for the connected user.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A boolean indicating the result of the stop all operation.</returns>
|
||||||
|
[HttpGet]
|
||||||
|
[Route("StopAll")]
|
||||||
|
public async Task<ActionResult<bool>> 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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deletes a bot specified by name.
|
/// Deletes a bot specified by name.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using Managing.Domain.Users;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace Managing.Application.ManageBot.Commands
|
||||||
|
{
|
||||||
|
public class StopAllUserBotsCommand : IRequest<bool>
|
||||||
|
{
|
||||||
|
public User User { get; }
|
||||||
|
|
||||||
|
public StopAllUserBotsCommand(User user)
|
||||||
|
{
|
||||||
|
User = user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<StopAllUserBotsCommand, bool>
|
||||||
|
{
|
||||||
|
private readonly IBotService _botService;
|
||||||
|
private readonly ILogger<StopAllUserBotsCommandHandler> _logger;
|
||||||
|
|
||||||
|
public StopAllUserBotsCommandHandler(IBotService botService, ILogger<StopAllUserBotsCommandHandler> logger)
|
||||||
|
{
|
||||||
|
_botService = botService;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user