Added database table data into configuration. Store saves is auto-handled. Added Action & Redemption stores.
This commit is contained in:
47
Requests/CreateRedemption.cs
Normal file
47
Requests/CreateRedemption.cs
Normal file
@ -0,0 +1,47 @@
|
||||
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."));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user