Skip to content

Commit

Permalink
Merge pull request #277 from rGunti/feature/v2-api
Browse files Browse the repository at this point in the history
implemented controller methods for built-in command configuration
  • Loading branch information
rGunti authored May 5, 2024
2 parents d6b7fd4 + 350fa8b commit 8425d57
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/FloppyBot.WebApi.V2/Controllers/CommandConfigController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,53 @@ [FromRoute] string commandName
);
}

[HttpPost("{commandName}")]
[Authorize(Policy = Permissions.EDIT_CONFIG)]
public IActionResult SetCommandConfig(
[FromRoute] string messageInterface,
[FromRoute] string channel,
[FromRoute] string commandName,
[FromBody] CommandConfigurationDto commandConfigurationDto
)
{
var channelId = EnsureChannelAccess(messageInterface, channel);
var command = _distributedCommandRegistry
.GetCommand(commandName)
.OrThrow(() => new NotFoundException("Command does not exist"));
var commandConfiguration =
_commandConfigurationService
.GetCommandConfiguration(channelId, commandName)
.FirstOrDefault()
?? new CommandConfiguration
{
CommandName = command.Name,
ChannelId = channelId,
Disabled = false,
};

_commandConfigurationService.SetCommandConfiguration(
(commandConfigurationDto with { Id = commandConfiguration.Id, }).ToEntity()
);

return NoContent();
}

[HttpDelete("{commandName}")]
[Authorize(Policy = Permissions.EDIT_CONFIG)]
public IActionResult ResetCommand(
[FromRoute] string messageInterface,
[FromRoute] string channel,
[FromRoute] string commandName
)
{
var channelId = EnsureChannelAccess(messageInterface, channel);
_commandConfigurationService.DeleteCommandConfiguration(channelId, commandName);

return NoContent();
}

[HttpPost("{commandName}/disable")]
[Authorize(Policy = Permissions.EDIT_CONFIG)]
public IActionResult DisableCommand(
[FromRoute] string messageInterface,
[FromRoute] string channel,
Expand Down
18 changes: 17 additions & 1 deletion src/FloppyBot.WebApi.V2/Dtos/CommandConfigurationDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace FloppyBot.WebApi.V2.Dtos;

public record CommandConfigurationDto(
string Id,
string? Id,
string ChannelId,
string CommandName,
PrivilegeLevel? RequiredPrivilegeLevel,
Expand All @@ -25,4 +25,20 @@ public static CommandConfigurationDto FromEntity(CommandConfiguration entity)
entity.CustomCooldownConfiguration.Select(CooldownConfigurationDto.FromEntity).ToArray()
);
}

public CommandConfiguration ToEntity()
{
return new CommandConfiguration
{
Id = Id,

Check warning on line 33 in src/FloppyBot.WebApi.V2/Dtos/CommandConfigurationDto.cs

View workflow job for this annotation

GitHub Actions / Build & Test Project

Possible null reference assignment.

Check warning on line 33 in src/FloppyBot.WebApi.V2/Dtos/CommandConfigurationDto.cs

View workflow job for this annotation

GitHub Actions / Build & Test Project

Possible null reference assignment.

Check warning on line 33 in src/FloppyBot.WebApi.V2/Dtos/CommandConfigurationDto.cs

View workflow job for this annotation

GitHub Actions / Publish Web API Agent / Build floppybot/web-api

Possible null reference assignment.

Check warning on line 33 in src/FloppyBot.WebApi.V2/Dtos/CommandConfigurationDto.cs

View workflow job for this annotation

GitHub Actions / Publish Web API Agent / Build floppybot/web-api

Possible null reference assignment.
ChannelId = ChannelId,
CommandName = CommandName,
RequiredPrivilegeLevel = RequiredPrivilegeLevel,
Disabled = Disabled,
CustomCooldown = CustomCooldown,
CustomCooldownConfiguration = CustomCooldownConfiguration
.Select(c => c.ToEntity())
.ToArray(),
};
}
}
9 changes: 9 additions & 0 deletions src/FloppyBot.WebApi.V2/Dtos/CooldownConfigurationDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ public static CooldownConfigurationDto FromEntity(CooldownConfiguration entity)
{
return new CooldownConfigurationDto(entity.PrivilegeLevel, entity.CooldownMs);
}

public CooldownConfiguration ToEntity()
{
return new CooldownConfiguration
{
PrivilegeLevel = PrivilegeLevel,
CooldownMs = CooldownMs,
};
}
}

0 comments on commit 8425d57

Please sign in to comment.