Skip to content

Commit

Permalink
implemented api for editing counter value
Browse files Browse the repository at this point in the history
  • Loading branch information
rGunti committed May 5, 2024
1 parent 8425d57 commit d7725e2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
27 changes: 24 additions & 3 deletions src/FloppyBot.WebApi.V2/Controllers/CustomCommandController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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();
}

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ 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,
entity.Name,
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
);
}

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit d7725e2

Please sign in to comment.