Esempio n. 1
0
        public static IPaginationCollection <dynamic> FixTimeZoneOffsetInQueryResult(IPaginationCollection <dynamic> dtos)
        {
            if (dtos.Items != null)
            {
                var timeZoneOffset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);

                var items = new List <dynamic>();
                foreach (var dtoItem in dtos.Items)
                {
                    if (dtoItem is IDictionary <string, object> propertyDictionary)
                    {
                        var expandoObjectDictionary = new Dictionary <string, object>();
                        foreach (var(propertyName, value) in propertyDictionary)
                        {
                            var propertyValue = value;

                            // Property fix operations
                            if (propertyValue is DateTime dateTimeProperty)
                            {
                                propertyValue = dateTimeProperty.Add(timeZoneOffset);
                            }

                            expandoObjectDictionary.Add(propertyName, propertyValue);
                        }

                        // Convert Dictionary to ExpandoObject
                        var expandoObject = new ExpandoObject();
                        var eoColl        = (ICollection <KeyValuePair <string, object> >)expandoObject;
                        foreach (var kvp in expandoObjectDictionary)
                        {
                            eoColl.Add(kvp);
                        }

                        dynamic dynamicObject = expandoObject;
                        items.Add(dynamicObject);
                    }
                }

                return(new PaginationCollection <dynamic>
                {
                    Count = dtos.Count,
                    Items = items
                });
            }

            return(dtos);
        }
        public static dynamic ExecuteSelectQuery <T>(this IPaginationCollection <T> paginationCollection, IDictionary <string, bool> selectFields)
        {
            if (paginationCollection?.Items == null || selectFields == null || !selectFields.Any())
            {
                return(paginationCollection);
            }

            bool isInclude = selectFields.Values.Any(x => x);
            bool isExclude = selectFields.Values.Any(x => !x);

            if (isInclude && isExclude)
            {
                throw new SelectQueryInclusionException();
            }

            var selectedProperties      = new List <PropertyInfo>();
            var jsonFieldNameDictionary = new Dictionary <string, string>();

            var properties = typeof(T).GetProperties();

            foreach (var propertyInfo in properties)
            {
                bool?isSelected            = null;
                var  jsonPropertyAttribute = propertyInfo.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(JsonPropertyAttribute));
                var  constructorArguments  = jsonPropertyAttribute?.ConstructorArguments.ToList();
                var  attributeValue        = constructorArguments?.Select(x => x.Value?.ToString()).FirstOrDefault(x => !string.IsNullOrEmpty(x));
                if (attributeValue != null && selectFields.ContainsKey(attributeValue))
                {
                    isSelected = selectFields[attributeValue];
                }

                if (attributeValue != null && !string.IsNullOrEmpty(attributeValue))
                {
                    /*
                     * In projections that explicitly include fields, the _id field is the only field that you can explicitly exclude.
                     * In projections that explicitly excludes fields, the _id field is the only field that you can explicitly include; however, the _id field is included by default.
                     */
                    if (attributeValue == "_id")
                    {
                        selectedProperties.Add(propertyInfo);
                        jsonFieldNameDictionary.Add(propertyInfo.Name, attributeValue);
                        continue;
                    }

                    if (isInclude)
                    {
                        if (isSelected != null && isSelected.Value)
                        {
                            selectedProperties.Add(propertyInfo);
                        }
                    }
                    else
                    {
                        if (isSelected != null && !isSelected.Value)
                        {
                            selectedProperties.Add(propertyInfo);
                        }
                    }

                    if (!jsonFieldNameDictionary.ContainsKey(propertyInfo.Name))
                    {
                        jsonFieldNameDictionary.Add(propertyInfo.Name, attributeValue);
                    }
                }
            }

            if (isExclude)
            {
                var exludedProperties = new List <PropertyInfo>();
                exludedProperties.AddRange(selectedProperties);
                selectedProperties.Clear();
                selectedProperties.AddRange(properties.Where(propertyInfo => !exludedProperties.Contains(propertyInfo)));
            }

            List <ExpandoObject> projectinatedList = new List <ExpandoObject>();

            foreach (var item in paginationCollection.Items)
            {
                dynamic expandoObject = new ExpandoObject();
                IDictionary <string, object> expandoObjectDictionary = expandoObject as IDictionary <string, object>;
                foreach (var propertyInfo in selectedProperties)
                {
                    var propertyName = jsonFieldNameDictionary.ContainsKey(propertyInfo.Name)
                                                ? jsonFieldNameDictionary[propertyInfo.Name]
                                                : propertyInfo.Name;

                    expandoObjectDictionary.Add(propertyName, propertyInfo.GetValue(item));
                }

                projectinatedList.Add(expandoObject);
            }

            return(new PaginationCollection <dynamic>
            {
                Count = paginationCollection.Count,
                Items = projectinatedList
            });
        }