feat(dashboard): add key roles
This commit is contained in:
parent
b52df95b65
commit
65d286389d
4 changed files with 170 additions and 4 deletions
|
|
@ -13,6 +13,8 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
using System.Net;
|
||||
using Catalogger.Backend.Api.Middleware;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Remora.Discord.API;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
|
|
@ -104,4 +106,41 @@ public partial class GuildsController
|
|||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPut("key-roles/{roleId}")]
|
||||
public async Task<IActionResult> AddKeyRoleAsync(string id, ulong roleId)
|
||||
{
|
||||
var (guildId, _) = await ParseGuildAsync(id);
|
||||
var guildConfig = await guildRepository.GetAsync(guildId);
|
||||
|
||||
if (roleCache.GuildRoles(guildId).All(r => r.ID.Value != roleId))
|
||||
throw new ApiError(HttpStatusCode.BadRequest, ErrorCode.BadRequest, "Role not found");
|
||||
|
||||
if (guildConfig.KeyRoles.Contains(roleId))
|
||||
throw new ApiError(
|
||||
HttpStatusCode.BadRequest,
|
||||
ErrorCode.BadRequest,
|
||||
"Role is already a key role"
|
||||
);
|
||||
|
||||
await guildRepository.AddKeyRoleAsync(guildId, DiscordSnowflake.New(roleId));
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("key-roles/{roleId}")]
|
||||
public async Task<IActionResult> RemoveKeyRoleAsync(string id, ulong roleId)
|
||||
{
|
||||
var (guildId, _) = await ParseGuildAsync(id);
|
||||
var guildConfig = await guildRepository.GetAsync(guildId);
|
||||
|
||||
if (!guildConfig.KeyRoles.Contains(roleId))
|
||||
throw new ApiError(
|
||||
HttpStatusCode.BadRequest,
|
||||
ErrorCode.BadRequest,
|
||||
"Role is already not a key role"
|
||||
);
|
||||
|
||||
await guildRepository.RemoveKeyRoleAsync(guildId, DiscordSnowflake.New(roleId));
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ using Catalogger.Backend.Cache;
|
|||
using Catalogger.Backend.Cache.InMemoryCache;
|
||||
using Catalogger.Backend.Database;
|
||||
using Catalogger.Backend.Database.Repositories;
|
||||
using Catalogger.Backend.Extensions;
|
||||
using Catalogger.Backend.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Remora.Discord.API;
|
||||
|
|
@ -91,6 +92,16 @@ public partial class GuildsController(
|
|||
.Select(ToChannel)
|
||||
));
|
||||
|
||||
var roles = roleCache
|
||||
.GuildRoles(guildId)
|
||||
.OrderByDescending(r => r.Position)
|
||||
.Select(r => new GuildRole(
|
||||
r.ID.ToString(),
|
||||
r.Name,
|
||||
r.Position,
|
||||
r.Colour.ToPrettyString()
|
||||
));
|
||||
|
||||
return Ok(
|
||||
new GuildResponse(
|
||||
guild.Id,
|
||||
|
|
@ -98,7 +109,9 @@ public partial class GuildsController(
|
|||
guild.IconUrl,
|
||||
categories,
|
||||
channelsWithoutCategories,
|
||||
guildConfig.Channels
|
||||
roles,
|
||||
guildConfig.Channels,
|
||||
guildConfig.KeyRoles
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
@ -122,13 +135,17 @@ public partial class GuildsController(
|
|||
string IconUrl,
|
||||
IEnumerable<GuildCategory> Categories,
|
||||
IEnumerable<GuildChannel> ChannelsWithoutCategory,
|
||||
Database.Models.Guild.ChannelConfig Config
|
||||
IEnumerable<GuildRole> Roles,
|
||||
Database.Models.Guild.ChannelConfig Config,
|
||||
ulong[] KeyRoles
|
||||
);
|
||||
|
||||
private record GuildCategory(string Id, string Name, IEnumerable<GuildChannel> Channels);
|
||||
|
||||
private record GuildChannel(string Id, string Name, bool CanLogTo, bool CanRedirectFrom);
|
||||
|
||||
private record GuildRole(string Id, string Name, int Position, string Colour);
|
||||
|
||||
[Authorize]
|
||||
[HttpPatch]
|
||||
[ProducesResponseType<Database.Models.Guild.ChannelConfig>(statusCode: StatusCodes.Status200OK)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue