mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
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.).
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Govor.Contracts.DTOs; // Required for FriendshipDto
|
||||
|
||||
namespace Govor.ConsoleClient.Commands
|
||||
{
|
||||
public class AdminListUserFriendshipsCommand : BaseCommand
|
||||
{
|
||||
public override async Task ExecuteAsync(string? argument)
|
||||
{
|
||||
if (!EnsureLoggedIn()) return;
|
||||
|
||||
Guid userId;
|
||||
if (string.IsNullOrWhiteSpace(argument) || !Guid.TryParse(argument, out userId))
|
||||
{
|
||||
Console.Write("Введите ID пользователя для просмотра его связей: ");
|
||||
var input = Console.ReadLine();
|
||||
if (string.IsNullOrWhiteSpace(input) || !Guid.TryParse(input, out userId))
|
||||
{
|
||||
Console.WriteLine("[Ошибка] Неверный или пустой ID пользователя.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"Получение всех связей для пользователя {userId} (только для администраторов)...");
|
||||
try
|
||||
{
|
||||
var response = await HttpClientService.GetAsync($"api/admin/Friendships/{userId}");
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.Forbidden)
|
||||
{
|
||||
Console.WriteLine("[Ошибка] Доступ запрещен. Эта команда только для администраторов.");
|
||||
return;
|
||||
}
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var friendships = await response.Content.ReadFromJsonAsync<List<FriendshipDto>>();
|
||||
|
||||
if (friendships != null && friendships.Any())
|
||||
{
|
||||
Console.WriteLine($"Дружеские связи для пользователя {userId}:");
|
||||
foreach (var f in friendships)
|
||||
{
|
||||
Console.WriteLine($"- ID: {f.Id}, Пользователь1: {f.RequesterId}, Пользователь2: {f.AddresseeId}, Статус: {f.Status}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Дружеские связи для пользователя {userId} не найдены.");
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException httpEx) when (httpEx.StatusCode == System.Net.HttpStatusCode.Forbidden)
|
||||
{
|
||||
Console.WriteLine("[Ошибка] Доступ запрещен. Эта команда только для администраторов.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[Ошибка] {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public override string GetHelp()
|
||||
{
|
||||
return "/adminlistuserfs [ID_пользователя] - (Админ) Показать все дружеские связи для указанного пользователя. Если ID не указан, запросит ввод.";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user