diff --git a/src/FloppyBot.WebApi.V2/Controllers/CustomCommandController.cs b/src/FloppyBot.WebApi.V2/Controllers/CustomCommandController.cs index ecf86dc..3912f2a 100644 --- a/src/FloppyBot.WebApi.V2/Controllers/CustomCommandController.cs +++ b/src/FloppyBot.WebApi.V2/Controllers/CustomCommandController.cs @@ -10,6 +10,7 @@ namespace FloppyBot.WebApi.V2.Controllers; [ApiController] [Route("api/v2/custom-commands/{messageInterface}/{channel}")] +[Route("api/v2/commands/custom/{messageInterface}/{channel}")] [Authorize(Policy = Permissions.READ_CUSTOM_COMMANDS)] public class CustomCommandController : ChannelScopedController { diff --git a/src/FloppyBot.WebApi.V2/Controllers/TimerMessageController.cs b/src/FloppyBot.WebApi.V2/Controllers/TimerMessageController.cs new file mode 100644 index 0000000..4a03293 --- /dev/null +++ b/src/FloppyBot.WebApi.V2/Controllers/TimerMessageController.cs @@ -0,0 +1,55 @@ +using FloppyBot.Commands.Aux.Twitch.Storage; +using FloppyBot.WebApi.Auth; +using FloppyBot.WebApi.Auth.Controllers; +using FloppyBot.WebApi.Auth.UserProfiles; +using FloppyBot.WebApi.V2.Dtos; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace FloppyBot.WebApi.V2.Controllers; + +[ApiController] +[Route("api/v2/commands/config/{messageInterface}/{channel}/timer")] +[Authorize(Policy = Permissions.READ_CONFIG)] +public class TimerMessageController : ChannelScopedController +{ + private readonly ITimerMessageConfigurationService _timerMessageConfigurationService; + + public TimerMessageController( + IUserService userService, + ITimerMessageConfigurationService timerMessageConfigurationService + ) + : base(userService) + { + _timerMessageConfigurationService = timerMessageConfigurationService; + } + + [HttpGet] + public TimerMessageConfigurationDto Get( + [FromRoute] string messageInterface, + [FromRoute] string channel + ) + { + var channelId = EnsureChannelAccess(messageInterface, channel); + + return _timerMessageConfigurationService + .GetConfigForChannel(channelId) + .Select(TimerMessageConfigurationDto.FromEntity) + .FirstOrDefault(TimerMessageConfigurationDto.Empty(channelId)); + } + + [HttpPost] + public IActionResult Update( + [FromRoute] string messageInterface, + [FromRoute] string channel, + [FromBody] TimerMessageConfigurationDto config + ) + { + var channelId = EnsureChannelAccess(messageInterface, channel); + _timerMessageConfigurationService.UpdateConfigurationForChannel( + channelId, + config.ToEntity() + ); + return NoContent(); + } +} diff --git a/src/FloppyBot.WebApi.V2/Dtos/TimerMessageConfigurationDto.cs b/src/FloppyBot.WebApi.V2/Dtos/TimerMessageConfigurationDto.cs new file mode 100644 index 0000000..6639750 --- /dev/null +++ b/src/FloppyBot.WebApi.V2/Dtos/TimerMessageConfigurationDto.cs @@ -0,0 +1,33 @@ +using FloppyBot.Commands.Aux.Twitch.Storage.Entities; + +namespace FloppyBot.WebApi.V2.Dtos; + +public record TimerMessageConfigurationDto( + string ChannelId, + string[] Messages, + int Interval, + int MinMessages +) +{ + private static readonly TimerMessageConfigurationDto EmptyDto = new(string.Empty, [], 0, 0); + + public static TimerMessageConfigurationDto FromEntity(TimerMessageConfiguration entity) + { + return new TimerMessageConfigurationDto( + entity.Id, + entity.Messages, + entity.Interval, + entity.MinMessages + ); + } + + public static TimerMessageConfigurationDto Empty(string channelId) + { + return EmptyDto with { ChannelId = channelId }; + } + + public TimerMessageConfiguration ToEntity() + { + return new TimerMessageConfiguration(ChannelId, Messages, Interval, MinMessages); + } +}