/// <summary>
        /// Method that extends IWrapRequest<T> allowing to get ordination properties from request
        /// </summary>
        /// <typeparam name="TModel">Generic type of the entity</typeparam>
        /// <param name="source">Self IWrapRequest<T> instance</param>
        /// <returns>Returns a dictionary with properties and values found</returns>
        internal static Ordination Ordination <TModel>(
            this IWrapRequest <TModel> source
            ) where TModel : class
        {
            var ordination = new Ordination();

            #region GET ORDER PROPERTY
            var orderProperty = source.AllProperties.Where(x =>
                                                           x.Name.ToLower().Equals(Constants.CONST_ORDENATION_ORDER.ToLower()) &&
                                                           x.Source == WrapPropertySource.FromQuery
                                                           ).FirstOrDefault();
            if (orderProperty != null &&
                (orderProperty.Value.ToString().ToLower().Equals(Constants.CONST_ORDENATION_ORDER_ASCENDING.ToLower()) ||
                 orderProperty.Value.ToString().ToLower().Equals(Constants.CONST_ORDENATION_ORDER_DESCENDING.ToLower())))
            {
                ordination.Order = orderProperty.Value.ToString().ToLower().Equals(Constants.CONST_ORDENATION_ORDER_ASCENDING.ToLower()) ? Constants.CONST_ORDENATION_ORDER_ASCENDING : Constants.CONST_ORDENATION_ORDER_DESCENDING;
            }
            else
            {
                ordination.Order = Constants.CONST_ORDENATION_ORDER_ASCENDING;
            }
            #endregion

            #region GET ORDERBY PROPERTY
            var orderByProperty = source.AllProperties.Where(x =>
                                                             x.Name.ToLower().Equals(Constants.CONST_ORDENATION_ORDERBY.ToLower()) &&
                                                             x.Source == WrapPropertySource.FromQuery
                                                             ).FirstOrDefault();
            if (orderByProperty != null && typeof(TModel).GetProperties().Any(x => x.Name.ToLower().Equals(orderByProperty.Value.ToString().ToLower())))
            {
                var property = typeof(TModel).GetProperties().Where(x => x.Name.ToLower().Equals(orderByProperty.Value.ToString().ToLower())).SingleOrDefault();
                ordination.OrderBy = property.Name.ToCamelCase();
            }
            else
            {
                if (source.KeyProperties().Any())
                {
                    ordination.OrderBy = source.KeyProperties().FirstOrDefault();
                }
            }
            #endregion

            source.RequestObject.SetValue(Constants.CONST_ORDENATION, ordination);

            return(ordination);
        }
Esempio n. 2
0
        /// <summary>
        /// Mathod that extends IWrapRequest<T> allowing use patch
        /// </summary>
        /// <typeparam name="TModel">Generic type of the entity</typeparam>
        /// <param name="request">Self IWrapRequest<T> instance</param>
        /// <param name="model">Entity were data from request will be patched</param>
        /// <returns>Entity updated</returns>
        public static TModel Patch <TModel>(
            this IWrapRequest <TModel> request,
            TModel model
            ) where TModel : class
        {
            var properties = typeof(TModel).GetProperties().Where(x => request.SuppliedProperties().Any(y => y.ToLower().Equals(x.Name.ToLower()))).ToList();

            properties = properties.Where(p => !request.IsPropertySuppressed(p.Name)).ToList();
            properties = properties.Where(p => !request.KeyProperties().Any(x => x.ToLower().Equals(p.Name.ToLower()))).ToList();

            properties.ForEach(property => property.SetValue(model, property.GetValue(request.Model)));

            request.SetModelOnRequest(model, properties);

            return(model);
        }
Esempio n. 3
0
        /// <summary>
        /// Mathod that extends IWrapRequest<T> allowing use post
        /// </summary>
        /// <typeparam name="TModel">Generic type of the entity</typeparam>
        /// <param name="request">Self IWrapRequest<T> instance</param>
        /// <returns>New entity</returns>
        public static TModel Post <TModel>(
            this IWrapRequest <TModel> request
            ) where TModel : class
        {
            var model = Activator.CreateInstance <TModel>();

            var properties = typeof(TModel).GetProperties().ToList();

            properties = properties.Where(p => !request.IsPropertySuppressed(p.Name)).ToList();
            properties = properties.Where(p => !request.KeyProperties().Any(x => x.ToLower().Equals(p.Name.ToLower()))).ToList();

            properties.ForEach(property => property.SetValue(model, property.GetValue(request.Model)));

            request.SetModelOnRequest(model, properties);

            return(model);
        }