excise entity framework from all remaining code
This commit is contained in:
parent
d6c3133d52
commit
ff92c5f335
62 changed files with 402 additions and 1339 deletions
|
|
@ -15,8 +15,8 @@
|
|||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json;
|
||||
using Catalogger.Backend.Database;
|
||||
using Catalogger.Backend.Database.Models;
|
||||
using Catalogger.Backend.Database.Repositories;
|
||||
using NodaTime;
|
||||
|
||||
namespace Catalogger.Backend.Api;
|
||||
|
|
@ -28,7 +28,7 @@ public class DiscordRequestService
|
|||
private readonly ApiCache _apiCache;
|
||||
private readonly Config _config;
|
||||
private readonly IClock _clock;
|
||||
private readonly DatabaseContext _db;
|
||||
private readonly ApiTokenRepository _tokenRepository;
|
||||
|
||||
private static readonly JsonSerializerOptions JsonOptions =
|
||||
new() { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower };
|
||||
|
|
@ -38,14 +38,14 @@ public class DiscordRequestService
|
|||
ApiCache apiCache,
|
||||
Config config,
|
||||
IClock clock,
|
||||
DatabaseContext db
|
||||
ApiTokenRepository tokenRepository
|
||||
)
|
||||
{
|
||||
_logger = logger.ForContext<DiscordRequestService>();
|
||||
_apiCache = apiCache;
|
||||
_config = config;
|
||||
_clock = clock;
|
||||
_db = db;
|
||||
_tokenRepository = tokenRepository;
|
||||
|
||||
_httpClient = new HttpClient();
|
||||
_httpClient.DefaultRequestHeaders.Add(
|
||||
|
|
@ -154,16 +154,13 @@ public class DiscordRequestService
|
|||
var meUser = await GetMeAsync($"Bearer {token.AccessToken}");
|
||||
var meGuilds = await GetGuildsAsync($"Bearer {token.AccessToken}");
|
||||
|
||||
var apiToken = new ApiToken
|
||||
{
|
||||
DashboardToken = ApiUtils.RandomToken(64),
|
||||
UserId = meUser.Id,
|
||||
AccessToken = token.AccessToken,
|
||||
RefreshToken = token.RefreshToken,
|
||||
ExpiresAt = _clock.GetCurrentInstant() + Duration.FromSeconds(token.ExpiresIn),
|
||||
};
|
||||
_db.Add(apiToken);
|
||||
await _db.SaveChangesAsync(ct);
|
||||
var apiToken = await _tokenRepository.CreateAsync(
|
||||
ApiUtils.RandomToken(64),
|
||||
meUser.Id,
|
||||
token.AccessToken,
|
||||
token.RefreshToken,
|
||||
token.ExpiresIn
|
||||
);
|
||||
|
||||
return (apiToken, meUser, meGuilds);
|
||||
}
|
||||
|
|
@ -231,8 +228,12 @@ public class DiscordRequestService
|
|||
token.RefreshToken = discordToken.RefreshToken;
|
||||
token.ExpiresAt = _clock.GetCurrentInstant() + Duration.FromSeconds(discordToken.ExpiresIn);
|
||||
|
||||
_db.Update(token);
|
||||
await _db.SaveChangesAsync();
|
||||
await _tokenRepository.UpdateAsync(
|
||||
token.Id,
|
||||
discordToken.AccessToken,
|
||||
discordToken.RefreshToken,
|
||||
discordToken.ExpiresIn
|
||||
);
|
||||
}
|
||||
|
||||
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Local")]
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@
|
|||
// 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.Queries;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Remora.Discord.API;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
|
|
@ -26,7 +25,7 @@ public partial class GuildsController
|
|||
public async Task<IActionResult> AddIgnoredChannelAsync(string id, ulong channelId)
|
||||
{
|
||||
var (guildId, _) = await ParseGuildAsync(id);
|
||||
var guildConfig = await db.GetGuildAsync(guildId);
|
||||
var guildConfig = await guildRepository.GetAsync(guildId);
|
||||
|
||||
if (guildConfig.Channels.IgnoredChannels.Contains(channelId))
|
||||
return NoContent();
|
||||
|
|
@ -47,8 +46,7 @@ public partial class GuildsController
|
|||
return NoContent();
|
||||
|
||||
guildConfig.Channels.IgnoredChannels.Add(channelId);
|
||||
db.Update(guildConfig);
|
||||
await db.SaveChangesAsync();
|
||||
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
|
@ -57,11 +55,10 @@ public partial class GuildsController
|
|||
public async Task<IActionResult> RemoveIgnoredChannelAsync(string id, ulong channelId)
|
||||
{
|
||||
var (guildId, _) = await ParseGuildAsync(id);
|
||||
var guildConfig = await db.GetGuildAsync(guildId);
|
||||
var guildConfig = await guildRepository.GetAsync(guildId);
|
||||
|
||||
guildConfig.Channels.IgnoredChannels.Remove(channelId);
|
||||
db.Update(guildConfig);
|
||||
await db.SaveChangesAsync();
|
||||
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
|
@ -81,7 +78,7 @@ public partial class GuildsController
|
|||
public async Task<IActionResult> AddIgnoredUserAsync(string id, ulong userId)
|
||||
{
|
||||
var (guildId, _) = await ParseGuildAsync(id);
|
||||
var guildConfig = await db.GetGuildAsync(guildId);
|
||||
var guildConfig = await guildRepository.GetAsync(guildId);
|
||||
|
||||
if (guildConfig.Channels.IgnoredUsers.Contains(userId))
|
||||
return NoContent();
|
||||
|
|
@ -91,8 +88,7 @@ public partial class GuildsController
|
|||
return NoContent();
|
||||
|
||||
guildConfig.Channels.IgnoredUsers.Add(userId);
|
||||
db.Update(guildConfig);
|
||||
await db.SaveChangesAsync();
|
||||
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
|
@ -101,11 +97,10 @@ public partial class GuildsController
|
|||
public async Task<IActionResult> RemoveIgnoredUserAsync(string id, ulong userId)
|
||||
{
|
||||
var (guildId, _) = await ParseGuildAsync(id);
|
||||
var guildConfig = await db.GetGuildAsync(guildId);
|
||||
var guildConfig = await guildRepository.GetAsync(guildId);
|
||||
|
||||
guildConfig.Channels.IgnoredUsers.Remove(userId);
|
||||
db.Update(guildConfig);
|
||||
await db.SaveChangesAsync();
|
||||
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@
|
|||
|
||||
using System.Net;
|
||||
using Catalogger.Backend.Api.Middleware;
|
||||
using Catalogger.Backend.Database.Queries;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
|
||||
|
|
@ -31,7 +30,7 @@ public partial class GuildsController
|
|||
{
|
||||
var (guildId, _) = await ParseGuildAsync(id);
|
||||
var guildChannels = channelCache.GuildChannels(guildId).ToList();
|
||||
var guildConfig = await db.GetGuildAsync(guildId.Value);
|
||||
var guildConfig = await guildRepository.GetAsync(guildId);
|
||||
|
||||
Console.WriteLine($"Source: {req.Source}, target: {req.Target}");
|
||||
|
||||
|
|
@ -62,8 +61,7 @@ public partial class GuildsController
|
|||
);
|
||||
|
||||
guildConfig.Channels.Redirects[source.ID.Value] = target.ID.Value;
|
||||
db.Update(guildConfig);
|
||||
await db.SaveChangesAsync();
|
||||
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
|
@ -72,7 +70,7 @@ public partial class GuildsController
|
|||
public async Task<IActionResult> DeleteRedirectAsync(string id, ulong channelId)
|
||||
{
|
||||
var (guildId, _) = await ParseGuildAsync(id);
|
||||
var guildConfig = await db.GetGuildAsync(guildId.Value);
|
||||
var guildConfig = await guildRepository.GetAsync(guildId);
|
||||
|
||||
if (!guildConfig.Channels.Redirects.ContainsKey(channelId))
|
||||
throw new ApiError(
|
||||
|
|
@ -82,8 +80,7 @@ public partial class GuildsController
|
|||
);
|
||||
|
||||
guildConfig.Channels.Redirects.Remove(channelId, out _);
|
||||
db.Update(guildConfig);
|
||||
await db.SaveChangesAsync();
|
||||
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,12 +16,10 @@
|
|||
using System.Net;
|
||||
using Catalogger.Backend.Api.Middleware;
|
||||
using Catalogger.Backend.Bot;
|
||||
using Catalogger.Backend.Database.Queries;
|
||||
using Catalogger.Backend.Extensions;
|
||||
using Catalogger.Backend.Services;
|
||||
using Dapper;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Remora.Discord.Extensions.Embeds;
|
||||
|
||||
namespace Catalogger.Backend.Api;
|
||||
|
|
|
|||
|
|
@ -18,8 +18,7 @@ using Catalogger.Backend.Api.Middleware;
|
|||
using Catalogger.Backend.Cache;
|
||||
using Catalogger.Backend.Cache.InMemoryCache;
|
||||
using Catalogger.Backend.Database;
|
||||
using Catalogger.Backend.Database.Dapper;
|
||||
using Catalogger.Backend.Database.Dapper.Repositories;
|
||||
using Catalogger.Backend.Database.Repositories;
|
||||
using Catalogger.Backend.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Remora.Discord.API;
|
||||
|
|
@ -32,7 +31,6 @@ namespace Catalogger.Backend.Api;
|
|||
[Route("/api/guilds/{id}")]
|
||||
public partial class GuildsController(
|
||||
ILogger logger,
|
||||
DatabaseContext db,
|
||||
DatabaseConnection dbConn,
|
||||
GuildRepository guildRepository,
|
||||
GuildCache guildCache,
|
||||
|
|
|
|||
|
|
@ -15,10 +15,8 @@
|
|||
|
||||
using Catalogger.Backend.Api.Middleware;
|
||||
using Catalogger.Backend.Cache.InMemoryCache;
|
||||
using Catalogger.Backend.Extensions;
|
||||
using Catalogger.Backend.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
|
||||
namespace Catalogger.Backend.Api;
|
||||
|
||||
|
|
|
|||
|
|
@ -14,14 +14,14 @@
|
|||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
using System.Net;
|
||||
using Catalogger.Backend.Database;
|
||||
using Catalogger.Backend.Database.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Catalogger.Backend.Database.Repositories;
|
||||
using NodaTime;
|
||||
|
||||
namespace Catalogger.Backend.Api.Middleware;
|
||||
|
||||
public class AuthenticationMiddleware(DatabaseContext db, IClock clock) : IMiddleware
|
||||
public class AuthenticationMiddleware(ApiTokenRepository tokenRepository, IClock clock)
|
||||
: IMiddleware
|
||||
{
|
||||
public async Task InvokeAsync(HttpContext ctx, RequestDelegate next)
|
||||
{
|
||||
|
|
@ -37,9 +37,7 @@ public class AuthenticationMiddleware(DatabaseContext db, IClock clock) : IMiddl
|
|||
|
||||
var token = ctx.Request.Headers.Authorization.ToString();
|
||||
|
||||
var apiToken = await db.ApiTokens.FirstOrDefaultAsync(t =>
|
||||
t.DashboardToken == token && t.ExpiresAt > clock.GetCurrentInstant()
|
||||
);
|
||||
var apiToken = await tokenRepository.GetAsync(token);
|
||||
if (apiToken == null)
|
||||
{
|
||||
if (requireAuth)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue