Added API token functionality

This commit is contained in:
2024-12-21 16:09:55 +01:00
parent c087dbdf2b
commit ba46147a74
17 changed files with 118 additions and 55 deletions

View File

@@ -11,14 +11,14 @@ internal sealed class TokenRepository<TDbContext>(TDbContext context) : ITokenRe
return await context.Tokens
.Include(t => t.Owner)
.Where(t => t.Content == guid)
.Where(t => t.TokenId == guid)
.SingleOrDefaultAsync();
}
public async Task<Token> CreateToken(int type, User owner) {
var token = new Token {
CreatedAt = DateTime.Now,
Content = Guid.NewGuid(),
TokenId = Guid.NewGuid(),
Type = type,
Owner = owner
};
@@ -38,4 +38,18 @@ internal sealed class TokenRepository<TDbContext>(TDbContext context) : ITokenRe
context.Tokens.RemoveRange(tokens);
await context.SaveChangesAsync();
}
public async Task<Token> CreateApiToken(User owner, DateTime expirationDate) {
var token = new Token {
CreatedAt = expirationDate,
TokenId = Guid.NewGuid(),
Type = Token.ApiTokenType,
Owner = owner
};
await context.Tokens.AddAsync(token);
await context.SaveChangesAsync();
return token;
}
}