Beispiel #1
0
        public static ExpandableOfmForGet Shape(this ExpandableOfmForGet expandableOfmForGetSource, string fields)
        {
            if (expandableOfmForGetSource == null)
            {
                throw new ArgumentNullException("expandableOfmForGetSource");
            }

            if (string.IsNullOrWhiteSpace(fields))
            {
                return(expandableOfmForGetSource);
            }

            // the field are separated by ",", so we split it.
            var fieldsAfterSplit = fields.Split(',').Select(s => s.ToLower().Trim());

            var shapedExpandableOfmForGet = new ExpandableOfmForGet();

            foreach (var field in fieldsAfterSplit)
            {
                var property = expandableOfmForGetSource.FirstOrDefault(f => f.Key.ToLowerInvariant() == field);

                if (!property.IsDefault()) // in effect if the struct KeyValuePair is not null
                {
                    shapedExpandableOfmForGet.Add(property.Key, property.Value);
                }
            }

            // return
            return(shapedExpandableOfmForGet);
        }
        public static IEnumerable <ExpandableOfmForGet> Shape(
            this IEnumerable <ExpandableOfmForGet> expandableOfmForGetSourceCollection,
            string fields,
            bool includeHateoasLinks)
        {
            if (expandableOfmForGetSourceCollection == null)
            {
                throw new ArgumentNullException("expandableOfmForGetSourceCollection");
            }

            var expandableOfmForGetList           = new List <ExpandableOfmForGet>();
            IEnumerable <string> fieldsAfterSplit = null;

            if (string.IsNullOrWhiteSpace(fields))
            {
                return(expandableOfmForGetSourceCollection);
            }
            else
            {
                fieldsAfterSplit = fields.Split(',').Select(field => field.ToLower().Trim());
            }

            foreach (var ofmForGetSource in expandableOfmForGetSourceCollection)
            {
                var shapedExpandableOfmForGet = new ExpandableOfmForGet();

                foreach (var field in fieldsAfterSplit)
                {
                    var property = ofmForGetSource.FirstOrDefault(f => f.Key.ToLowerInvariant() == field);

                    if (!property.IsDefault()) // in effect if the struct KeyValuePair is not null
                    {
                        shapedExpandableOfmForGet.Add(property.Key, property.Value);
                    }
                }

                if (includeHateoasLinks)
                {
                    var property = ofmForGetSource.FirstOrDefault(f => f.Key.ToLowerInvariant() == "links");
                    shapedExpandableOfmForGet.Add(property.Key, property.Value);
                }

                expandableOfmForGetList.Add(shapedExpandableOfmForGet);
            }

            return(expandableOfmForGetList);
        }