Ejemplo n.º 1
0
        public async Task EventMessageServiceUpdateAsyncDoesNothingForNoPageLocation()
        {
            // arrange
            var existingContentPageModel = A.Fake <ContentPageModel>();
            var contentPageModel         = A.Fake <ContentPageModel>();
            var expectedResult           = HttpStatusCode.OK;

            existingContentPageModel.Version      = Guid.NewGuid();
            contentPageModel.Version              = Guid.NewGuid();
            contentPageModel.PartitionKey         = "a-partition-key";
            existingContentPageModel.PartitionKey = contentPageModel.PartitionKey;

            A.CallTo(() => fakeContentPageService.GetByIdAsync(A <Guid> .Ignored, A <string> .Ignored)).Returns(existingContentPageModel);
            A.CallTo(() => fakeContentPageService.UpsertAsync(A <ContentPageModel> .Ignored)).Returns(expectedResult);

            var eventMessageService = new EventMessageService <ContentPageModel>(fakeLogger, fakeContentPageService);

            // act
            var result = await eventMessageService.UpdateAsync(contentPageModel).ConfigureAwait(false);

            // assert
            A.CallTo(() => fakeContentPageService.GetByIdAsync(A <Guid> .Ignored, A <string> .Ignored)).MustHaveHappenedOnceExactly();
            A.CallTo(() => fakeContentPageService.DeleteAsync(A <Guid> .Ignored)).MustNotHaveHappened();
            A.CallTo(() => fakeContentPageService.UpsertAsync(A <ContentPageModel> .Ignored)).MustNotHaveHappened();
            A.Equals(result, expectedResult);
        }
Ejemplo n.º 2
0
        public async Task <HttpStatusCode> UpdateAsync(TModel?upsertDocumentModel)
        {
            if (upsertDocumentModel == null)
            {
                return(HttpStatusCode.BadRequest);
            }

            var existingDocument = await contentPageService.GetByIdAsync(upsertDocumentModel.Id).ConfigureAwait(false);

            if (existingDocument == null)
            {
                return(HttpStatusCode.NotFound);
            }

            var beforePageLocationUpdate = JsonConvert.SerializeObject(upsertDocumentModel);

            upsertDocumentModel.PageLocation = ExtractPageLocation(upsertDocumentModel);

            if (string.IsNullOrEmpty(upsertDocumentModel.PageLocation) || string.IsNullOrEmpty(upsertDocumentModel.PartitionKey))
            {
                logger.LogError(
                    "PageLocation ({PageLocation}) and/or PartitionKey ({PartitionKey}) is empty or null. Document before = {SerialisedDocumentBefore}. Document = {SerialisedDocument}",
                    upsertDocumentModel.PageLocation,
                    upsertDocumentModel.PartitionKey,
                    beforePageLocationUpdate,
                    JsonConvert.SerializeObject(upsertDocumentModel));

                return(HttpStatusCode.BadRequest);
            }

            if (existingDocument.PartitionKey?.Equals(upsertDocumentModel.PartitionKey, StringComparison.OrdinalIgnoreCase) == true)
            {
                upsertDocumentModel.Etag = existingDocument.Etag;
            }
            else
            {
                var deleted = await contentPageService.DeleteAsync(existingDocument.Id).ConfigureAwait(false);

                if (deleted)
                {
                    logger.LogInformation($"{nameof(UpdateAsync)} has deleted content for: {existingDocument.CanonicalName} due to partition key change: {existingDocument.PartitionKey} -> {upsertDocumentModel.PartitionKey}");
                }
                else
                {
                    logger.LogWarning($"{nameof(UpdateAsync)} failed to delete content for: {existingDocument.CanonicalName} due to partition key change: {existingDocument.PartitionKey} -> {upsertDocumentModel.PartitionKey}");
                    return(HttpStatusCode.BadRequest);
                }
            }

            var response = await contentPageService.UpsertAsync(upsertDocumentModel).ConfigureAwait(false);

            logger.LogInformation($"{nameof(UpdateAsync)} has upserted content for: {upsertDocumentModel.CanonicalName} with response code {response}");

            return(response);
        }
Ejemplo n.º 3
0
        public async Task <HttpStatusCode> UpdateAsync(TModel?upsertDocumentModel)
        {
            if (upsertDocumentModel == null)
            {
                return(HttpStatusCode.BadRequest);
            }

            var existingDocument = await contentPageService.GetByIdAsync(upsertDocumentModel.Id).ConfigureAwait(false);

            if (existingDocument == null)
            {
                return(HttpStatusCode.NotFound);
            }

            upsertDocumentModel.PageLocation = ExtractPageLocation(upsertDocumentModel);

            if (existingDocument.PartitionKey != null && existingDocument.PartitionKey.Equals(upsertDocumentModel.PartitionKey, StringComparison.Ordinal))
            {
                upsertDocumentModel.Etag = existingDocument.Etag;
            }
            else
            {
                var deleted = await contentPageService.DeleteAsync(existingDocument.Id).ConfigureAwait(false);

                if (deleted)
                {
                    logger.LogInformation($"{nameof(UpdateAsync)} has deleted content for: {existingDocument.CanonicalName} due to partition key change: {existingDocument.PartitionKey} -> {upsertDocumentModel.PartitionKey}");
                }
                else
                {
                    logger.LogWarning($"{nameof(UpdateAsync)} failed to delete content for: {existingDocument.CanonicalName} due to partition key change: {existingDocument.PartitionKey} -> {upsertDocumentModel.PartitionKey}");
                    return(HttpStatusCode.BadRequest);
                }
            }

            var response = await contentPageService.UpsertAsync(upsertDocumentModel).ConfigureAwait(false);

            logger.LogInformation($"{nameof(UpdateAsync)} has upserted content for: {upsertDocumentModel.CanonicalName} with response code {response}");

            return(response);
        }
        public async Task <IActionResult> Delete(Guid documentId)
        {
            var isDeleted = await jobCategoryPageContentService.DeleteAsync(documentId).ConfigureAwait(false);

            if (isDeleted)
            {
                logger.LogInformation($"{nameof(Delete)} has deleted content for document Id: {documentId}");
                return(Ok());
            }
            else
            {
                logger.LogWarning($"{nameof(Delete)} has returned no content for: {documentId}");
                return(NotFound());
            }
        }