98 lines
3.2 KiB
C#
98 lines
3.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using ChaosBot.Models;
|
|
using ChaosBot.WebServer.Models;
|
|
using ChaosBot.WebServer.Services;
|
|
using FlexLabs.EntityFrameworkCore.Upsert;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ChaosBot.WebServer.App.ApiControllers
|
|
{
|
|
[ApiController]
|
|
[Route("/api/custom-commands")]
|
|
public class CustomCommandController : BaseApiController<CustomCommand, string>
|
|
{
|
|
public CustomCommandController(
|
|
AccessTokenCache accessTokenCache,
|
|
ValidationService validationService
|
|
) : base(
|
|
accessTokenCache,
|
|
validationService
|
|
) {}
|
|
|
|
[HttpGet]
|
|
[Route("{guildId}")]
|
|
public async Task<IActionResult> IndexAction(ulong guildId)
|
|
{
|
|
return await Index(guildId);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("{guildId}")]
|
|
public async Task<IActionResult> UpsertAction(
|
|
[FromRoute] ulong guildId,
|
|
[FromBody] JsonElement requestBody)
|
|
{
|
|
return await Upsert(guildId, requestBody);
|
|
}
|
|
|
|
[HttpDelete]
|
|
[Route("{guildId}/{command}")]
|
|
public async Task<IActionResult> DeleteAction([FromRoute]ulong guildId, [FromRoute]string command)
|
|
{
|
|
return await Delete(guildId, command);
|
|
}
|
|
|
|
protected override DbSet<CustomCommand> GetBasicQuery(ChaosbotContext context)
|
|
{
|
|
return context.CustomCommands;
|
|
}
|
|
|
|
protected override IQueryable<CustomCommand> ApplyFilterForCurrentGuild(IQueryable<CustomCommand> query, ulong guildId)
|
|
{
|
|
return query.Where(e => e.DiscordGuildId == guildId);
|
|
}
|
|
|
|
protected override List<string> GetIndexFields() {
|
|
return new List<string>
|
|
{
|
|
"Command",
|
|
"Type",
|
|
"Content"
|
|
};
|
|
}
|
|
|
|
protected override Dictionary<string, List<string>> GetValidationRules()
|
|
{
|
|
return new Dictionary<string, List<string>>
|
|
{
|
|
{"Command", new List<string>{"required", "type:string", "min:1", "max:128"}},
|
|
{"Type", new List<string>{"required", "type:integer", "in:CustomCommandType"}},
|
|
{"Content", new List<string>{"required", "type:string"}},
|
|
};
|
|
}
|
|
|
|
protected override UpsertCommandBuilder<CustomCommand> ApplyFilterForUpsert(
|
|
UpsertCommandBuilder<CustomCommand> builder)
|
|
{
|
|
return builder.On(cc => new {cc.DiscordGuildId, cc.Command});
|
|
}
|
|
|
|
protected override CustomCommand FilterQueryForDeletion(IQueryable<CustomCommand> query, ulong guildId, string deleteParameter)
|
|
{
|
|
return query
|
|
.Where(cc => cc.DiscordGuildId == guildId)
|
|
.First(cc => cc.Command == deleteParameter)
|
|
;
|
|
}
|
|
|
|
protected override List<CustomCommand> FilterQueryMultipleForDeletion(IQueryable<CustomCommand> query, ulong guildId, string deleteParameter)
|
|
{
|
|
return new List<CustomCommand>();
|
|
}
|
|
}
|
|
}
|