Сильно переделанная версия, добавлены избранные контакты, отправка фото и т.п.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace nsszcontactbot;
|
||||
public class FavoriteContactsManager
|
||||
{
|
||||
private readonly IDatabase _redisDb;
|
||||
private const string KeyPrefix = "user:favorites:";
|
||||
|
||||
public FavoriteContactsManager(ConnectionMultiplexer redis)
|
||||
{
|
||||
_redisDb = redis.GetDatabase();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавить контакт в избранное.
|
||||
/// </summary>
|
||||
public async Task AddFavoriteAsync(long userId, string contactId)
|
||||
{
|
||||
string key = GetRedisKey(userId);
|
||||
await _redisDb.SetAddAsync(key, contactId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удалить контакт из избранного.
|
||||
/// </summary>
|
||||
public async Task RemoveFavoriteAsync(long userId, string contactId)
|
||||
{
|
||||
string key = GetRedisKey(userId);
|
||||
await _redisDb.SetRemoveAsync(key, contactId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Проверить, является ли контакт избранным.
|
||||
/// </summary>
|
||||
public async Task<bool> IsFavoriteAsync(long userId, string contactId)
|
||||
{
|
||||
string key = GetRedisKey(userId);
|
||||
return await _redisDb.SetContainsAsync(key, contactId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получить список всех избранных контактов пользователя.
|
||||
/// </summary>
|
||||
public async Task<List<string>> GetAllFavoritesAsync(long userId)
|
||||
{
|
||||
string key = GetRedisKey(userId);
|
||||
var members = await _redisDb.SetMembersAsync(key);
|
||||
return members.Select(m => m.ToString()).ToList();
|
||||
}
|
||||
|
||||
private string GetRedisKey(long userId) => $"{KeyPrefix}{userId}";
|
||||
}
|
||||
Reference in New Issue
Block a user