diff --git a/src/FloppyBot.WebApi.V2/Controllers/CustomCommandController.cs b/src/FloppyBot.WebApi.V2/Controllers/CustomCommandController.cs index 3912f2a..7fc9d77 100644 --- a/src/FloppyBot.WebApi.V2/Controllers/CustomCommandController.cs +++ b/src/FloppyBot.WebApi.V2/Controllers/CustomCommandController.cs @@ -15,14 +15,17 @@ namespace FloppyBot.WebApi.V2.Controllers; public class CustomCommandController : ChannelScopedController { private readonly ICustomCommandService _customCommandService; + private readonly ICounterStorageService _counterStorageService; public CustomCommandController( IUserService userService, - ICustomCommandService customCommandService + ICustomCommandService customCommandService, + ICounterStorageService counterStorageService ) : base(userService) { _customCommandService = customCommandService; + _counterStorageService = counterStorageService; } [HttpGet] @@ -34,7 +37,9 @@ [FromRoute] string channel var channelId = EnsureChannelAccess(messageInterface, channel); return _customCommandService .GetCommandsOfChannel(channelId) - .Select(CustomCommandDto.FromEntity) + .Select(command => + CustomCommandDto.FromEntity(command, _counterStorageService.Peek(command.Id)) + ) .ToList(); } @@ -52,10 +57,11 @@ [FromRoute] string commandName return NotFound(); } - return CustomCommandDto.FromEntity(command); + return CustomCommandDto.FromEntity(command, _counterStorageService.Peek(command.Id)); } [HttpPost] + [Authorize(Policy = Permissions.EDIT_CUSTOM_COMMANDS)] public IActionResult CreateCommand( [FromRoute] string messageInterface, [FromRoute] string channel, @@ -69,10 +75,19 @@ [FromBody] CustomCommandDto createDto return BadRequest(); } + if (createDto.Counter is not null) + { + var createdCommand = + _customCommandService.GetCommand(channelId, createDto.Name) + ?? throw new InvalidOperationException("Command not found after creation."); + _counterStorageService.Set(createdCommand.Id, createDto.Counter.Value); + } + return NoContent(); } [HttpPut("{commandName}")] + [Authorize(Policy = Permissions.EDIT_CUSTOM_COMMANDS)] public IActionResult UpdateCommand( [FromRoute] string messageInterface, [FromRoute] string channel, @@ -90,10 +105,16 @@ [FromBody] CustomCommandDto updateDto command = updateDto.ToEntity().WithId(command.Id) with { Owners = command.Owners, }; _customCommandService.UpdateCommand(command); + if (updateDto.Counter is not null) + { + _counterStorageService.Set(command.Id, updateDto.Counter.Value); + } + return NoContent(); } [HttpDelete("{commandName}")] + [Authorize(Policy = Permissions.EDIT_CUSTOM_COMMANDS)] public IActionResult DeleteCommand( [FromRoute] string messageInterface, [FromRoute] string channel, diff --git a/src/FloppyBot.WebApi.V2/Dtos/CustomCommands/CustomCommandReportDto.cs b/src/FloppyBot.WebApi.V2/Dtos/CustomCommands/CustomCommandReportDto.cs index 36d6a48..43d98e5 100644 --- a/src/FloppyBot.WebApi.V2/Dtos/CustomCommands/CustomCommandReportDto.cs +++ b/src/FloppyBot.WebApi.V2/Dtos/CustomCommands/CustomCommandReportDto.cs @@ -10,10 +10,11 @@ public record CustomCommandDto( string[] Aliases, CommandResponseDto[] Responses, CommandLimitationDto Limitations, - CommandResponseMode ResponseMode + CommandResponseMode ResponseMode, + CounterValueDto? Counter ) { - public static CustomCommandDto FromEntity(CustomCommandDescription entity) + public static CustomCommandDto FromEntity(CustomCommandDescription entity, int? counter) { return new CustomCommandDto( entity.Id, @@ -21,7 +22,8 @@ public static CustomCommandDto FromEntity(CustomCommandDescription entity) entity.Aliases.OrderBy(s => s).ToArray(), entity.Responses.Select(CommandResponseDto.FromEntity).ToArray(), CommandLimitationDto.FromEntity(entity.Limitations), - ToEntityMode(entity.ResponseMode) + ToEntityMode(entity.ResponseMode), + counter.HasValue ? new CounterValueDto(counter.Value) : null ); } @@ -149,6 +151,8 @@ public enum CommandResponseMode All, } +public record CounterValueDto(int Value); + public static class CommandResponseModeExtensions { public static Commands.Custom.Storage.Entities.CommandResponseMode ToEntity(