public ActionResult CreateAlert(JobAlert alert)
        {
            var query = HttpUtility.ParseQueryString(Request.Url.Query);

            if (ModelState.IsValid)
            {
                var converter  = new JobSearchQueryConverter(ConfigurationManager.AppSettings["TranslateObsoleteJobTypes"]?.ToUpperInvariant() == "TRUE");
                var encoder    = new JobAlertIdEncoder(converter);
                var alertsRepo = new AzureTableStorageAlertsRepository(converter, ConfigurationManager.ConnectionStrings["Escc.EastSussexGovUK.Umbraco.AzureStorage"].ConnectionString);
                alert.Query   = converter.ToQuery(query);
                alert.AlertId = encoder.GenerateId(alert);
                alertsRepo.SaveAlert(alert);

                var jobAlertsSettings = new JobAlertsSettingsFromUmbraco(Umbraco).GetJobAlertsSettings(alert.JobsSet);
                if (jobAlertsSettings != null && !String.IsNullOrEmpty(jobAlertsSettings.NewAlertEmailSubject))
                {
                    var emailService = ServiceContainer.LoadService <IEmailSender>(new ConfigurationServiceRegistry(), new HttpContextCacheStrategy());
                    var sender       = new JobAlertsByEmailSender(jobAlertsSettings, new HtmlJobAlertFormatter(jobAlertsSettings, encoder), emailService);

                    sender.SendNewAlertConfirmation(alert);
                }

                query.Add("subscribed", "1");
            }
            return(new RedirectToUmbracoPageResult(CurrentPage, query));
        }
        public void GeneratedIdIsConsistent()
        {
            var converter = new FakeSearchQueryConverter();
            var encoder   = new JobAlertIdEncoder(converter);
            var alert     = new JobAlert()
            {
                Email = "*****@*****.**", Query = new JobSearchQuery()
                {
                    Keywords = "test"
                }
            };

            var id1 = encoder.GenerateId(alert);
            var id2 = encoder.GenerateId(alert);

            Assert.AreEqual(id1, id2);
        }
        public void GeneratedIdVariesByCriteria()
        {
            var converter = new FakeSearchQueryConverter();
            var encoder   = new JobAlertIdEncoder(converter);
            var alert1    = new JobAlert()
            {
                Email = "*****@*****.**", Query = new JobSearchQuery()
                {
                    Keywords = "test1"
                }
            };
            var alert2 = new JobAlert()
            {
                Email = "*****@*****.**", Query = new JobSearchQuery()
                {
                    Keywords = "test2"
                }
            };

            var id1 = encoder.GenerateId(alert1);
            var id2 = encoder.GenerateId(alert2);

            Assert.AreNotEqual(id1, id2);
        }
        public void IdIsAddedToTheUrl()
        {
            var url       = new Uri("https://www.example.org");
            var converter = new FakeSearchQueryConverter();
            var encoder   = new JobAlertIdEncoder(converter);
            var id        = encoder.GenerateId(new JobAlert()
            {
                Email = "*****@*****.**", Query = new JobSearchQuery()
                {
                    Keywords = "test"
                }
            });

            var after = encoder.AddIdToUrl(url, id);

            Assert.IsTrue(after.ToString().Contains(id));
        }
        public void IdIsRemovedFromTheUrl()
        {
            var url       = new Uri("https://www.example.org");
            var converter = new FakeSearchQueryConverter();
            var encoder   = new JobAlertIdEncoder(converter);
            var id        = encoder.GenerateId(new JobAlert()
            {
                Email = "*****@*****.**", Query = new JobSearchQuery()
                {
                    Keywords = "test"
                }
            });
            var urlWithId = encoder.AddIdToUrl(url, id);

            var urlWithoutId = encoder.RemoveIdFromUrl(urlWithId);

            Assert.AreEqual(url, urlWithoutId);
        }
        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));
            }
        }