Permalink
Browse files

collection.OrderBy("Name", "Foo desc").Expand("Dog").InlineCount().To…

…Path()
  • Loading branch information...
1 parent 7d1015f commit 2a5094408157a15b048970432a5bfe2ca54ab5e8 @remi committed Jan 30, 2011
Showing with 99 additions and 13 deletions.
  1. +30 −0 spec/NugetExampleSpec.cs
  2. +69 −13 src/EasyOData.cs
View
@@ -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(),
@@ -44,6 +48,32 @@ public class NugetExampleSpec : Spec {
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/";
View
@@ -68,60 +68,96 @@ public static class XmlParsing {
}
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";
}
}
@@ -179,6 +215,26 @@ public class Collection {
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();

0 comments on commit 2a50944

Please sign in to comment.