/// <summary>
        /// Creates a new URL token record.
        /// </summary>
        /// <param name="record">The URL token record to create.</param>
        public virtual void CreateUrlToken(UrlTokenRecord record)
        {
            if (record == null)
            {
                throw new ArgumentNullException("record", "record must have a value.");
            }

            if (String.IsNullOrEmpty(record.Key))
            {
                throw new ArgumentException("record.Key must have a value.", "record");
            }

            lock (locker)
            {
                IDictionary<string, UrlTokenRecord> dict = Dictionary;
                dict[record.Key] = new UrlTokenRecord(record);
                SaveDictionary(dict);
            }
        }
        /// <summary>
        /// Creates a new URL token record.
        /// </summary>
        /// <param name="record">The URL token record to create.</param>
        public void CreateUrlToken(UrlTokenRecord record)
        {
            const string Sql = "INSERT INTO \"tasty_url_token\"(\"key\",\"type\",\"data\",\"created\",\"expires\") VALUES(:key,:type,:data,:created,:expires)";

            using (NpgsqlConnection connection = new NpgsqlConnection(this.ConnectionString))
            {
                connection.Open();

                using (NpgsqlCommand command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.Text;
                    command.CommandText = Sql;

                    command.Parameters.Add(new NpgsqlParameter(":key", record.Key));
                    command.Parameters.Add(new NpgsqlParameter(":type", record.StorageTypeName));
                    command.Parameters.Add(new NpgsqlParameter(":data", record.Data));
                    command.Parameters.Add(new NpgsqlParameter(":created", record.Created));
                    command.Parameters.Add(new NpgsqlParameter(":expires", record.Expires));

                    command.ExecuteNonQuery();
                }
            }
        }
        /// <summary>
        /// Creates a new URL token record.
        /// </summary>
        /// <param name="record">The URL token record to create.</param>
        public void CreateUrlToken(UrlTokenRecord record)
        {
            const string Sql = "INSERT INTO [TastyUrlToken]([Key],[Type],[Data],[Created],[Expires]) VALUES(@Key,@Type,@Data,@Created,@Expires);";

            using (SQLiteConnection connection = new SQLiteConnection(this.ConnectionString))
            {
                connection.Open();

                using (SQLiteCommand command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.Text;
                    command.CommandText = Sql;

                    command.Parameters.AddWithValue("@Key", record.Key);
                    command.Parameters.AddWithValue("@Type", record.StorageTypeName);
                    command.Parameters.AddWithValue("@Data", record.Data);
                    command.Parameters.AddWithValue("@Created", record.Created);
                    command.Parameters.AddWithValue("@Expires", record.Expires);

                    command.ExecuteNonQuery();
                }
            }
        }
Example #4
0
        /// <summary>
        /// Get URL token tests.
        /// </summary>
        protected virtual void GetUrlToken()
        {
            if (this.Store != null)
            {
                IUrlToken token1 = new TestIdUrlToken();
                IUrlToken token2 = new TestIdUrlToken();

                UrlTokenRecord record1 = new UrlTokenRecord()
                {
                    Created = DateTime.UtcNow,
                    Data = token1.Serialize(),
                    Expires = DateTime.UtcNow.AddHours(1),
                    Key = token1.GenerateKey(),
                    TokenType = token1.GetType()
                };

                UrlTokenRecord record2 = new UrlTokenRecord()
                {
                    Created = DateTime.UtcNow,
                    Data = token2.Serialize(),
                    Expires = DateTime.UtcNow.AddHours(1),
                    Key = token2.GenerateKey(),
                    TokenType = token2.GetType()
                };

                this.Store.CreateUrlToken(record1);
                this.Store.CreateUrlToken(record2);

                SqlDateTimeAssert.AreEqual(record1.Created, this.Store.GetUrlToken(record1.Key).Created);
                Assert.AreEqual(record1.Data, this.Store.GetUrlToken(record1.Key).Data);
                SqlDateTimeAssert.AreEqual(record1.Expires, this.Store.GetUrlToken(record1.Key).Expires);

                SqlDateTimeAssert.AreEqual(record2.Created, this.Store.GetUrlToken(record2.Key).Created);
                Assert.AreEqual(record2.Data, this.Store.GetUrlToken(record2.Key).Data);
                SqlDateTimeAssert.AreEqual(record2.Expires, this.Store.GetUrlToken(record2.Key).Expires);
            }
        }