|
1 |
| -using System.Collections.Generic; |
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Linq;
|
3 | 4 | using System.Net;
|
4 | 5 | using System.Net.Http;
|
@@ -170,6 +171,60 @@ public async Task GetSpecificRectangle()
|
170 | 171 | ValidateRectangle(rectangle, "2");
|
171 | 172 | }
|
172 | 173 | }
|
| 174 | + |
| 175 | + [Fact(DisplayName = "Get a group and validation relationship")] |
| 176 | + public async Task GetGroup() |
| 177 | + { |
| 178 | + using (var server = CreateServer()) |
| 179 | + { |
| 180 | + var client = server.GetClient(); |
| 181 | + var result = await client.GetJsonResponseAsync("api/groups"); |
| 182 | + _output.WriteLine(result.ToString()); |
| 183 | + |
| 184 | + var items = result["data"] as JArray; |
| 185 | + Assert.Equal(1, items.Count); |
| 186 | + |
| 187 | + var group = items[0]; |
| 188 | + Assert.Equal("group", group["type"]); |
| 189 | + Assert.Equal("1", group["id"]); |
| 190 | + Assert.Equal("Group 1", group["attributes"]["name"]); |
| 191 | + |
| 192 | + var relationships = group["relationships"]["shapes"]["data"] as JArray; |
| 193 | + Assert.Equal(3, relationships.Count); |
| 194 | + Assert.Equal("circle", relationships[0]["type"]); |
| 195 | + Assert.Equal("1", relationships[0]["id"]); |
| 196 | + |
| 197 | + Assert.Equal("rectangle", relationships[1]["type"]); |
| 198 | + Assert.Equal("2", relationships[1]["id"]); |
| 199 | + |
| 200 | + Assert.Equal("circle", relationships[2]["type"]); |
| 201 | + Assert.Equal("3", relationships[2]["id"]); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + [Fact(DisplayName = "Get a group with included shapes and validate types and shapes itself")] |
| 206 | + public async Task GetGroupWithIncludes() |
| 207 | + { |
| 208 | + using (var server = CreateServer()) |
| 209 | + { |
| 210 | + var client = server.GetClient(); |
| 211 | + var result = await client.GetJsonResponseAsync("api/groups?include=shapes"); |
| 212 | + _output.WriteLine(result.ToString()); |
| 213 | + |
| 214 | + var items = result["data"] as JArray; |
| 215 | + var included = result["included"] as JArray; |
| 216 | + Assert.Equal(1, items.Count); |
| 217 | + Assert.Equal(3, included.Count); |
| 218 | + |
| 219 | + var circle1 = included[0]; |
| 220 | + var rectangle2 = included[1]; |
| 221 | + var circle3 = included[2]; |
| 222 | + |
| 223 | + ValidateCircle(circle1,"1"); |
| 224 | + ValidateRectangle(rectangle2, "2"); |
| 225 | + ValidateCircle(circle3,"3"); |
| 226 | + } |
| 227 | + } |
173 | 228 |
|
174 | 229 |
|
175 | 230 | private static void ValidateRectangle(JToken rectangle, string expectedId)
|
@@ -222,18 +277,38 @@ public class ShapeApiResourceProvider : IApiResourceProvider
|
222 | 277 | {
|
223 | 278 | public ApiResource Resolve(object dataObject)
|
224 | 279 | {
|
225 |
| - if (dataObject is Circle) |
| 280 | + var type = dataObject.GetType(); |
| 281 | + |
| 282 | + if (type.IsEnumerable() || type.IsArray) |
| 283 | + type = type.GetGenericTypeParameterOfCollection(); |
| 284 | + |
| 285 | + if (type == typeof(Group)) |
| 286 | + { |
| 287 | + return new GroupResource(); |
| 288 | + } |
| 289 | + |
| 290 | + if (type == typeof(Circle)) |
226 | 291 | {
|
227 | 292 | return new CircleResource();
|
228 | 293 | }
|
229 | 294 |
|
230 |
| - if (dataObject is Rectangle) |
| 295 | + if (type == typeof(Rectangle)) |
231 | 296 | {
|
232 | 297 | return new RectangleResource();
|
233 | 298 | }
|
234 | 299 |
|
235 | 300 | return new ShapeResource();
|
236 | 301 | }
|
| 302 | + |
| 303 | + public ApiResource ResolveRelationship(object dataObject, ApiResource relationship) |
| 304 | + { |
| 305 | + if (relationship is ShapeResource) |
| 306 | + { |
| 307 | + return Resolve(dataObject); |
| 308 | + } |
| 309 | + |
| 310 | + return new GroupResource(); |
| 311 | + } |
237 | 312 | }
|
238 | 313 | }
|
239 | 314 | }
|
0 commit comments