1
1
using System . Collections . Generic ;
2
2
using System . Linq ;
3
+ using System . Text . RegularExpressions ;
3
4
using JsonApiDotNetCore . Internal ;
4
5
using JsonApiDotNetCore . Internal . Contracts ;
5
6
using JsonApiDotNetCore . Internal . Query ;
6
7
using JsonApiDotNetCore . Managers . Contracts ;
7
8
using JsonApiDotNetCore . Models ;
8
- using JsonApiDotNetCore . Query ;
9
9
10
10
namespace JsonApiDotNetCore . Query
11
11
{
12
- public class IncludeService : IIncludeService , IQueryParameterService
12
+
13
+ public abstract class QueryParameterService : IQueryParameterService
14
+ {
15
+
16
+ /// <summary>
17
+ /// By default, the name is derived from the implementing type.
18
+ /// </summary>
19
+ /// <example>
20
+ /// The following query param service will match the query displayed in URL
21
+ /// `?include=some-relationship`
22
+ /// <code>public class IncludeService : QueryParameterService { /* ... */ } </code>
23
+ /// </example>
24
+ public virtual string Name { get { return GetParameterNameFromType ( ) ; } }
25
+
26
+ /// <inheritdoc/>
27
+ public abstract void Parse ( string value ) ;
28
+
29
+ /// <summary>
30
+ /// Gets the query parameter name from the implementing class name. Trims "Service"
31
+ /// from the name if present.
32
+ /// </summary>
33
+ private string GetParameterNameFromType ( ) => new Regex ( "Service$" ) . Replace ( GetType ( ) . Name , string . Empty ) ;
34
+ }
35
+
36
+ public class IncludeService : QueryParameterService , IIncludeService
13
37
{
14
38
/// todo: make readonly
15
39
private readonly List < List < RelationshipAttribute > > _includedChains ;
@@ -29,16 +53,14 @@ public IncludeService(ICurrentRequest currentRequest,
29
53
/// </summary>
30
54
internal IncludeService ( ) : this ( null , null ) { }
31
55
32
- public string Name => QueryConstants . INCLUDE ;
33
-
34
56
/// <inheritdoc/>
35
57
public List < List < RelationshipAttribute > > Get ( )
36
58
{
37
59
return _includedChains . Select ( chain => chain . ToList ( ) ) . ToList ( ) ;
38
60
}
39
61
40
62
/// <inheritdoc/>
41
- public void Parse ( string value )
63
+ public override void Parse ( string value )
42
64
{
43
65
if ( string . IsNullOrWhiteSpace ( value ) )
44
66
throw new JsonApiException ( 400 , "Include parameter must not be empty if provided" ) ;
0 commit comments