Skip to content

Commit

Permalink
collection.OrderBy("Name", "Foo desc").Expand("Dog").InlineCount().To…
Browse files Browse the repository at this point in the history
…Path()
  • Loading branch information
remi committed Jan 30, 2011
1 parent 7d1015f commit 2a50944
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 13 deletions.
30 changes: 30 additions & 0 deletions spec/NugetExampleSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ namespace EasyOData.Specs {
public class NugetExampleSpec : Spec {

// Note, this isn't specific to NugetExampleSpec and can be moved ...
//
// TODO move this [Test] and split it into 1 [Test] per QueryOption
//
[Test]
public void can_get_path_that_would_be_queried() {
var comma = "%2c";
var slash = "%2f";

var collection = new Collection {
Service = new Service(),
Expand Down Expand Up @@ -44,6 +48,32 @@ public void can_get_path_that_would_be_queried() {
ShouldEqual(string.Format("Dogs?$top=3&$select=Name{0}Category{0}Foo&$skip=4", comma));

// OrderBy
collection.OrderBy("*").ToPath().ShouldEqual("Dogs?$orderby=*");
collection.OrderBy("Name").ToPath().ShouldEqual("Dogs?$orderby=Name");
collection.OrderBy("Name", "Category").ToPath().ShouldEqual(string.Format("Dogs?$orderby=Name{0}Category", comma));
collection.OrderBy("Name", "Category,Foo").ToPath().ShouldEqual(string.Format("Dogs?$orderby=Name{0}Category{0}Foo", comma));
collection.Top(3).OrderBy("Name", "Category,Foo desc").Skip(4).ToPath().
ShouldEqual(string.Format("Dogs?$top=3&$orderby=Name{0}Category{0}Foo%20desc&$skip=4", comma));
collection.Select("Name").OrderBy("Name", "Category").Top(3).ToPath().
ShouldEqual(string.Format("Dogs?$select=Name&$orderby=Name{0}Category&$top=3", comma));

// Expand
collection.Expand("*").ToPath().ShouldEqual("Dogs?$expand=*");
collection.Expand("Name").ToPath().ShouldEqual("Dogs?$expand=Name");
collection.Expand("Name", "Category").ToPath().ShouldEqual(string.Format("Dogs?$expand=Name{0}Category", comma));
collection.Expand("Name", "Products/Suppliers").ToPath().ShouldEqual(string.Format("Dogs?$expand=Name{0}Products{1}Suppliers", comma, slash));
collection.Top(4).Expand("Name", "Category,Foo").Skip(2).ToPath().
ShouldEqual(string.Format("Dogs?$top=4&$expand=Name{0}Category{0}Foo&$skip=2", comma));

// InlineCount
collection.InlineCount().ToPath().ShouldEqual("Dogs?$inlinecount=allpages");
collection.Top(1).InlineCount().ToPath().ShouldEqual("Dogs?$top=1&$inlinecount=allpages");
collection.NoInlineCount().ToPath().ShouldEqual("Dogs?$inlinecount=none");
collection.Top(3).NoInlineCount().Skip(4).ToPath().ShouldEqual("Dogs?$top=3&$inlinecount=none&$skip=4");

// Filter <--- probably won't be using a RAW Filter, but I want to support it!

// custom filter method
}

string NuGetServiceRoot = "http://packages.nuget.org/v1/FeedService.svc/";
Expand Down
82 changes: 69 additions & 13 deletions src/EasyOData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,60 +68,96 @@ public static Collection ToCollection(this XmlNode node, Service service) {
}

public abstract class QueryOption {

// 5
// Name
public virtual object Value { get; set; }

// $top
// $select
public virtual string Key {
get { return "$" + GetType().Name.Replace("QueryOption","").ToLower(); }
}

public QueryOption() {}
public QueryOption(object value) {
Value = value;
}

public abstract string AddToPath(string path);

public virtual string AddQueryString(string path, string queryKey, string queryValue) {
path += path.Contains("?") ? "&" : "?";
return string.Format("{0}{1}={2}", path, queryKey, HttpUtility.UrlEncode(queryValue));
}

public virtual string AddToPath(string path) {
return AddQueryString(path, Key, Value.ToString());
}
}

public class TopQueryOption : QueryOption {
public TopQueryOption() : base() {}
public TopQueryOption(object v) : base(v) {}

public new int Value {
get { return (int) base.Value; }
set { base.Value = value; }
}

public override string AddToPath(string path) {
return AddQueryString(path, "$top", Value.ToString());
}
}

public class SkipQueryOption : QueryOption {
public SkipQueryOption() : base() {}
public SkipQueryOption(object v) : base(v) {}

public new int Value {
get { return (int) base.Value; }
set { base.Value = value; }
}
}

public class SelectQueryOption : QueryOption {
public SelectQueryOption(object v) : base(v) {}

public new string[] Value {
get { return base.Value as string[]; }
set { base.Value = value; }
}

public override string AddToPath(string path) {
return AddQueryString(path, "$skip", Value.ToString());
return AddQueryString(path, Key, string.Join(",", Value).Replace(" ",""));
}
}

public class SelectQueryOption : QueryOption {
public SelectQueryOption() : base() {}
public SelectQueryOption(object v) : base(v) {}
public class ExpandQueryOption : QueryOption {
public ExpandQueryOption(object v) : base(v) {}

public new string[] Value {
get { return base.Value as string[]; }
set { base.Value = value; }
}

public override string AddToPath(string path) {
return AddQueryString(path, Key, string.Join(",", Value).Replace(" ",""));
}
}

public class OrderByQueryOption : QueryOption {
public OrderByQueryOption(object v) : base(v) {}

public new string[] Value {
get { return base.Value as string[]; }
set { base.Value = value; }
}

public override string AddToPath(string path) {
return AddQueryString(path, "$select", string.Join(",", Value).Replace(" ",""));
// can have spaces, eg. "Name asc" (should encode to %20 - HttpUtility encodes to +, which we fix)
return AddQueryString(path, Key, string.Join(",", Value)).Replace("+", "%20");
}
}

public class InlineCountQueryOption : QueryOption {
public InlineCountQueryOption(bool allPages) {
if (allPages)
Value = "allpages";
else
Value = "none";
}
}

Expand Down Expand Up @@ -179,6 +215,26 @@ public Collection Select(params string[] propertyNames) {
return this;
}

public Collection OrderBy(params string[] propertyNamesWithAscOrDesc) {
Query.Add(new OrderByQueryOption(propertyNamesWithAscOrDesc));
return this;
}

public Collection Expand(params string[] propertyOrAssociatinNames) {
Query.Add(new ExpandQueryOption(propertyOrAssociatinNames));
return this;
}

public Collection InlineCount() {
Query.Add(new InlineCountQueryOption(true));
return this;
}

public Collection NoInlineCount() {
Query.Add(new InlineCountQueryOption(false));
return this;
}

public string ToPath() {
var path = Query.Path;
_query.Clear();
Expand Down

0 comments on commit 2a50944

Please sign in to comment.