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;
}
}
}