/// <summary>Increments the redirect count for the specified key, for use when the key was found in cache</summary> /// <param name="key">The key for which to update the redirect count</param> /// <returns>An async task</returns> public async Task IncrementRedirectCount(string key) { if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentException("Invalid (null or whitespace) key"); } long urlShorteningId = UrlUtility.ConvertKeyToId(key); using (var connection = new NpgsqlConnection(_settings.RepositoryConnectionString)) { await connection.OpenAsync(); using (var command = connection.CreateCommand()) { command.CommandType = CommandType.Text; command.CommandText = $@"SELECT urls.""IncrementRedirectCount""(@urlShorteningId);"; command.Parameters.Add(new NpgsqlParameter("urlShorteningId", NpgsqlDbType.Bigint) { Value = urlShorteningId }); await command.ExecuteNonQueryAsync(); } } }
/// <summary>Gets the URL for the specified key, and increments its redirect count</summary> /// <param name="key">The key for which to get the URL</param> /// <returns>The URL for the key, or null if not found</returns> public async Task <string> GetRedirectUrl(string key) { if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentException("Invalid (null or whitespace) key"); } long urlShorteningId = UrlUtility.ConvertKeyToId(key); object returnValue = null; using (var connection = new NpgsqlConnection(_settings.RepositoryConnectionString)) { await connection.OpenAsync(); using (var command = connection.CreateCommand()) { command.CommandType = CommandType.Text; command.CommandText = $@"SELECT urls.""GetRedirectUrl""(@urlShorteningId);"; command.Parameters.Add(new NpgsqlParameter("urlShorteningId", NpgsqlDbType.Bigint) { Value = urlShorteningId }); returnValue = await command.ExecuteScalarAsync(); } } if (returnValue == DBNull.Value) { return(null); } else { return((string)returnValue); } }