public async Task <ActionResult> CancelAlert()
        {
            var converter   = new JobSearchQueryConverter(ConfigurationManager.AppSettings["TranslateObsoleteJobTypes"]?.ToUpperInvariant() == "TRUE");
            var encoder     = new JobAlertIdEncoder(converter);
            var absoluteUrl = new Uri(Request.Url, Request.RawUrl);
            var alertId     = encoder.ParseIdFromUrl(absoluteUrl);
            var alertsRepo  = new AzureTableStorageAlertsRepository(converter, ConfigurationManager.ConnectionStrings["Escc.EastSussexGovUK.Umbraco.AzureStorage"].ConnectionString);
            var success     = await alertsRepo.CancelAlert(alertId);

            return(new RedirectResult(absoluteUrl.AbsolutePath + "?cancelled=" + (success ? "1" : "0")));
        }
        public async Task <ActionResult> ReplaceAlert(JobSearchQuery searchQuery)
        {
            if (ModelState.IsValid)
            {
                var converter = new JobSearchQueryConverter(ConfigurationManager.AppSettings["TranslateObsoleteJobTypes"]?.ToUpperInvariant() == "TRUE");
                var encoder   = new JobAlertIdEncoder(converter);
                var alertId   = encoder.ParseIdFromUrl(new Uri(Request.Url, Request.RawUrl));
                var repo      = new AzureTableStorageAlertsRepository(converter, ConfigurationManager.ConnectionStrings["Escc.EastSussexGovUK.Umbraco.AzureStorage"].ConnectionString);
                var oldAlert  = repo.GetAlertById(alertId);

                var newAlert = new JobAlert()
                {
                    Query     = searchQuery,
                    Email     = oldAlert.Email,
                    Frequency = searchQuery.Frequency,
                    JobsSet   = searchQuery.JobsSet
                };
                newAlert.AlertId = encoder.GenerateId(newAlert);

                if (oldAlert.AlertId == newAlert.AlertId)
                {
                    // The alert id didn't change but the frequency may have, so update the existing alert
                    await repo.SaveAlert(newAlert);
                }
                else
                {
                    // The alert id, and therefore the criteria, changed, so save the new alert and delete the old one
                    await repo.SaveAlert(newAlert);

                    await repo.CancelAlert(oldAlert.AlertId);
                }

                var urlWithoutQueryString = new Uri(Request.Url, new Uri(Request.Url, Request.RawUrl).AbsolutePath);
                var urlWithoutAlertId     = encoder.RemoveIdFromUrl(urlWithoutQueryString);
                var urlWithAlertId        = encoder.AddIdToUrl(urlWithoutAlertId, newAlert.AlertId);

                return(new RedirectResult(urlWithAlertId + "?updated=1"));
            }
            else
            {
                return(new RedirectResult(Request.RawUrl));
            }
        }