public async Task <SegmentModel> RefreshSegmentAsync(RefreshJobProfileSegment refreshJobProfileSegmentModel)
        {
            if (refreshJobProfileSegmentModel is null)
            {
                throw new ArgumentNullException(nameof(refreshJobProfileSegmentModel));
            }

            switch (refreshJobProfileSegmentModel.Segment)
            {
            case JobProfileSegment.Overview:
                return(await GetSegmentDataAsync(overviewBannerSegmentService, refreshJobProfileSegmentModel).ConfigureAwait(false));

            case JobProfileSegment.HowToBecome:
                return(await GetSegmentDataAsync(howToBecomeSegmentService, refreshJobProfileSegmentModel).ConfigureAwait(false));

            case JobProfileSegment.WhatItTakes:
                return(await GetSegmentDataAsync(whatItTakesSegmentService, refreshJobProfileSegmentModel).ConfigureAwait(false));

            case JobProfileSegment.WhatYouWillDo:
                return(await GetSegmentDataAsync(whatYouWillDoSegmentService, refreshJobProfileSegmentModel).ConfigureAwait(false));

            case JobProfileSegment.CareerPathsAndProgression:
                return(await GetSegmentDataAsync(careerPathSegmentService, refreshJobProfileSegmentModel).ConfigureAwait(false));

            case JobProfileSegment.CurrentOpportunities:
                return(await GetSegmentDataAsync(currentOpportunitiesSegmentService, refreshJobProfileSegmentModel).ConfigureAwait(false));

            case JobProfileSegment.RelatedCareers:
                return(await GetSegmentDataAsync(relatedCareersSegmentService, refreshJobProfileSegmentModel).ConfigureAwait(false));

            default:
                throw new ArgumentOutOfRangeException(nameof(refreshJobProfileSegmentModel), $"Segment to be refreshed should be one of {Enum.GetNames(typeof(JobProfileSegment))}");
            }
        }
        public async Task ProfileControllerPostRefreshReturnsBadResultWhenModelIsNull(string mediaTypeName)
        {
            // Arrange
            RefreshJobProfileSegment refreshJobProfileSegmentModel = null;
            var controller = BuildProfileController(mediaTypeName);

            // Act
            var result = await controller.Refresh(refreshJobProfileSegmentModel).ConfigureAwait(false);

            // Assert
            var statusResult = Assert.IsType <BadRequestResult>(result);

            Assert.Equal((int)HttpStatusCode.BadRequest, statusResult.StatusCode);

            controller.Dispose();
        }
Exemple #3
0
        public async Task <HttpStatusCode> RefreshSegmentsAsync(RefreshJobProfileSegment refreshJobProfileSegmentModel)
        {
            if (refreshJobProfileSegmentModel is null)
            {
                throw new ArgumentNullException(nameof(refreshJobProfileSegmentModel));
            }

            //Check existing document
            var existingJobProfile = await GetByIdAsync(refreshJobProfileSegmentModel.JobProfileId).ConfigureAwait(false);

            if (existingJobProfile is null)
            {
                return(HttpStatusCode.NotFound);
            }

            var existingItem = existingJobProfile.Segments.SingleOrDefault(s => s.Segment == refreshJobProfileSegmentModel.Segment);

            if (existingItem?.RefreshSequence > refreshJobProfileSegmentModel.SequenceNumber)
            {
                return(HttpStatusCode.AlreadyReported);
            }

            var offlineSegmentData = segmentService.GetOfflineSegment(refreshJobProfileSegmentModel.Segment);
            var segmentData        = await segmentService.RefreshSegmentAsync(refreshJobProfileSegmentModel).ConfigureAwait(false);

            if (existingItem is null)
            {
                segmentData.Markup = !string.IsNullOrEmpty(segmentData.Markup?.Value) ? segmentData.Markup : offlineSegmentData.OfflineMarkup;
                segmentData.Json ??= offlineSegmentData.OfflineJson;
                existingJobProfile.Segments.Add(segmentData);
            }
            else
            {
                var index          = existingJobProfile.Segments.IndexOf(existingItem);
                var fallbackMarkup = !string.IsNullOrEmpty(existingItem.Markup?.Value) ? existingItem.Markup : offlineSegmentData.OfflineMarkup;
                segmentData.Markup = !string.IsNullOrEmpty(segmentData.Markup?.Value) ? segmentData.Markup : fallbackMarkup;
                segmentData.Json ??= existingItem.Json ?? offlineSegmentData.OfflineJson;

                existingJobProfile.Segments[index] = segmentData;
            }

            var result = await repository.UpsertAsync(existingJobProfile).ConfigureAwait(false);

            return(segmentData.RefreshStatus == Data.Enums.RefreshStatus.Success ? result : HttpStatusCode.FailedDependency);
        }
        public async Task ProfileControllerPostRefreshReturnsBadResultWhenModelIsInvalid(string mediaTypeName)
        {
            // Arrange
            var refreshJobProfileSegmentModel = new RefreshJobProfileSegment();
            var controller = BuildProfileController(mediaTypeName);

            controller.ModelState.AddModelError(string.Empty, "Model is not valid");

            // Act
            var result = await controller.Refresh(refreshJobProfileSegmentModel).ConfigureAwait(false);

            // Assert
            var statusResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.Equal((int)HttpStatusCode.BadRequest, statusResult.StatusCode);

            controller.Dispose();
        }
Exemple #5
0
        public async Task <IActionResult> Refresh([FromBody] RefreshJobProfileSegment refreshJobProfileSegmentModel)
        {
            logService.LogInformation($"{nameof(Refresh)} has been called with {refreshJobProfileSegmentModel?.JobProfileId} for {refreshJobProfileSegmentModel?.CanonicalName} with seq number {refreshJobProfileSegmentModel?.SequenceNumber}");

            if (refreshJobProfileSegmentModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var response = await jobProfileService.RefreshSegmentsAsync(refreshJobProfileSegmentModel).ConfigureAwait(false);

            logService.LogInformation($"{nameof(Refresh)} has upserted content for: {refreshJobProfileSegmentModel.CanonicalName} - Response - {response}");
            return(new StatusCodeResult((int)response));
        }
        private async Task <SegmentModel> GetSegmentDataAsync <T>(ISegmentRefreshService <T> segmentService, RefreshJobProfileSegment toRefresh)
            where T : SegmentClientOptions
        {
            var jsonResultTask = segmentService.GetJsonAsync(toRefresh.JobProfileId);
            var htmlResultTask = segmentService.GetMarkupAsync(toRefresh.JobProfileId);

            await Task.WhenAll(jsonResultTask, htmlResultTask).ConfigureAwait(false);

            return(new SegmentModel
            {
                Segment = toRefresh.Segment,
                RefreshedAt = DateTime.UtcNow,
                RefreshSequence = toRefresh.SequenceNumber,
                Markup = htmlResultTask?.IsCompletedSuccessfully == true && htmlResultTask.Result != null ? new HtmlString(htmlResultTask.Result) : HtmlString.Empty,
                Json = jsonResultTask?.IsCompletedSuccessfully == true ? jsonResultTask.Result : null,
                RefreshStatus = jsonResultTask?.IsCompletedSuccessfully == true &&
                                htmlResultTask?.IsCompletedSuccessfully == true
                    ? Data.Enums.RefreshStatus.Success
                    : Data.Enums.RefreshStatus.Failed,
            });
        }