Exemple #1
0
        /// <summary>
        /// Saves a new or updated alert.
        /// </summary>
        /// <param name="alert">The alert.</param>
        /// <exception cref="ArgumentNullException">alert</exception>
        /// <exception cref="ArgumentException">The alert must have an AlertId - alert</exception>
        public void SaveAlert(JobAlert alert)
        {
            if (alert == null)
            {
                throw new ArgumentNullException(nameof(alert));
            }
            if (String.IsNullOrEmpty(alert.AlertId))
            {
                throw new ArgumentException("The alert must have an AlertId", nameof(alert));
            }

            var table = _tableClient.GetTableReference(_alertsTable);

            table.CreateIfNotExistsAsync().Wait();

            var query = _queryConverter.ToCollection(alert.Query);

            query.Remove("page");
            query.Remove("pagesize");
            query.Remove("sort");
            var serialised = query.ToString();

            // Azure tables use an index clustered first by partition key then by row key,
            // so use email as the partition key to make it easy to get all alerts for a user.
            var entity = new JobAlertTableEntity()
            {
                PartitionKey = ToAzureKeyString(alert.Email),
                RowKey       = alert.AlertId,
                Criteria     = serialised,
                Frequency    = alert.Frequency,
                JobsSet      = alert.JobsSet.ToString()
            };

            try
            {
                table.Execute(TableOperation.InsertOrReplace(entity));
            }
            catch (StorageException ex)
            {
                if (ex.Message.Contains("(400) Bad Request"))
                {
                    var alertQuery = _queryConverter.ToCollection(alert.Query).ToString();
                    LogHelper.Error <AzureTableStorageAlertsRepository>(alertQuery + " returned " + ex.RequestInformation.ExtendedErrorInformation.ErrorMessage, ex);
                    ex.ToExceptionless().Submit();
                }
                else
                {
                    throw;
                }
            }
        }
        /// <summary>
        /// Generates a new unique identifier based on the content of a job alert.
        /// </summary>
        /// <param name="alert">The alert.</param>
        /// <returns></returns>
        public string GenerateId(JobAlert alert)
        {
            if (alert == null)
            {
                throw new ArgumentNullException(nameof(alert));
            }
            if (String.IsNullOrEmpty(alert.Email))
            {
                throw new ArgumentException("The alert must include an email address to generate a unique ID", nameof(alert));
            }

            HashAlgorithm algorithm = SHA1.Create();
            var           bytes     = Encoding.ASCII.GetBytes(alert.JobsSet + alert.Email + _queryConverter.ToCollection(alert.Query).ToString());
            var           hash      = algorithm.ComputeHash(bytes);
            StringBuilder sb        = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return(sb.ToString().ToLowerInvariant());
        }