Skip to content

Commit 5a5f080

Browse files
author
Bart Koelman
authored
Cleanup formatting; fixed some outdated signatures (#860)
1 parent 1cff6eb commit 5a5f080

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

docs/usage/resources/resource-definitions.md

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ public class UserDefinition : JsonApiResourceDefinition<User>
3030
{
3131
}
3232

33-
public override SparseFieldSetExpression OnApplySparseFieldSet(SparseFieldSetExpression existingSparseFieldSet)
33+
public override SparseFieldSetExpression OnApplySparseFieldSet(
34+
SparseFieldSetExpression existingSparseFieldSet)
3435
{
3536
if (IsAdministrator)
3637
{
3738
return existingSparseFieldSet;
3839
}
3940

40-
return existingSparseFieldSet.Excluding<User>(x => x.Password, ResourceGraph);
41+
return existingSparseFieldSet.Excluding<User>(
42+
user => user.Password, ResourceGraph);
4143
}
4244
}
4345
```
@@ -82,6 +84,10 @@ You can define the default sort order if no `sort` query string parameter is pro
8284
```c#
8385
public class AccountDefinition : JsonApiResourceDefinition<Account>
8486
{
87+
public AccountDefinition(IResourceGraph resourceGraph) : base(resourceGraph)
88+
{
89+
}
90+
8591
public override SortExpression OnApplySort(SortExpression existingSort)
8692
{
8793
if (existingSort != null)
@@ -105,17 +111,25 @@ You may want to enforce pagination on large database tables.
105111
```c#
106112
public class AccessLogDefinition : JsonApiResourceDefinition<AccessLog>
107113
{
108-
public override PaginationExpression OnApplyPagination(PaginationExpression existingPagination)
114+
public AccessLogDefinition(IResourceGraph resourceGraph) : base(resourceGraph)
115+
{
116+
}
117+
118+
public override PaginationExpression OnApplyPagination(
119+
PaginationExpression existingPagination)
109120
{
110121
var maxPageSize = new PageSize(10);
111122

112123
if (existingPagination != null)
113124
{
114-
var pageSize = existingPagination.PageSize?.Value <= maxPageSize.Value ? existingPagination.PageSize : maxPageSize;
125+
var pageSize = existingPagination.PageSize?.Value <= maxPageSize.Value
126+
? existingPagination.PageSize
127+
: maxPageSize;
128+
115129
return new PaginationExpression(existingPagination.PageNumber, pageSize);
116130
}
117131

118-
return new PaginationExpression(PageNumber.ValueOne, _maxPageSize);
132+
return new PaginationExpression(PageNumber.ValueOne, maxPageSize);
119133
}
120134
}
121135
```
@@ -127,17 +141,26 @@ Soft-deletion sets `IsSoftDeleted` to `true` instead of actually deleting the re
127141
```c#
128142
public class AccountDefinition : JsonApiResourceDefinition<Account>
129143
{
144+
public AccountDefinition(IResourceGraph resourceGraph) : base(resourceGraph)
145+
{
146+
}
147+
130148
public override FilterExpression OnApplyFilter(FilterExpression existingFilter)
131149
{
132150
var resourceContext = ResourceGraph.GetResourceContext<Account>();
133-
var isSoftDeletedAttribute = resourceContext.Attributes.Single(a => a.Property.Name == nameof(Account.IsSoftDeleted));
151+
152+
var isSoftDeletedAttribute =
153+
resourceContext.Attributes.Single(a =>
154+
a.Property.Name == nameof(Account.IsSoftDeleted));
134155

135156
var isNotSoftDeleted = new ComparisonExpression(ComparisonOperator.Equals,
136-
new ResourceFieldChainExpression(isSoftDeletedAttribute), new LiteralConstantExpression(bool.FalseString));
157+
new ResourceFieldChainExpression(isSoftDeletedAttribute),
158+
new LiteralConstantExpression(bool.FalseString));
137159

138160
return existingFilter == null
139161
? (FilterExpression) isNotSoftDeleted
140-
: new LogicalExpression(LogicalOperator.And, new[] {isNotSoftDeleted, existingFilter});
162+
: new LogicalExpression(LogicalOperator.And,
163+
new[] {isNotSoftDeleted, existingFilter});
141164
}
142165
}
143166
```
@@ -147,9 +170,15 @@ public class AccountDefinition : JsonApiResourceDefinition<Account>
147170
```c#
148171
public class EmployeeDefinition : JsonApiResourceDefinition<Employee>
149172
{
150-
public override IReadOnlyCollection<IncludeElementExpression> OnApplyIncludes(IReadOnlyCollection<IncludeElementExpression> existingIncludes)
173+
public EmployeeDefinition(IResourceGraph resourceGraph) : base(resourceGraph)
174+
{
175+
}
176+
177+
public override IReadOnlyCollection<IncludeElementExpression> OnApplyIncludes(
178+
IReadOnlyCollection<IncludeElementExpression> existingIncludes)
151179
{
152-
if (existingIncludes.Any(include => include.Relationship.Property.Name == nameof(Employee.Manager)))
180+
if (existingIncludes.Any(include =>
181+
include.Relationship.Property.Name == nameof(Employee.Manager)))
153182
{
154183
throw new JsonApiException(new Error(HttpStatusCode.BadRequest)
155184
{
@@ -175,7 +204,12 @@ But it only works on primary resource endpoints (for example: /articles, but not
175204
```c#
176205
public class ItemDefinition : JsonApiResourceDefinition<Item>
177206
{
178-
protected override QueryStringParameterHandlers OnRegisterQueryableHandlersForQueryStringParameters()
207+
public ItemDefinition(IResourceGraph resourceGraph) : base(resourceGraph)
208+
{
209+
}
210+
211+
public override QueryStringParameterHandlers<Item>
212+
OnRegisterQueryableHandlersForQueryStringParameters()
179213
{
180214
return new QueryStringParameterHandlers<Item>
181215
{
@@ -186,10 +220,14 @@ public class ItemDefinition : JsonApiResourceDefinition<Item>
186220
};
187221
}
188222

189-
private static IQueryable<Item> FilterByHighRisk(IQueryable<Item> source, StringValues parameterValue)
223+
private static IQueryable<Item> FilterByHighRisk(IQueryable<Item> source,
224+
StringValues parameterValue)
190225
{
191226
bool isFilterOnHighRisk = bool.Parse(parameterValue);
192-
return isFilterOnHighRisk ? source.Where(item => item.RiskLevel >= 5) : source.Where(item => item.RiskLevel < 5);
227+
228+
return isFilterOnHighRisk
229+
? source.Where(item => item.RiskLevel >= 5)
230+
: source.Where(item => item.RiskLevel < 5);
193231
}
194232
}
195233
```

0 commit comments

Comments
 (0)