Beispiel #1
0
        /// <summary>
        /// The default action to render the front-end view
        /// </summary>
        /// <param name="model"/>
        /// <returns/>
        public async new Task <ActionResult> Index(RenderModel model)
        {
            var modelBuilder           = new JobsSearchViewModelFromUmbraco(model.Content, new JobAlertsViewModel());
            var viewModel              = (JobAlertsViewModel)modelBuilder.BuildModel();
            var lookupValuesDataSource = new JobsLookupValuesFromApi(new Uri(ConfigurationManager.AppSettings["JobsApiBaseUrl"]), viewModel.JobsSet, new MemoryJobCacheStrategy(MemoryCache.Default, Request.QueryString["ForceCacheRefresh"] == "1"));
            await modelBuilder.AddLookupValuesToModel(lookupValuesDataSource, viewModel);

            var converter = new JobSearchQueryConverter(ConfigurationManager.AppSettings["TranslateObsoleteJobTypes"]?.ToUpperInvariant() == "TRUE");
            var alertId   = new JobAlertIdEncoder(converter).ParseIdFromUrl(Request.Url);

            if (!string.IsNullOrEmpty(alertId))
            {
                if (ConfigurationManager.ConnectionStrings["Escc.EastSussexGovUK.Umbraco.AzureStorage"] == null || String.IsNullOrEmpty(ConfigurationManager.ConnectionStrings["Escc.EastSussexGovUK.Umbraco.AzureStorage"].ConnectionString))
                {
                    var error = new ConfigurationErrorsException("The Escc.EastSussexGovUK.Umbraco.AzureStorage connection string is missing from web.config");
                    LogHelper.Error <JobAlertsController>(error.Message, error);
                    error.ToExceptionless().Submit();
                }
                else
                {
                    var alertsRepo = new AzureTableStorageAlertsRepository(converter, ConfigurationManager.ConnectionStrings["Escc.EastSussexGovUK.Umbraco.AzureStorage"].ConnectionString);
                    viewModel.Alert = alertsRepo.GetAlertById(alertId);
                    viewModel.Query = viewModel.Alert?.Query;

                    if (viewModel.Alert == null && Request.QueryString["cancelled"] != "1" && string.IsNullOrEmpty(Request.QueryString["altTemplate"]))
                    {
                        // Returning HttpNotFoundResult() ends up with a generic browser 404,
                        // so to get our custom one we need to look it up and transfer control to it.
                        var notFoundUrl = new HttpStatusFromConfiguration().GetCustomUrlForStatusCode(404);
                        if (notFoundUrl != null && Server != null)
                        {
                            Server.TransferRequest(notFoundUrl + "?404;" + HttpUtility.UrlEncode(Request.Url.ToString()));
                        }
                    }
                }
            }

            var baseModelBuilder = new BaseViewModelBuilder(new EastSussexGovUKTemplateRequest(Request, webChatSettingsService: new UmbracoWebChatSettingsService(model.Content, new UrlListReader())));
            await baseModelBuilder.PopulateBaseViewModel(viewModel, model.Content, new ContentExperimentSettingsService(),
                                                         new ExpiryDateFromExamine(model.Content.Id, ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"], new ExpiryDateMemoryCache(TimeSpan.FromHours(1))).ExpiryDate,
                                                         UmbracoContext.Current.InPreviewMode);

            baseModelBuilder.PopulateBaseViewModelWithInheritedContent(viewModel,
                                                                       new UmbracoLatestService(model.Content),
                                                                       new UmbracoSocialMediaService(model.Content),
                                                                       null, null);

            return(CurrentTemplate(viewModel));
        }
        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));
            }
        }