51 lines
1.9 KiB
C#
51 lines
1.9 KiB
C#
using HermesSocketLibrary.db;
|
|
using HermesSocketLibrary.Requests.Messages;
|
|
using HermesSocketServer.Models;
|
|
using HermesSocketServer.Store;
|
|
using HermesSocketServer.Store.Internal;
|
|
using ILogger = Serilog.ILogger;
|
|
|
|
namespace HermesSocketServer.Requests
|
|
{
|
|
public class CreateGroup : IRequest
|
|
{
|
|
public string Name => "create_group";
|
|
public string[] RequiredKeys => ["name", "priority"];
|
|
private readonly DatabaseTable _table;
|
|
private readonly Database _database;
|
|
private ILogger _logger;
|
|
|
|
public CreateGroup([FromKeyedServices("ChatterGroup")] DatabaseTable table, Database database, ILogger logger)
|
|
{
|
|
_table = table;
|
|
_database = database;
|
|
_logger = logger;
|
|
}
|
|
|
|
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
|
|
{
|
|
var id = Guid.NewGuid();
|
|
string name = data["name"].ToString()!;
|
|
if (!int.TryParse(data["priority"].ToString()!, out var priority))
|
|
return Task.FromResult(RequestResult.Failed("Priority needs to be an integer."));
|
|
|
|
var group = new Group()
|
|
{
|
|
Id = id.ToString(),
|
|
UserId = channel.Id,
|
|
Name = name,
|
|
Priority = priority,
|
|
};
|
|
|
|
bool result = channel.Groups.Set(id.ToString(), group);
|
|
if (result)
|
|
{
|
|
var store = new ChatterGroupStore(channel.Id, group.Id, _table, _database, _logger);
|
|
channel.Groups.Chatters.Add(group.Id, store);
|
|
_logger.Information($"Added group to channel [group id: {id}][name: {name}][priority: {priority}][channel: {channel.Id}]");
|
|
return Task.FromResult(RequestResult.Successful(group));
|
|
}
|
|
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
|
|
}
|
|
}
|
|
} |