excise entity framework from all remaining code

This commit is contained in:
sam 2024-10-28 14:04:55 +01:00
parent d6c3133d52
commit ff92c5f335
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
62 changed files with 402 additions and 1339 deletions

View file

@ -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")]