private T CreatePathFromOperation <T>(PatchOperation operation, ITestApiContext context) where T : PatchOperationBase
        {
            var validationResult = this._pathFromValidationService.Validate(operation);

            if (validationResult.HasErrors())
            {
                context.AddError(string.Format("{0}  The instruction was ignored.", validationResult));
                return(null);
            }

            if (operation.Value != null)
            {
                context.AddWarning(FormatTooMuchInformationWarningMessage(operation, "value"));
            }

            return(this.CreateOperation <T>(operation.Path, operation.From));
        }
        private T CreatePathValueOperation <T>(PatchOperation operation, ITestApiContext context) where T : PatchOperationBase
        {
            var validationResult = this._pathValueValidationService.Validate(operation);

            if (validationResult.HasErrors())
            {
                context.AddError(string.Format("{0}  The instruction was ignored.", validationResult));
                return(null);
            }

            if (!operation.From.IsNullOrEmpty())
            {
                var warning = FormatTooMuchInformationWarningMessage(operation, "from");
                context.AddWarning(warning);
            }

            return(this.CreateOperation <T>(operation.Path, operation.Value));
        }
예제 #3
0
        public ResponseEnvelope <TicketWM> HandlePatch(Guid entityId, IEnumerable <PatchOperationBase> changes, ITestApiContext context)
        {
            var ticketEntity = _ticketService.GetTicket(entityId);

            var ticket = _objectFactory.Create <Ticket>(ticketEntity);

            var ticketWM = _objectFactory.Create <TicketWM>(ticket);


            var operationBases = changes as IList <PatchOperationBase> ?? changes.ToList();

            //Todo This logic will be changed when rest of PatchOperation will be decided.
            var replaceOperatonList =
                operationBases.Where(operation => operation.GetType() == typeof(ReplacePatchOperation)).ToList();

            var invalidOperations = replaceOperatonList.Where(x => x.Path.TrimStart('/').Equals("ticketNumber", StringComparison.OrdinalIgnoreCase));

            if (invalidOperations.Any())
            {
                context.AddError("Invalid patch request.");
                throw new BadRequestException();
            }

            _entityPatcher.Patch(ticketWM, replaceOperatonList, context);


            _ticketService.UpdateTicket(_objectFactory.Create <Ticket>(ticketWM));


            var updatedTicket = _ticketService.GetTicket(entityId);


            var ticketWMUpadted = _objectFactory.Create <TicketWM>(updatedTicket);

            var recordCounts = CreateRecordCounts(1);

            var searchResults = new ApiSearchResult <TicketWM>(recordCounts, ticketWMUpadted);

            return(_responseBuilder.CreateResponse(context, searchResults));
        }
예제 #4
0
        private static void UpdateField(object entity, Queue <string> path, object value, ITestApiContext context)
        {
            var entityType   = entity.GetType();
            var propertyName = path.Dequeue();

            var property = entityType.GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

            if (property == null)
            {
                context.AddError(
                    string.Format("Property Name {0} does not exist in {1} Model", propertyName, entityType.Name));
                return;
            }

            if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(IDictionary <,>))
            {
                var dictionary = GetDictionaryPropertyValue(entity, path, value, property);
                value = dictionary;
            }

            value = GetCollectionPropertyValue(value, property);

            if (path.Any())
            {
                var propertyValue = property.GetValue(entity);

                UpdateField(propertyValue, path, value, context);
                return;
            }

            var strongTypedValue =
                value is IConvertible
                   ? Convert.ChangeType(value, property.PropertyType)
                   : value;

            property.SetValue(entity, strongTypedValue);
        }
        /// <summary>
        /// The translate.
        /// </summary>
        /// <param name="operations">
        /// The operations.
        /// </param>
        /// <param name="context">
        /// Context containing information about the request.
        /// </param>
        /// <returns>
        /// The patch operations in business object form.
        /// </returns>
        public IEnumerable <PatchOperationBase> Translate(IEnumerable <PatchOperation> operations, ITestApiContext context)
        {
            Ensure.That <PostRequestEntityDetailsNullException>(operations != null);

            var operationList = new List <PatchOperationBase>();

            foreach (var operation in operations)
            {
                var operationInstruction = operation.Operation ?? string.Empty;
                switch (operationInstruction.ToLowerInvariant())
                {
                case "test":
                    var testOperation = this.CreatePathValueOperation <TestPatchOperation>(operation, context);
                    if (testOperation != null)
                    {
                        operationList.Add(testOperation);
                    }

                    break;

                case "remove":
                    var removeOperation = this.CreatePathOnlyOperation <RemovePatchOperation>(operation, context);
                    if (removeOperation != null)
                    {
                        operationList.Add(removeOperation);
                    }

                    break;

                case "add":
                    var addOperation = this.CreatePathValueOperation <AddPatchOperation>(operation, context);
                    if (addOperation != null)
                    {
                        operationList.Add(addOperation);
                    }

                    break;

                case "replace":
                    var replaceOperation = this.CreatePathValueOperation <ReplacePatchOperation>(operation, context);
                    if (replaceOperation != null)
                    {
                        operationList.Add(replaceOperation);
                    }

                    break;

                case "move":
                    var moveOperation = this.CreatePathFromOperation <MovePatchOperation>(operation, context);
                    if (moveOperation != null)
                    {
                        operationList.Add(moveOperation);
                    }

                    break;

                case "copy":
                    var copyOperation = this.CreatePathFromOperation <CopyPatchOperation>(operation, context);
                    if (copyOperation != null)
                    {
                        operationList.Add(copyOperation);
                    }

                    break;

                default:
                    context.AddError(string.Format(@"Invalid PATCH operation '{0}'.  The instruction was ignored.", operation.Operation ?? "null"));
                    break;
                }
            }

            return(operationList);
        }