2
2
using SampleWebApiAspNetCore . Models ;
3
3
using SampleWebApiAspNetCore . Helpers ;
4
4
using System . Reflection ;
5
+ using Microsoft . AspNetCore . Mvc . Routing ;
6
+ using Microsoft . AspNetCore . Mvc . Infrastructure ;
5
7
6
8
namespace SampleWebApiAspNetCore . Services
7
9
{
8
- [ AttributeUsage ( AttributeTargets . Method ) ]
9
- public class LinkCollectionAttribute : Attribute
10
- {
11
- private string name ;
12
-
13
- public LinkCollectionAttribute ( string name )
14
- {
15
- this . name = name ;
16
- }
17
- }
18
-
19
- [ AttributeUsage ( AttributeTargets . Method ) ]
20
- public class HateoasAttribute : Attribute
21
- {
22
-
23
- }
24
-
25
10
public class LinkService < T > : ILinkService < T >
26
11
{
27
12
private readonly IUrlHelper _urlHelper ;
28
13
29
- public LinkService ( IUrlHelper urlHelper )
14
+ public LinkService ( IUrlHelperFactory urlHelperFactory , IActionContextAccessor actionContextAccessor )
30
15
{
31
- _urlHelper = urlHelper ;
16
+ _urlHelper = urlHelperFactory . GetUrlHelper ( actionContextAccessor . ActionContext ) ;
32
17
}
33
18
34
19
public List < LinkDto > CreateLinksForCollection ( QueryParameters queryParameters , int totalCount , ApiVersion version )
35
20
{
36
- Type myType = ( typeof ( T ) ) ;
37
- MethodInfo [ ] methods = myType . GetMethods ( ) ;
21
+ Type controllerType = ( typeof ( T ) ) ;
22
+ MethodInfo [ ] methods = controllerType . GetMethods ( ) ;
38
23
39
24
var links = new List < LinkDto > ( ) ;
40
- var getAllMethodName = GetGetAllMethod ( methods ) ;
25
+ var getAllMethodName = GetMethod ( methods , typeof ( HttpGetAttribute ) , 0 ) ;
41
26
42
27
// self
43
28
links . Add ( new LinkDto ( _urlHelper . Link ( getAllMethodName , new
@@ -95,21 +80,21 @@ public object ExpandSingleFoodItem(object resource, int identifier, ApiVersion v
95
80
{
96
81
var resourceToReturn = resource . ToDynamic ( ) as IDictionary < string , object > ;
97
82
98
- var links = GetLinks ( identifier , version ) ;
83
+ var links = GetLinksForSingleItem ( identifier , version ) ;
99
84
100
85
resourceToReturn . Add ( "links" , links ) ;
101
86
102
87
return resourceToReturn ;
103
88
}
104
89
105
90
106
- private IEnumerable < LinkDto > GetLinks ( int id , ApiVersion version )
91
+ private IEnumerable < LinkDto > GetLinksForSingleItem ( int id , ApiVersion version )
107
92
{
108
93
Type myType = ( typeof ( T ) ) ;
109
94
MethodInfo [ ] methods = myType . GetMethods ( ) ;
110
95
var links = new List < LinkDto > ( ) ;
111
96
112
- var getLink = _urlHelper . Link ( GetGetSingleMethod ( methods ) , new { version = version . ToString ( ) , id = id } ) ;
97
+ var getLink = _urlHelper . Link ( GetMethod ( methods , typeof ( HttpGetAttribute ) , 1 ) , new { version = version . ToString ( ) , id = id } ) ;
113
98
links . Add ( new LinkDto ( getLink , "self" , "GET" ) ) ;
114
99
115
100
var deleteLink = _urlHelper . Link ( GetMethod ( methods , typeof ( HttpDeleteAttribute ) ) , new { version = version . ToString ( ) , id = id } ) ;
@@ -133,62 +118,33 @@ private IEnumerable<LinkDto> GetLinks(int id, ApiVersion version)
133
118
return links ;
134
119
}
135
120
136
- private string GetMethod ( MethodInfo [ ] methods , Type type )
137
- {
138
- var method = methods . Where ( m => m . GetCustomAttributes ( type , false ) . Length > 0 ) . ToArray ( ) . FirstOrDefault ( ) ;
139
-
140
- if ( method is null )
141
- {
142
- return null ;
143
- }
144
-
145
- return method . Name ;
146
- }
147
-
148
- private string GetGetSingleMethod ( MethodInfo [ ] methods )
121
+ private string GetMethod ( MethodInfo [ ] methods , Type type , int routeParamsLength = 0 )
149
122
{
150
- var getMethods = methods . Where ( m => m . GetCustomAttributes ( typeof ( HttpGetAttribute ) , false ) . Length > 0 ) . ToArray ( ) ;
123
+ var filteredMethods = methods . Where ( m => m . GetCustomAttributes ( type , false ) . Length > 0 ) . ToArray ( ) ;
151
124
152
- if ( getMethods . Length == 0 )
125
+ if ( filteredMethods . Length == 0 )
153
126
{
154
- return null ;
127
+ return "" ;
155
128
}
156
129
157
- foreach ( var getMethod in getMethods )
130
+ if ( routeParamsLength == 0 )
158
131
{
159
- var routeAttribs = getMethod . GetCustomAttributes ( typeof ( RouteAttribute ) ) ;
132
+ var toReturn = filteredMethods . FirstOrDefault ( ) ;
160
133
161
- if ( routeAttribs . Count ( ) == 1 )
162
- {
163
- return getMethod . Name ;
164
- }
134
+ return toReturn is not null ? toReturn . Name : "" ;
165
135
}
166
136
167
-
168
- return null ;
169
- }
170
-
171
- private string GetGetAllMethod ( MethodInfo [ ] methods )
172
- {
173
- var getMethods = methods . Where ( m => m . GetCustomAttributes ( typeof ( HttpGetAttribute ) , false ) . Length > 0 ) . ToArray ( ) ;
174
-
175
- if ( getMethods . Length == 0 )
137
+ foreach ( var method in filteredMethods )
176
138
{
177
- return null ;
178
- }
139
+ var routeAttribs = method . GetCustomAttributes ( typeof ( RouteAttribute ) ) ;
179
140
180
- foreach ( var getMethod in getMethods )
181
- {
182
- var routeAttribs = getMethod . GetCustomAttributes ( typeof ( RouteAttribute ) ) ;
183
-
184
- if ( routeAttribs . Count ( ) == 0 )
141
+ if ( routeAttribs . Count ( ) == routeParamsLength )
185
142
{
186
- return getMethod . Name ;
143
+ return method . Name ;
187
144
}
188
145
}
189
146
190
-
191
- return null ;
147
+ return "" ;
192
148
}
193
149
}
194
150
}
0 commit comments