-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added fields as query parameter #199
Changes from 4 commits
33a03a5
461cebc
08cec56
ed4de8e
79c9a86
a0ee858
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Saule.Queries.Fieldset | ||
{ | ||
/// <summary> | ||
/// Context for fieldset operations | ||
/// </summary> | ||
public class FieldsetContext | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FieldsetContext"/> class. | ||
/// </summary> | ||
/// <param name="queryParams">query string that might contain Fieldset keyword</param> | ||
public FieldsetContext(IEnumerable<KeyValuePair<string, string>> queryParams) | ||
{ | ||
Properties = | ||
from query in queryParams | ||
where query.Key.StartsWith(Constants.QueryNames.Fieldset) | ||
let type = query.Key.Substring(Constants.QueryNames.Fieldset.Length + 1) | ||
let fields = query.Value.Split(',') | ||
select new FieldsetProperty(type, fields); | ||
} | ||
|
||
/// <summary> | ||
/// Gets including properties | ||
/// </summary> | ||
public IEnumerable<FieldsetProperty> Properties { get; internal set; } | ||
|
||
/// <inheritdoc/> | ||
public override string ToString() | ||
{ | ||
return Properties != null && Properties.Any() ? string.Join("&", Properties.Select(p => p.ToString())) : string.Empty; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace Saule.Queries.Fieldset | ||
{ | ||
/// <summary> | ||
/// Property for fieldset | ||
/// </summary> | ||
public class FieldsetProperty | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FieldsetProperty"/> class. | ||
/// </summary> | ||
/// <param name="type">type for field filter</param> | ||
/// <param name="fields">fields to serialize filter</param> | ||
public FieldsetProperty(string type, string[] fields) | ||
{ | ||
Type = type; | ||
Fields = fields; | ||
} | ||
|
||
/// <summary> | ||
/// Gets property type | ||
/// </summary> | ||
public string Type { get; } | ||
|
||
/// <summary> | ||
/// Gets property fields | ||
/// </summary> | ||
public string[] Fields { get; } | ||
|
||
/// <inheritdoc/> | ||
public override string ToString() | ||
{ | ||
return $"filter[{Type}]={string.Join(",", Fields)}"; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,22 @@ public void ConditionallyAppliesFilters() | |
|
||
} | ||
|
||
[Fact(DisplayName = "Uses query fieldset expressions if specified")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need a test where we ask for a field that's not on the model (should get nothing in that case). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the test in a0ee858. I think only the attributes should be empty as id and type field are still returned which I think would be correct behaviour at this point. |
||
public void UsesQueryFieldsetExpressions() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks like tabs and spaces got mixed in this file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having some trouble detecting those on my machine I hope they got fixed with a0ee858 |
||
{ | ||
var target = new JsonApiSerializer<CompanyResource> | ||
{ | ||
AllowQuery = true | ||
}; | ||
var companies = Get.Companies(1).ToList(); | ||
var result = target.Serialize(companies, new Uri(DefaultUrl, "?fields[corporation]=Name,Location")); | ||
_output.WriteLine(result.ToString()); | ||
|
||
Assert.NotNull(result["data"][0]["attributes"]["name"]); | ||
Assert.NotNull(result["data"][0]["attributes"]["location"]); | ||
Assert.Null(result["data"][0]["attributes"]["number-of-employees"]); | ||
} | ||
|
||
[Fact(DisplayName = "Does not allow null Uri")] | ||
public void HasAContract() | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should say
fields
instead offilter
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 79c9a86