mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-22 20:24:55 +00:00
AppTests
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
using Govor.ConsoleClient.Services.Implementations;
|
||||
using Govor.ConsoleClient.Services.Interfaces;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Govor.ConsoleClient.Services.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IInputPipeline, InputPipeline>();
|
||||
services.AddSingleton<ICommandDispatcher, CommandDispatcher>();
|
||||
services.AddSingleton<IMiddlewarePipeline, MiddlewarePipeline>();
|
||||
services.AddSingleton<ConsoleLogger>();
|
||||
services.AddSingleton<ILogger>(sp => sp.GetRequiredService<ConsoleLogger>());
|
||||
return services;
|
||||
}
|
||||
}
|
||||
+7
-4
@@ -1,15 +1,16 @@
|
||||
using System.Reflection;
|
||||
using Govor.ConsoleClient.Commands;
|
||||
using Govor.ConsoleClient.Services.Interfaces;
|
||||
|
||||
namespace Govor.ConsoleClient.Services;
|
||||
namespace Govor.ConsoleClient.Services.Implementations;
|
||||
|
||||
public class CommandDispatcher
|
||||
public class CommandDispatcher : ICommandDispatcher
|
||||
{
|
||||
private readonly Dictionary<string, ICommand> _commands = new();
|
||||
private readonly ILogger _logger;
|
||||
private readonly MiddlewarePipeline _pipeline;
|
||||
private readonly IMiddlewarePipeline _pipeline;
|
||||
|
||||
public CommandDispatcher(IEnumerable<ICommand> commands, ILogger logger, MiddlewarePipeline pipeline)
|
||||
public CommandDispatcher(IEnumerable<ICommand> commands, ILogger logger, IMiddlewarePipeline pipeline)
|
||||
{
|
||||
_logger = logger;
|
||||
_pipeline = pipeline;
|
||||
@@ -38,4 +39,6 @@ public class CommandDispatcher
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<ICommand> GetAllCommands() => _commands.Values;
|
||||
}
|
||||
+3
-1
@@ -1,4 +1,6 @@
|
||||
namespace Govor.ConsoleClient.Services;
|
||||
using Govor.ConsoleClient.Services.Interfaces;
|
||||
|
||||
namespace Govor.ConsoleClient.Services.Implementations;
|
||||
|
||||
public class ConsoleLogger : ILogger
|
||||
{
|
||||
+8
-7
@@ -1,14 +1,15 @@
|
||||
using Govor.ConsoleClient.Commands;
|
||||
using Govor.ConsoleClient.Services.Interfaces;
|
||||
|
||||
namespace Govor.ConsoleClient.Services;
|
||||
namespace Govor.ConsoleClient.Services.Implementations;
|
||||
|
||||
public class InputPipeline
|
||||
public class InputPipeline : IInputPipeline
|
||||
{
|
||||
private readonly CommandDispatcher _dispatcher;
|
||||
private readonly ICommandDispatcher _dispatcher;
|
||||
private readonly ILogger _logger;
|
||||
private IInteractiveCommand? _activeCommand;
|
||||
|
||||
public InputPipeline(CommandDispatcher dispatcher, ILogger logger)
|
||||
public InputPipeline(ICommandDispatcher dispatcher, ILogger logger)
|
||||
{
|
||||
_dispatcher = dispatcher;
|
||||
_logger = logger;
|
||||
@@ -20,12 +21,12 @@ public class InputPipeline
|
||||
|
||||
if (input.StartsWith("/"))
|
||||
{
|
||||
_activeCommand = null; // Сброс активной команды
|
||||
_activeCommand = null;
|
||||
|
||||
var commandInput = input[1..];
|
||||
var result = await _dispatcher.DispatchAsync(commandInput);
|
||||
|
||||
// Если команда поддерживает интерактивность, сохраняем как активную
|
||||
// If the command supports interactivity, save it as active
|
||||
if (result is IInteractiveCommand interactiveCommand && !interactiveCommand.IsCompleted)
|
||||
{
|
||||
_activeCommand = interactiveCommand;
|
||||
@@ -42,7 +43,7 @@ public class InputPipeline
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Info($"[Ввод пользователя]: {input}");
|
||||
_logger.Info($"Введите /help, чтобы узнать доступные команды!");
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -1,10 +1,11 @@
|
||||
using Govor.ConsoleClient.Services.Interfaces;
|
||||
using Govor.ConsoleClient.Services.Middleware;
|
||||
|
||||
namespace Govor.ConsoleClient.Services;
|
||||
namespace Govor.ConsoleClient.Services.Implementations;
|
||||
|
||||
public delegate Task CommandMiddleware(CommandContext context, Func<Task> next);
|
||||
|
||||
public class MiddlewarePipeline
|
||||
public class MiddlewarePipeline : IMiddlewarePipeline
|
||||
{
|
||||
private readonly IList<ICommandMiddleware> _middlewares;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using Govor.ConsoleClient.Commands;
|
||||
|
||||
namespace Govor.ConsoleClient.Services.Interfaces;
|
||||
|
||||
public interface ICommandDispatcher
|
||||
{
|
||||
Task<ICommand?> DispatchAsync(string input);
|
||||
IEnumerable<ICommand> GetAllCommands();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Govor.ConsoleClient.Services.Interfaces;
|
||||
|
||||
public interface IInputPipeline
|
||||
{
|
||||
Task ProcessInputAsync(string input);
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace Govor.ConsoleClient.Services;
|
||||
namespace Govor.ConsoleClient.Services.Interfaces;
|
||||
|
||||
public interface ILogger
|
||||
{
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Govor.ConsoleClient.Services.Interfaces;
|
||||
|
||||
public interface IMiddlewarePipeline
|
||||
{
|
||||
Task ExecuteAsync(CommandContext context);
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using Govor.ConsoleClient.Services.Interfaces;
|
||||
|
||||
namespace Govor.ConsoleClient.Services.Middleware;
|
||||
|
||||
public class ExceptionHandlingMiddleware : ICommandMiddleware
|
||||
@@ -17,7 +19,7 @@ public class ExceptionHandlingMiddleware : ICommandMiddleware
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error($"Произошла ошибка при выполнении команды '{context.Route}': {ex.Message}");
|
||||
_logger.Error($"Произошла ошибка при выполнении команды '{context?.Route}': {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user