From 767cdc1f6f6a63ce997fc9476911e2c361f9d402 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Wed, 20 Feb 2013 20:33:05 -0500 Subject: Pushing missing changes --- MediaBrowser.Model/Web/QueryStringDictionary.cs | 265 ++++++++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 MediaBrowser.Model/Web/QueryStringDictionary.cs (limited to 'MediaBrowser.Model/Web/QueryStringDictionary.cs') diff --git a/MediaBrowser.Model/Web/QueryStringDictionary.cs b/MediaBrowser.Model/Web/QueryStringDictionary.cs new file mode 100644 index 000000000..29d91f0e9 --- /dev/null +++ b/MediaBrowser.Model/Web/QueryStringDictionary.cs @@ -0,0 +1,265 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace MediaBrowser.Model.Web +{ + /// + /// Class QueryStringDictionary + /// + public class QueryStringDictionary : Dictionary + { + /// + /// Initializes a new instance of the class. + /// + public QueryStringDictionary() + : base(StringComparer.OrdinalIgnoreCase) + { + } + + /// + /// Adds the specified name. + /// + /// The name. + /// The value. + public void Add(string name, int value) + { + Add(name, value.ToString()); + } + + /// + /// Adds the specified name. + /// + /// The name. + /// The value. + public void Add(string name, long value) + { + Add(name, value.ToString()); + } + + /// + /// Adds the specified name. + /// + /// The name. + /// The value. + public void Add(string name, double value) + { + Add(name, value.ToString()); + } + + /// + /// Adds if not null or empty. + /// + /// The name. + /// The value. + public void AddIfNotNullOrEmpty(string name, string value) + { + if (!string.IsNullOrEmpty(value)) + { + Add(name, value); + } + } + + /// + /// Adds if not null. + /// + /// The name. + /// The value. + public void AddIfNotNull(string name, int? value) + { + if (value.HasValue) + { + Add(name, value.Value); + } + } + + /// + /// Adds if not null. + /// + /// The name. + /// The value. + public void AddIfNotNull(string name, double? value) + { + if (value.HasValue) + { + Add(name, value.Value); + } + } + + /// + /// Adds if not null. + /// + /// The name. + /// The value. + public void AddIfNotNull(string name, long? value) + { + if (value.HasValue) + { + Add(name, value.Value); + } + } + + /// + /// Adds the specified name. + /// + /// The name. + /// if set to true [value]. + public void Add(string name, bool value) + { + Add(name, value.ToString()); + } + + /// + /// Adds if not null. + /// + /// The name. + /// if set to true [value]. + public void AddIfNotNull(string name, bool? value) + { + if (value.HasValue) + { + Add(name, value.Value); + } + } + + /// + /// Adds the specified name. + /// + /// The name. + /// The value. + /// value + public void Add(string name, Guid value) + { + if (value == Guid.Empty) + { + throw new ArgumentNullException("value"); + } + + Add(name, value.ToString()); + } + + /// + /// Adds if not empty. + /// + /// The name. + /// The value. + public void AddIfNotEmpty(string name, Guid value) + { + if (value != Guid.Empty) + { + Add(name, value); + } + + Add(name, value); + } + + /// + /// Adds if not null. + /// + /// The name. + /// The value. + public void AddIfNotNull(string name, Guid? value) + { + if (value.HasValue) + { + Add(name, value.Value); + } + } + + /// + /// Adds the specified name. + /// + /// The name. + /// The value. + /// value + public void Add(string name, IEnumerable value) + { + if (value == null) + { + throw new ArgumentNullException("value"); + } + + Add(name, string.Join(",", value.Select(v => v.ToString()).ToArray())); + } + + /// + /// Adds if not null. + /// + /// The name. + /// The value. + public void AddIfNotNull(string name, IEnumerable value) + { + if (value != null) + { + Add(name, value); + } + } + + /// + /// Adds the specified name. + /// + /// The name. + /// The value. + /// value + public void Add(string name, IEnumerable value) + { + if (value == null) + { + throw new ArgumentNullException("value"); + } + + Add(name, string.Join(",", value.Select(v => v.ToString()).ToArray())); + } + + /// + /// Adds if not null. + /// + /// The name. + /// The value. + public void AddIfNotNull(string name, IEnumerable value) + { + if (value != null) + { + Add(name, value); + } + } + + /// + /// Gets the query string. + /// + /// System.String. + public string GetQueryString() + { + var queryParams = this.Select(i => string.Format("{0}={1}", i.Key, GetEncodedValue(i.Value))).ToArray(); + + return string.Join("&", queryParams); + } + + /// + /// Gets the encoded value. + /// + /// The value. + /// System.String. + private string GetEncodedValue(string value) + { + return value; + } + + /// + /// Gets the URL. + /// + /// The prefix. + /// System.String. + public string GetUrl(string prefix) + { + var query = GetQueryString(); + + if (string.IsNullOrEmpty(query)) + { + return prefix; + } + + return prefix + "?" + query; + } + } +} -- cgit v1.2.3