public async Task JobProfileOverviewSegmentServicePatchHiddenAlternativeTitleReturnsExceptionWhenPatchmodelIsNull()
        {
            // arrange
            PatchHiddenAlternativeTitleModel patchModel = null;
            var documentId = Guid.NewGuid();

            // act
            var exceptionResult = await Assert.ThrowsAsync <ArgumentNullException>(async() => await jobProfileOverviewSegmentService.PatchHiddenAlternativeTitleAsync(patchModel, documentId).ConfigureAwait(false)).ConfigureAwait(false);

            // assert
            Assert.Equal("Value cannot be null. (Parameter 'patchModel')", exceptionResult.Message);
        }
        public async Task SegmentControllerPatchHiddenAlternativeTitleReturnsBadRequestWhenNullPatchmodel(string mediaTypeName)
        {
            // Arrange
            const HttpStatusCode             expectedResponse = HttpStatusCode.BadRequest;
            PatchHiddenAlternativeTitleModel patchModel       = null;
            var documentId = Guid.NewGuid();
            var controller = BuildSegmentController(mediaTypeName);

            // Act
            var result = await controller.PatchHiddenAlternativeTitle(patchModel, documentId).ConfigureAwait(false);

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

            Assert.Equal((int)expectedResponse, statusResult.StatusCode);

            controller.Dispose();
        }
        public async Task <HttpStatusCode> PatchHiddenAlternativeTitleAsync(PatchHiddenAlternativeTitleModel patchModel, Guid documentId)
        {
            if (patchModel is null)
            {
                throw new ArgumentNullException(nameof(patchModel));
            }

            var existingSegmentModel = await GetByIdAsync(documentId).ConfigureAwait(false);

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

            if (patchModel.SequenceNumber <= existingSegmentModel.SequenceNumber)
            {
                return(HttpStatusCode.AlreadyReported);
            }

            var existingHiddenAltTitle = existingSegmentModel.Data?.HiddenAlternativeTitle.SingleOrDefault(r => r.Id == patchModel.Id);

            if (existingHiddenAltTitle is null)
            {
                return(patchModel.MessageAction == MessageAction.Deleted ? HttpStatusCode.AlreadyReported : HttpStatusCode.NotFound);
            }

            var existingIndex = existingSegmentModel.Data.HiddenAlternativeTitle.ToList().FindIndex(ai => ai.Id == patchModel.Id);

            if (patchModel.MessageAction == MessageAction.Deleted)
            {
                existingSegmentModel.Data.HiddenAlternativeTitle.RemoveAt(existingIndex);
            }
            else
            {
                var updatedHiddenAltTitle = mapper.Map <Data.Models.HiddenAlternativeTitle>(patchModel);
                existingSegmentModel.Data.HiddenAlternativeTitle[existingIndex] = updatedHiddenAltTitle;
            }

            existingSegmentModel.SequenceNumber = patchModel.SequenceNumber;

            return(await UpsertAndRefreshSegmentModel(existingSegmentModel).ConfigureAwait(false));
        }
        public async Task <IActionResult> PatchHiddenAlternativeTitle([FromBody] PatchHiddenAlternativeTitleModel patchHiddenAlternativeTitleModel, Guid documentId)
        {
            logService.LogInformation($"{PatchHiddenAlternativeTitleActionName} has been called");

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

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

            var response = await jobProfileOverviewSegmentService.PatchHiddenAlternativeTitleAsync(patchHiddenAlternativeTitleModel, documentId).ConfigureAwait(false);

            if (response != HttpStatusCode.OK && response != HttpStatusCode.Created)
            {
                logService.LogError($"{PatchHiddenAlternativeTitleActionName}: Error while patching Hidden Alternative Title content for Job Profile with Id: {patchHiddenAlternativeTitleModel.JobProfileId} for the {patchHiddenAlternativeTitleModel.Title} title");
            }

            return(new StatusCodeResult((int)response));
        }