示例#1
0
        /// <summary>Creates a new API key.</summary>
        /// <param name="apiKey">The api key.</param>
        /// <returns>The created API key.</returns>
        public async Task <IUserApiKey> Create(IUserApiKey apiKey)
        {
            using (var connection = await this.OpenConnection())
            {
                var k = new SqlUserApiKey(apiKey);

                using (var transaction = connection.BeginTransaction())
                {
                    try
                    {
                        k.Created = DateTimeOffset.UtcNow;

                        var id = connection.ExecuteScalar <long>(
                            "INSERT INTO UserApiKeys (UserId, ApiKey, Name, Description, Created) VALUES (@UserId, @ApiKey, @Name, @Description, @Created); SELECT SCOPE_IDENTITY()",
                            new { k.UserId, k.ApiKey, k.Name, k.Description, Created = k.Created.ToString("s") },
                            transaction);

                        transaction.Commit();

                        var result = await connection.QueryAsync <SqlUserApiKey>("SELECT * FROM UserApiKeys WHERE Id = @Id", new { Id = id });

                        return(result.FirstOrDefault());
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
示例#2
0
        /// <summary>Updates the specified api key.</summary>
        /// <typeparam name="T">The primary key type.</typeparam>
        /// <param name="id">The api key identifier.</param>
        /// <param name="apiKey">The api key.</param>
        /// <returns>The updated key.</returns>
        public virtual async Task <IUserApiKey> Update <T>(T id, IUserApiKey apiKey)
        {
            using (var connection = await this.OpenConnection())
            {
                var k = (SqlUserApiKey)apiKey;

                using (var transaction = connection.BeginTransaction())
                {
                    try
                    {
                        connection.Execute(
                            "UPDATE UserApiKeys SET UserId = @UserId, ApiKey = @ApiKey, Name = @Name, Description = @Description, LastUsed = @LastUsed, Created = @Created WHERE Id = @Id",
                            new { Id = id, k.UserId, k.ApiKey, k.Name, k.Description, k.LastUsed, k.Created },
                            transaction);

                        transaction.Commit();

                        var result = await connection.QueryAsync <SqlUserApiKey>("SELECT * FROM UserApiKeys WHERE Id = @Id", new { Id = id });

                        return(result.FirstOrDefault());
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
示例#3
0
 /// <summary>Initializes a new instance of the <see cref="UserApiKey" /> class.</summary>
 /// <param name="userApiKey">The user api key.</param>
 public UserApiKey(IUserApiKey userApiKey)
 {
     this.UserId      = userApiKey.UserId;
     this.ApiKey      = userApiKey.ApiKey;
     this.Name        = userApiKey.Name;
     this.Description = userApiKey.Description;
     this.LastUsed    = userApiKey.LastUsed;
 }
示例#4
0
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise,
        /// false.
        /// </returns>
        public virtual bool Equals(IUserApiKey other)
        {
            if (this.UserId == other.UserId && this.Name == other.Name)
            {
                return(true);
            }

            return(false);
        }
示例#5
0
        /// <summary>Initializes a new instance of the <see cref="SqlUserApiKey" /> class.</summary>
        /// <param name="userApiKey">The user api key.</param>
        public SqlUserApiKey(IUserApiKey userApiKey)
            : base(userApiKey)
        {
            if (userApiKey is SqlUserApiKey)
            {
                this.Id = ((SqlUserApiKey)userApiKey).Id;
            }

            this.Created = DateTimeOffset.UtcNow;
        }