feat: watchlist repository, remove ef core from all bot code

This commit is contained in:
sam 2024-10-28 02:14:41 +01:00
parent da4dfae27c
commit f0511a560c
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
19 changed files with 155 additions and 97 deletions

View file

@ -43,7 +43,7 @@ public class DatabaseConnection(Guid id, ILogger logger, NpgsqlConnection inner)
DatabasePool.DecrementConnections();
var openFor = DateTimeOffset.UtcNow - _openTime;
_logger.Debug("Closing connection {ConnId}, open for {OpenFor}", ConnectionId, openFor);
_logger.Verbose("Closing connection {ConnId}, open for {OpenFor}", ConnectionId, openFor);
_hasClosed = true;
await inner.CloseAsync();
}
@ -53,7 +53,7 @@ public class DatabaseConnection(Guid id, ILogger logger, NpgsqlConnection inner)
CancellationToken cancellationToken
)
{
_logger.Debug("Beginning transaction on connection {ConnId}", ConnectionId);
_logger.Verbose("Beginning transaction on connection {ConnId}", ConnectionId);
return await inner.BeginTransactionAsync(isolationLevel, cancellationToken);
}

View file

@ -67,7 +67,7 @@ public class DatabasePool
private Guid LogOpen()
{
var connId = Guid.NewGuid();
_logger.Debug("Opening database connection {ConnId}", connId);
_logger.Verbose("Opening database connection {ConnId}", connId);
IncrementConnections();
return connId;
}

View file

@ -75,6 +75,24 @@ public class GuildRepository(ILogger logger, DatabaseConnection conn)
}
);
public async Task AddKeyRoleAsync(Snowflake guildId, Snowflake roleId) =>
await conn.ExecuteAsync(
"update guilds set key_roles = array_append(key_roles, @RoleId) where id = @GuildId",
new { GuildId = guildId.Value, RoleId = roleId.Value }
);
public async Task RemoveKeyRoleAsync(Snowflake guildId, Snowflake roleId) =>
await conn.ExecuteAsync(
"update guilds set key_roles = array_remove(key_roles, @RoleId) where id = @GuildId",
new { GuildId = guildId.Value, RoleId = roleId.Value }
);
public async Task UpdateChannelConfigAsync(Snowflake id, Guild.ChannelConfig config) =>
await conn.ExecuteAsync(
"update guilds set channels = @Channels where id = @Id",
new { Id = id, Channels = config }
);
public void Dispose()
{
conn.Dispose();

View file

@ -0,0 +1,53 @@
// 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 WatchlistRepository(ILogger logger, DatabaseConnection conn)
: IDisposable,
IAsyncDisposable
{
private readonly ILogger _logger = logger.ForContext<WatchlistRepository>();
public async Task<List<Watchlist>> GetGuildWatchlistAsync(Snowflake guildId) =>
(
await conn.QueryAsync<Watchlist>(
"select * from watchlists where guild_id = @GuildId",
new { GuildId = guildId.Value }
)
).ToList();
public async Task<Watchlist?> GetWatchlistEntryAsync(Snowflake guildId, Snowflake userId) =>
await conn.QueryFirstOrDefaultAsync<Watchlist>(
"select * from watchlists where guild_id = @GuildId and user_id = @UserId",
new { GuildId = guildId.Value, UserId = userId.Value }
);
public void Dispose()
{
conn.Dispose();
GC.SuppressFinalize(this);
}
public async ValueTask DisposeAsync()
{
await conn.DisposeAsync();
GC.SuppressFinalize(this);
}
}

View file

@ -53,14 +53,4 @@ public static class QueryExtensions
throw new CataloggerError("Guild not found, was not initialized during guild create");
return guild;
}
public static async Task<Watchlist?> GetWatchlistEntryAsync(
this DatabaseContext db,
Snowflake guildId,
Snowflake userId,
CancellationToken ct = default
) =>
await db
.Watchlists.AsNoTracking()
.FirstOrDefaultAsync(w => w.GuildId == guildId.Value && w.UserId == userId.Value, ct);
}