Files
hermes-server/Requests/CreateRedeemableAction.cs

53 lines
1.9 KiB
C#

using System.Text.Json;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
{
public class CreateRedeemableAction : IRequest
{
public string Name => "create_redeemable_action";
public string[] RequiredKeys => ["name", "data", "type"];
private ILogger _logger;
public CreateRedeemableAction(ILogger logger)
{
_logger = logger;
}
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
string name = data["name"].ToString()!;
string d = data["data"].ToString()!;
string type = data["type"].ToString()!;
IDictionary<string, string> dict = new Dictionary<string, string>();
try
{
dict = JsonSerializer.Deserialize<IDictionary<string, string>>(d)!;
}
catch (Exception ex)
{
_logger.Error(ex, $"Failed to parse data on redeemable action while creating action [name: {name}][type: {type}][data: {d}]");
return Task.FromResult(RequestResult.Failed("Could not parse the data on this action."));
}
var action = new RedeemableAction()
{
UserId = channel.Id,
Name = name,
Data = dict,
Type = type,
};
bool result = channel.Actions.Set(name, action);
if (result)
{
_logger.Information($"Added redeemable action to channel [name: {name}][type: {type}][channel: {channel.Id}]");
return Task.FromResult(RequestResult.Successful(action));
}
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
}
}