Skip to content

Commit

Permalink
Merge pull request #301 from hahmed/search-base-class
Browse files Browse the repository at this point in the history
Base class for searching GitHub
  • Loading branch information
shiftkey committed Jan 14, 2014
2 parents 524fb37 + 02fe983 commit 7890f70
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
93 changes: 93 additions & 0 deletions Octokit/Models/Request/BaseSearchRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace Octokit
{
/// <summary>
/// Base class for searching issues/code/users/repos
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1012:AbstractTypesShouldNotHaveConstructors")]
public abstract class BaseSearchRequest
{
public BaseSearchRequest(string term)
{
Ensure.ArgumentNotNullOrEmptyString(term, "term");
Term = term;
Page = 1;
PerPage = 100;
Order = SortDirection.Descending;
}

/// <summary>
/// The search term
/// </summary>
public string Term { get; private set; }

/// <summary>
/// The sort field
/// </summary>
public abstract string Sort
{
get;
}

private string SortOrder
{
get
{
return Order == SortDirection.Ascending ? "asc" : "desc";
}
}

/// <summary>
/// Optional Sort order if sort parameter is provided. One of asc or desc; the default is desc.
/// </summary>
public SortDirection Order { get; set; }

/// <summary>
/// Page of paginated results
/// </summary>
public int Page { get; set; }

/// <summary>
/// Number of items per page
/// </summary>
public int PerPage { get; set; }

/// <summary>
/// All qualifiers that are used for this search
/// </summary>
public abstract IReadOnlyCollection<string> MergedQualifiers();

/// <summary>
/// Add qualifiers onto the search term
/// </summary>
private string TermAndQualifiers
{
get
{
var mergedParameters = String.Join("+", MergedQualifiers());
return Term + (mergedParameters.IsNotBlank() ? "+" + mergedParameters : "");
}
}

/// <summary>
/// Get the query parameters that will be appending onto the search
/// </summary>
public IDictionary<string, string> Parameters
{
get
{
var d = new Dictionary<string, string>();
d.Add("page", Page.ToString(CultureInfo.CurrentCulture));
d.Add("per_page", PerPage.ToString(CultureInfo.CurrentCulture));
d.Add("sort", Sort);
d.Add("order", SortOrder);
d.Add("q", TermAndQualifiers);
return d;
}
}
}
}
1 change: 1 addition & 0 deletions Octokit/Octokit-Mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
<Compile Include="Clients\IGistCommentsClient.cs" />
<Compile Include="Models\Response\GistComment.cs" />
<Compile Include="Models\Request\BodyWrapper.cs" />
<Compile Include="Models\Request\BaseSearchRequest.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
1 change: 1 addition & 0 deletions Octokit/Octokit-MonoAndroid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
<Compile Include="Models\Request\LabelUpdate.cs" />
<Compile Include="Models\Request\NewLabel.cs" />
<Compile Include="Models\Response\Branch.cs" />
<Compile Include="Models\Request\BaseSearchRequest.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
1 change: 1 addition & 0 deletions Octokit/Octokit-Monotouch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
<Compile Include="Models\Request\LabelUpdate.cs" />
<Compile Include="Models\Request\NewLabel.cs" />
<Compile Include="Models\Response\Branch.cs" />
<Compile Include="Models\Request\BaseSearchRequest.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
1 change: 1 addition & 0 deletions Octokit/Octokit-netcore45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
<Compile Include="Clients\IGistCommentsClient.cs" />
<Compile Include="Models\Response\GistComment.cs" />
<Compile Include="Models\Request\BodyWrapper.cs" />
<Compile Include="Models\Request\BaseSearchRequest.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Clients\ActivitiesClient.cs" />
<Compile Include="Clients\SearchClient.cs" />
<Compile Include="Clients\ISearchClient.cs" />
<Compile Include="Models\Request\BaseSearchRequest.cs" />
<Compile Include="Models\Request\SearchCodeRequest.cs" />
<Compile Include="Models\Request\SearchIssuesRequest.cs" />
<Compile Include="Models\Request\SearchQualifierOperator.cs" />
Expand Down

0 comments on commit 7890f70

Please sign in to comment.