From ef6b90b8e6e6c317fcda85a392c79324f91250db Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 25 Oct 2016 15:02:04 -0400 Subject: make controller project portable --- .../Services/QueryParamCollection.cs | 164 +++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 MediaBrowser.Model/Services/QueryParamCollection.cs (limited to 'MediaBrowser.Model/Services/QueryParamCollection.cs') diff --git a/MediaBrowser.Model/Services/QueryParamCollection.cs b/MediaBrowser.Model/Services/QueryParamCollection.cs new file mode 100644 index 000000000..6393a87fb --- /dev/null +++ b/MediaBrowser.Model/Services/QueryParamCollection.cs @@ -0,0 +1,164 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using MediaBrowser.Model.Dto; + +namespace MediaBrowser.Model.Services +{ + public class QueryParamCollection : List + { + public QueryParamCollection() + { + + } + + public QueryParamCollection(IDictionary headers) + { + foreach (var pair in headers) + { + Add(pair.Key, pair.Value); + } + } + + private StringComparison GetStringComparison() + { + return StringComparison.OrdinalIgnoreCase; + } + + private StringComparer GetStringComparer() + { + return StringComparer.OrdinalIgnoreCase; + } + + /// + /// Adds a new query parameter. + /// + public void Add(string key, string value) + { + Add(new NameValuePair(key, value)); + } + + public void Set(string key, string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + var stringComparison = GetStringComparison(); + + var parameters = this.Where(p => string.Equals(key, p.Name, stringComparison)).ToArray(); + + foreach (var p in parameters) + { + Remove(p); + } + + return; + } + + foreach (var pair in this) + { + var stringComparison = GetStringComparison(); + + if (string.Equals(key, pair.Name, stringComparison)) + { + pair.Value = value; + return; + } + } + + Add(key, value); + } + + /// + /// True if the collection contains a query parameter with the given name. + /// + public bool ContainsKey(string name) + { + return this.Any(p => p.Name == name); + } + + /// + /// Removes all parameters of the given name. + /// + /// The number of parameters that were removed + /// is null. + public int Remove(string name) + { + return RemoveAll(p => p.Name == name); + } + + public string Get(string name) + { + return GetValues(name).FirstOrDefault(); + } + + public string[] GetValues(string name) + { + var stringComparison = GetStringComparison(); + + return this.Where(p => string.Equals(p.Name, name, stringComparison)).Select(p => p.Value).ToArray(); + } + + public Dictionary ToDictionary() + { + var stringComparer = GetStringComparer(); + + var headers = new Dictionary(stringComparer); + + foreach (var pair in this) + { + headers[pair.Name] = pair.Value; + } + + return headers; + } + + public IEnumerable Keys + { + get { return this.Select(i => i.Name); } + } + + /// + /// Gets or sets a query parameter value by name. A query may contain multiple values of the same name + /// (i.e. "x=1&x=2"), in which case the value is an array, which works for both getting and setting. + /// + /// The query parameter name + /// The query parameter value or array of values + public string this[string name] + { + get { return Get(name); } + set + { + Set(name, value); + //var parameters = this.Where(p => p.Name == name).ToArray(); + //var values = new[] { value }; + + //for (int i = 0; ; i++) + //{ + // if (i < parameters.Length && i < values.Length) + // { + // if (values[i] == null) + // Remove(parameters[i]); + // else if (values[i] is NameValuePair) + // this[IndexOf(parameters[i])] = (NameValuePair)values[i]; + // else + // parameters[i].Value = values[i]; + // } + // else if (i < parameters.Length) + // Remove(parameters[i]); + // else if (i < values.Length) + // { + // if (values[i] != null) + // { + // if (values[i] is NameValuePair) + // Add((NameValuePair)values[i]); + // else + // Add(name, values[i]); + // } + // } + // else + // break; + //} + } + } + } +} -- cgit v1.2.3