90 lines
3.2 KiB
C#
90 lines
3.2 KiB
C#
|
|
// Copyright (C) 2021-present sam (starshines.gay)
|
||
|
|
//
|
||
|
|
// This program is free software: you can redistribute it and/or modify
|
||
|
|
// it under the terms of the GNU Affero General Public License as published
|
||
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
||
|
|
// (at your option) any later version.
|
||
|
|
//
|
||
|
|
// This program is distributed in the hope that it will be useful,
|
||
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
|
// GNU Affero General Public License for more details.
|
||
|
|
//
|
||
|
|
// 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 Catalogger.Backend.Database.Models;
|
||
|
|
using Dapper;
|
||
|
|
using Remora.Rest.Core;
|
||
|
|
|
||
|
|
namespace Catalogger.Backend.Database.Dapper.Repositories;
|
||
|
|
|
||
|
|
public class GuildRepository(ILogger logger, DatabaseConnection conn)
|
||
|
|
: IDisposable,
|
||
|
|
IAsyncDisposable
|
||
|
|
{
|
||
|
|
private readonly ILogger _logger = logger.ForContext<GuildRepository>();
|
||
|
|
|
||
|
|
public async Task<Guild> GetAsync(Optional<Snowflake> id) => await GetAsync(id.Value.Value);
|
||
|
|
|
||
|
|
public async Task<Guild> GetAsync(Snowflake id) => await GetAsync(id.Value);
|
||
|
|
|
||
|
|
public async Task<Guild> GetAsync(ulong id)
|
||
|
|
{
|
||
|
|
_logger.Debug("Getting guild config for {GuildId}", id);
|
||
|
|
|
||
|
|
var guild = await conn.QueryFirstOrDefaultAsync<Guild>(
|
||
|
|
"select * from guilds where id = @Id",
|
||
|
|
new { Id = id }
|
||
|
|
);
|
||
|
|
if (guild == null)
|
||
|
|
throw new CataloggerError("Guild not found, was not initialized during guild create");
|
||
|
|
return guild;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> IsGuildKnown(ulong id) =>
|
||
|
|
await conn.ExecuteScalarAsync<bool>(
|
||
|
|
"select exists(select id from guilds where id = @Id)",
|
||
|
|
new { Id = id }
|
||
|
|
);
|
||
|
|
|
||
|
|
public async Task AddGuildAsync(ulong id) =>
|
||
|
|
await conn.ExecuteAsync(
|
||
|
|
"""
|
||
|
|
insert into guilds (id, key_roles, banned_systems, key_roles, channels)
|
||
|
|
values (@Id, array[]::bigint[], array[]::text[], array[]::bigint[], @Channels)
|
||
|
|
on conflict do nothing
|
||
|
|
""",
|
||
|
|
new { Id = id, Channels = new Guild.ChannelConfig() }
|
||
|
|
);
|
||
|
|
|
||
|
|
public async Task BanSystemAsync(Snowflake guildId, string hid, Guid uuid) =>
|
||
|
|
await conn.ExecuteAsync(
|
||
|
|
"update guilds set banned_systems = array_cat(banned_systems, @SystemIds) where id = @GuildId",
|
||
|
|
new { GuildId = guildId.Value, SystemIds = (string[])[hid, uuid.ToString()] }
|
||
|
|
);
|
||
|
|
|
||
|
|
public async Task UnbanSystemAsync(Snowflake guildId, string hid, Guid uuid) =>
|
||
|
|
await conn.ExecuteAsync(
|
||
|
|
"update guilds set banned_systems = array_remove(array_remove(banned_systems, @Hid), @Uuid) where id = @Id",
|
||
|
|
new
|
||
|
|
{
|
||
|
|
GuildId = guildId.Value,
|
||
|
|
Hid = hid,
|
||
|
|
Uuid = uuid.ToString(),
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
public void Dispose()
|
||
|
|
{
|
||
|
|
conn.Dispose();
|
||
|
|
GC.SuppressFinalize(this);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async ValueTask DisposeAsync()
|
||
|
|
{
|
||
|
|
await conn.DisposeAsync();
|
||
|
|
GC.SuppressFinalize(this);
|
||
|
|
}
|
||
|
|
}
|