hermes-server/Requests/CreateRedemption.cs

47 lines
1.8 KiB
C#
Raw Normal View History

using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
{
public class CreateRedemption : IRequest
{
public string Name => "create_redemption";
public string[] RequiredKeys => ["redemption", "action", "order"];
private ILogger _logger;
public CreateRedemption(ILogger logger)
{
_logger = logger;
}
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
var id = Guid.NewGuid();
string redemptionId = data["redemption"].ToString()!;
string actionName = data["action"].ToString()!;
if (channel.Actions.Get(actionName) == null)
return Task.FromResult(RequestResult.Failed("Action Name must be an existing action."));
if (!int.TryParse(data["order"].ToString()!, out var order))
return Task.FromResult(RequestResult.Failed("Order must be an integer."));
var redemption = new Redemption()
{
Id = id.ToString(),
UserId = channel.Id,
RedemptionId = redemptionId,
ActionName = actionName,
Order = order,
State = true,
};
bool result = channel.Redemptions.Set(id.ToString(), redemption);
if (result)
{
_logger.Information($"Added redemption to channel [id: {id}][redemption id: {redemptionId}][action: {actionName}][order: {order}][channel: {channel.Id}]");
return Task.FromResult(RequestResult.Successful(redemption));
}
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
}
}