diff --git a/src/Managing.Application.Abstractions/Repositories/IBotRepository.cs b/src/Managing.Application.Abstractions/Repositories/IBotRepository.cs index 0473039..2d98040 100644 --- a/src/Managing.Application.Abstractions/Repositories/IBotRepository.cs +++ b/src/Managing.Application.Abstractions/Repositories/IBotRepository.cs @@ -5,4 +5,5 @@ public interface IBotRepository Task InsertBotAsync(BotBackup bot); IEnumerable GetBots(); Task UpdateBackupBot(BotBackup bot); -} + void DeleteBotBackup(string botName); +} \ No newline at end of file diff --git a/src/Managing.Application/Abstractions/IBotService.cs b/src/Managing.Application/Abstractions/IBotService.cs index cca593c..55eee9d 100644 --- a/src/Managing.Application/Abstractions/IBotService.cs +++ b/src/Managing.Application/Abstractions/IBotService.cs @@ -28,4 +28,7 @@ public interface IBotService string scenario, Enums.Timeframe interval, bool isForWatchingOnly); IBot CreateSimpleBot(string botName, Workflow workflow); + Task StopBot(string requestName); + Task DeleteBot(string requestName); + Task RestartBot(string requestName); } \ No newline at end of file diff --git a/src/Managing.Application/ManageBot/BotService.cs b/src/Managing.Application/ManageBot/BotService.cs index 1410eaf..39f9784 100644 --- a/src/Managing.Application/ManageBot/BotService.cs +++ b/src/Managing.Application/ManageBot/BotService.cs @@ -158,6 +158,46 @@ namespace Managing.Application.ManageBot return new SimpleBot(botName, _tradingBotLogger, workflow, this); } + public async Task StopBot(string botName) + { + if (_botTasks.TryGetValue(botName, out var botWrapper)) + { + if (botWrapper.BotInstance is IBot bot) + { + await Task.Run(() => + bot.Stop()); // Assuming Stop is an asynchronous process wrapped in Task.Run for synchronous methods + return bot.GetStatus(); + } + } + + return Enums.BotStatus.Down.ToString(); + } + + public Task DeleteBot(string botName) + { + if (_botTasks.TryRemove(botName, out _)) + { + _botRepository.DeleteBotBackup(botName); + return Task.FromResult(true); + } + + return Task.FromResult(false); + } + + public Task RestartBot(string botName) + { + if (_botTasks.TryGetValue(botName, out var botWrapper)) + { + if (botWrapper.BotInstance is IBot bot) + { + bot.Restart(); + return Task.FromResult(bot.GetStatus()); + } + } + + return Task.FromResult(Enums.BotStatus.Down.ToString()); + } + public ITradingBot CreateScalpingBot(string accountName, MoneyManagement moneyManagement, string name, Enums.Ticker ticker, string scenario, Enums.Timeframe interval, bool isForWatchingOnly) { diff --git a/src/Managing.Application/ManageBot/DeleteBotCommandHandler.cs b/src/Managing.Application/ManageBot/DeleteBotCommandHandler.cs index 36c4757..7ae2734 100644 --- a/src/Managing.Application/ManageBot/DeleteBotCommandHandler.cs +++ b/src/Managing.Application/ManageBot/DeleteBotCommandHandler.cs @@ -8,25 +8,16 @@ namespace Managing.Application.ManageBot; public class DeleteBotCommandHandler : IRequestHandler { private readonly ILogger _log; - private readonly ITaskCache _taskCache; + private readonly IBotService _botService; - public DeleteBotCommandHandler(ITaskCache taskCache, ILogger log) + public DeleteBotCommandHandler(ILogger log, IBotService botService) { - _taskCache = taskCache; _log = log; + _botService = botService; } public Task Handle(DeleteBotCommand request, CancellationToken cancellationToken) { - try - { - _taskCache.Invalidate(request.Name); - return Task.FromResult(true); - } - catch (Exception e) - { - _log.LogError(e.Message); - return Task.FromResult(false); - } + return _botService.DeleteBot(request.Name); } -} +} \ No newline at end of file diff --git a/src/Managing.Application/ManageBot/RestartBotCommandHandler.cs b/src/Managing.Application/ManageBot/RestartBotCommandHandler.cs index b8a1079..c11ad69 100644 --- a/src/Managing.Application/ManageBot/RestartBotCommandHandler.cs +++ b/src/Managing.Application/ManageBot/RestartBotCommandHandler.cs @@ -8,32 +8,16 @@ namespace Managing.Application.ManageBot { public class RestartBotCommandHandler : IRequestHandler { - private readonly ITaskCache _taskCache; + private readonly IBotService _botService; - public RestartBotCommandHandler(ITaskCache taskCache) + public RestartBotCommandHandler(IBotService botService) { - _taskCache = taskCache; + _botService = botService; } public Task Handle(RestartBotCommand request, CancellationToken cancellationToken) { - switch (request.BotType) - { - case BotType.SimpleBot: - var simpleBot = _taskCache.Get(request.Name); - simpleBot.Restart(); - return Task.FromResult(simpleBot.GetStatus()); - case BotType.ScalpingBot: - var scalpingBot = _taskCache.Get(request.Name); - scalpingBot.Restart(); - return Task.FromResult(scalpingBot.GetStatus()); - case BotType.FlippingBot: - var flippingBot = _taskCache.Get(request.Name); - flippingBot.Restart(); - return Task.FromResult(flippingBot.GetStatus()); - default: - return Task.FromResult(BotStatus.Down.ToString()); - } + return _botService.RestartBot(request.Name); } } -} +} \ No newline at end of file diff --git a/src/Managing.Application/ManageBot/StopBotCommandHandler.cs b/src/Managing.Application/ManageBot/StopBotCommandHandler.cs index 5a0a701..def858c 100644 --- a/src/Managing.Application/ManageBot/StopBotCommandHandler.cs +++ b/src/Managing.Application/ManageBot/StopBotCommandHandler.cs @@ -8,32 +8,16 @@ namespace Managing.Application.ManageBot { public class StopBotCommandHandler : IRequestHandler { - private readonly ITaskCache _taskCache; + private readonly IBotService _botService; - public StopBotCommandHandler(ITaskCache taskCache) + public StopBotCommandHandler(IBotService botService) { - _taskCache = taskCache; + _botService = botService; } public Task Handle(StopBotCommand request, CancellationToken cancellationToken) { - switch (request.BotType) - { - case BotType.SimpleBot: - var simpleBot = _taskCache.Get(request.Name); - simpleBot.Stop(); - return Task.FromResult(simpleBot.GetStatus()); - case BotType.ScalpingBot: - var scalpingBot = _taskCache.Get(request.Name); - scalpingBot.Stop(); - return Task.FromResult(scalpingBot.GetStatus()); - case BotType.FlippingBot: - var flippingBot = _taskCache.Get(request.Name); - flippingBot.Stop(); - return Task.FromResult(flippingBot.GetStatus()); - default: - return Task.FromResult(BotStatus.Down.ToString()); - } + return _botService.StopBot(request.Name); } } -} +} \ No newline at end of file diff --git a/src/Managing.Infrastructure.Database/BotRepository.cs b/src/Managing.Infrastructure.Database/BotRepository.cs index 2fa3ec7..573e351 100644 --- a/src/Managing.Infrastructure.Database/BotRepository.cs +++ b/src/Managing.Infrastructure.Database/BotRepository.cs @@ -32,9 +32,8 @@ public class BotRepository : IBotRepository _botRepository.Update(dto); } - public void DeleteBotByName(string name) + public void DeleteBotBackup(string botName) { - var bot = _botRepository.FindOne(b => b.Name == name); - _botRepository.DeleteById(bot.Id.ToString()); + _botRepository.DeleteOne(b => b.Name == botName); } } \ No newline at end of file