using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests { public class CreatePolicy : IRequest { public string Name => "create_policy"; public string[] RequiredKeys => ["groupId", "path", "count", "span"]; private ILogger _logger; public CreatePolicy(ILogger logger) { _logger = logger; } public Task Grant(Channel channel, IDictionary data) { var id = Guid.NewGuid(); string groupId = data["groupId"].ToString()!; string path = data["path"].ToString()!; int count = int.Parse(data["count"].ToString()!); int span = int.Parse(data["span"].ToString()!); var policy = new PolicyMessage() { Id = id, UserId = channel.Id, GroupId = Guid.Parse(groupId), Path = path, Usage = count, Span = span, }; bool result = channel.Policies.Set(id.ToString(), policy); if (result) { _logger.Information($"Added policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {channel.Id}]"); return Task.FromResult(RequestResult.Successful(policy)); } return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache.")); } } }