Files
Govor/Govor.Console/Commands/BaseCommand.cs
T
google-labs-jules[bot] b42c7d6de6 I refactored Govor.Console to use the command pattern. Here's what I did:
- I implemented a command-based architecture for Govor.Console.
- Each of your actions (e.g., login, send friend request, list friends) is now encapsulated in its own command class inheriting from BaseCommand.
- I integrated friend-related functionalities (search, list, send/accept/reject request, block/unblock) using a mix of SignalR Hub and REST API calls.
- I included admin functionalities for managing friendships (list all, list user's, remove).
- I added a /help (-h) command to display available commands and their usage.
- I refactored Program.cs to handle command parsing and execution.
- I updated FriendsClient.cs, removing methods now handled directly by commands via SignalR.
- I added SignalR event handlers for real-time notifications (friend requests, accepts, etc.).
2025-07-11 05:47:22 +00:00

66 lines
3.1 KiB
C#

using System;
using System.Threading.Tasks;
namespace Govor.ConsoleClient.Commands
{
public abstract class BaseCommand : ICommand
{
protected static FriendsClient? FriendsClient { get; private set; }
protected static HttpClientService HttpClientService { get; private set; }
protected static Func<string?> GetAuthToken { get; private set; }
protected static Action<string> SetAuthToken { get; private set; }
protected static Func<Task> InitializeHubConnectionAsync { get; private set; }
protected static Microsoft.AspNetCore.SignalR.Client.HubConnection? HubConnection => _hubConnection?.Invoke();
private static Func<Microsoft.AspNetCore.SignalR.Client.HubConnection?> _hubConnection;
public static void InitializeServices(
FriendsClient? friendsClient,
HttpClientService httpClientService,
Func<string?> getAuthToken,
Action<string> setAuthToken,
Func<Task> initializeHubConnectionAsync,
Func<Microsoft.AspNetCore.SignalR.Client.HubConnection?> hubConnection)
{
FriendsClient = friendsClient;
HttpClientService = httpClientService;
GetAuthToken = getAuthToken;
SetAuthToken = setAuthToken;
InitializeHubConnectionAsync = initializeHubConnectionAsync;
_hubConnection = hubConnection;
}
public abstract Task ExecuteAsync(string? argument);
public abstract string GetHelp();
protected boolEnsureLoggedIn()
{
if (GetAuthToken() == null)
{
System.Console.WriteLine("[Ошибка] Сначала войдите в систему. Используйте /login или /reg.");
return false;
}
if (FriendsClient == null && !(this is LoginCommand || this is RegisterCommand)) // FriendsClient might not be needed for auth commands
{
// Attempt to re-initialize FriendsClient if token exists but client is null
// This can happen if the app was closed and token was persisted, but client was not re-instantiated.
// However, the current setup in Program.cs initializes FriendsClient after login/reg.
// This check is more of a safeguard.
System.Console.WriteLine("[Ошибка] Клиент для работы с друзьями не инициализирован. Попробуйте войти снова.");
return false;
}
return true;
}
protected boolEnsureHubConnection()
{
if (HubConnection == null || HubConnection.State != Microsoft.AspNetCore.SignalR.Client.HubConnectionState.Connected)
{
System.Console.WriteLine("[Ошибка] SignalR соединение не установлено. Попробуйте войти снова или проверьте соединение.");
return false;
}
return true;
}
}
}