90 lines
2.8 KiB
C#
90 lines
2.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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
|
|
) : base(
|
|
accessTokenCache
|
|
) {}
|
|
|
|
[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] dynamic 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>>();
|
|
}
|
|
|
|
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>();
|
|
}
|
|
}
|
|
}
|