-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #276 from rGunti/feature/v2-api
added TimerMessageController
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/FloppyBot.WebApi.V2/Controllers/TimerMessageController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
src/FloppyBot.WebApi.V2/Dtos/TimerMessageConfigurationDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |