/// <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; } } }
private JobAlert BuildAlertFromEntity(JobAlertTableEntity entity) { JobAlert alert = null; if (entity != null) { var searchQuery = HttpUtility.ParseQueryString(String.IsNullOrEmpty(entity.Criteria) ? String.Empty : entity.Criteria); alert = new JobAlert() { AlertId = entity.RowKey, Query = _queryConverter.ToQuery(searchQuery), Email = entity.PartitionKey, Frequency = entity.Frequency, JobsSet = (JobsSet)Enum.Parse(typeof(JobsSet), entity.JobsSet) }; alert.Query.JobsSet = alert.JobsSet; } return(alert); }
/// <summary> /// Gets a single alert by its identifier. /// </summary> /// <param name="alertId">The alert identifier.</param> /// <returns></returns> public JobAlert GetAlertById(string alertId) { JobAlertTableEntity entity = ReadAlertEntity(alertId); return(BuildAlertFromEntity(entity)); }