api.auth/src/Robware.Data/API/ApiKeyRepository.cs

43 lines
No EOL
1.1 KiB
C#

using System;
using System.Threading.Tasks;
using System.Linq;
using Dapper;
using Robware.Auth.API;
using Robware.Data.Users;
namespace Robware.Data.API {
public class ApiKeyRepository : IApiKeys {
private readonly IDatabaseProvider _dbProvider;
public ApiKeyRepository(IDatabaseProvider dbProvider) {
_dbProvider = dbProvider;
}
public async Task<ApiKey> Get(string key) {
const string query = "SELECT * FROM api_keys WHERE api_key=@key";
using (var connection = _dbProvider.NewConnection()) {
connection.Open();
var result = await connection.QueryAsync<ApiKeyState>(query, new {key});
if (!result.Any())
throw new ApiKeyNotFoundException(key);
var dbKey = result.Single();
return new ApiKey {
Key = dbKey.Api_Key,
Enabled = dbKey.Enabled,
IssueTimestamp = dbKey.IssueTimestamp,
Name = dbKey.Name
};
}
}
}
public class ApiKeyState {
public string Name { get; set; }
public DateTime IssueTimestamp { get; set; }
public string Api_Key { get; set; }
public bool Enabled { get; set; }
}
}