Skip to content

Commit

Permalink
Merge pull request #276 from rGunti/feature/v2-api
Browse files Browse the repository at this point in the history
added TimerMessageController
  • Loading branch information
rGunti authored May 4, 2024
2 parents ca68358 + 96cdbf5 commit d6b7fd4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
55 changes: 55 additions & 0 deletions src/FloppyBot.WebApi.V2/Controllers/TimerMessageController.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
33 changes: 33 additions & 0 deletions src/FloppyBot.WebApi.V2/Dtos/TimerMessageConfigurationDto.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit d6b7fd4

Please sign in to comment.