using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
namespace Octokit
{
///
/// Base class for searching issues/code/users/repos
///
[SuppressMessage("Microsoft.Design", "CA1012:AbstractTypesShouldNotHaveConstructors")]
public abstract class BaseSearchRequest
{
///
/// Initializes a new instance of the class.
///
protected BaseSearchRequest()
{
Page = 1;
PerPage = 100;
Order = SortDirection.Descending;
}
///
/// Initializes a new instance of the class.
///
/// The term.
protected BaseSearchRequest(string term) : this()
{
Ensure.ArgumentNotNullOrEmptyString(term, "term");
Term = term;
}
///
/// The search term
///
public string Term { get; private set; }
///
/// The sort field
///
public abstract string Sort
{
get;
}
///
/// Gets the sort order as a properly formatted lowercased query string parameter.
///
///
/// The sort order.
///
private string SortOrder
{
get
{
return Order.ToParameter();
}
}
///
/// Optional Sort order if sort parameter is provided. One of asc or desc; the default is desc.
///
public SortDirection Order { get; set; }
///
/// Page of paginated results
///
public int Page { get; set; }
///
/// Number of items per page
///
public int PerPage { get; set; }
///
/// All qualifiers that are used for this search
///
public abstract IReadOnlyList MergedQualifiers();
///
/// Add qualifiers onto the search term
///
private string TermAndQualifiers
{
get
{
var mergedParameters = string.Join("+", MergedQualifiers());
return Term + (mergedParameters.IsNotBlank() ? "+" + mergedParameters : "");
}
}
///
/// Get the query parameters that will be appending onto the search
///
public IDictionary Parameters
{
get
{
var d = new Dictionary
{
{ "page", Page.ToString(CultureInfo.CurrentCulture) }
, { "per_page", PerPage.ToString(CultureInfo.CurrentCulture) }
, { "order", SortOrder }
, { "q", TermAndQualifiers }
};
if (!string.IsNullOrWhiteSpace(Sort))
{
d.Add("sort", Sort);
}
return d;
}
}
}
}