Mike-Mortensen-Portfolio / PubHub_H6_Final

This repository represents the final exam for the Data Technician with Specaility in Programming 2024. Grade recieved: Simon: 12 (A+), Jasmin: 12 (A+), Mike: 12 (A+)
MIT License
2 stars 1 forks source link

ToQuery not processing collections correctly #143

Closed ZhakalenDk closed 5 months ago

ZhakalenDk commented 5 months ago

However takes this just replace the code in GenericExtensions -> ToQuery<TObject> with this code:

        /// <summary>
        /// Converts this <typeparamref name="TObject"/> into a query representation of itself
        /// </summary>
        /// <typeparam name="TObject"></typeparam>
        /// <param name="obj"></param>
        /// <param name="ignoreNull">Whether or not <see langword="null"/> values should be excluded from the query <see langword="string"/></param>
        /// <param name="enforceLowercase">Whether or not property names should be formatted as <strong>lowercase</strong> characters</param>
        /// <returns>A <see langword="string"/> that represents <paramref name="obj"/> as query parameters</returns>
        public static string ToQuery<TObject>(this TObject obj, bool ignoreNull = false, bool enforceLowercase = false) where TObject : new()
        {
            string queryString = string.Empty;
            var properties = typeof(TObject).GetProperties();
            for (int i = 0; i < properties.Length; i++)
            {
                var propertyName = ((enforceLowercase) ? (properties[i].Name.ToLowerInvariant()) : (properties[i].Name));
                var propertyValue = properties[i].GetValue(obj);

                if (ignoreNull && propertyValue == null)
                    continue;

                if (propertyValue!.GetType().IsArray)
                {
                    var propertValueAsCollection = propertyValue as IList;
                    for (int j = 0; j < propertValueAsCollection!.Count; j++)
                    {
                        queryString += $"{propertyName}={propertValueAsCollection[j]?.ToString()}{(((j + 1 < propertValueAsCollection.Count) || i + 1 < properties.Length) ? ("&") : (string.Empty))}";
                    }

                    continue;
                }

                queryString += $"{propertyName}={propertyValue}{((i + 1 < properties.Length) ? ("&") : (string.Empty))}";
            }

            return queryString;
        }

This lets the method process arrays.

Sjimooon commented 5 months ago

Shouldn't we set the ignoreNull parameter to true by default, so that we won't try to convert properties with a value of null to query parameters? I don't think a query parameter with an empty value wouldn't be of much use 😅

image

ZhakalenDk commented 5 months ago

Shouldn't we set the ignoreNull parameter to true by default, so that we won't try to convert properties with a value of null to query parameters? I don't think a query parameter with an empty value wouldn't be of much use 😅

image

I get your point. However, the value null is still a value 🤔

We could however check if the type is colelctiojn and mainly state if that is null or empty simply leave it out 🤷