|
| 1 | +using System.Net; |
1 | 2 | using System.Threading;
|
2 | 3 | using System.Threading.Tasks;
|
3 | 4 | using JsonApiDotNetCore.Configuration;
|
| 5 | +using JsonApiDotNetCore.Controllers; |
| 6 | +using JsonApiDotNetCore.Serialization.Objects; |
4 | 7 | using JsonApiDotNetCore.Services;
|
5 | 8 | using Microsoft.AspNetCore.Mvc;
|
6 | 9 | using Microsoft.Extensions.Logging;
|
7 | 10 |
|
8 | 11 | namespace JsonApiDotNetCoreTests.IntegrationTests.ControllerActionResults
|
9 | 12 | {
|
10 |
| - public sealed class ToothbrushesController : BaseToothbrushesController |
| 13 | + public sealed class ToothbrushesController : BaseJsonApiController<Toothbrush, int> |
11 | 14 | {
|
| 15 | + internal const int EmptyActionResultId = 11111111; |
| 16 | + internal const int ActionResultWithErrorObjectId = 22222222; |
| 17 | + internal const int ActionResultWithStringParameter = 33333333; |
| 18 | + internal const int ObjectResultWithErrorObjectId = 44444444; |
| 19 | + internal const int ObjectResultWithErrorCollectionId = 55555555; |
| 20 | + |
12 | 21 | public ToothbrushesController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Toothbrush> resourceService)
|
13 | 22 | : base(options, loggerFactory, resourceService)
|
14 | 23 | {
|
15 | 24 | }
|
16 | 25 |
|
17 | 26 | [HttpGet("{id}")]
|
18 |
| - public override Task<IActionResult> GetAsync(int id, CancellationToken cancellationToken) |
| 27 | + public override async Task<IActionResult> GetAsync(int id, CancellationToken cancellationToken) |
19 | 28 | {
|
20 |
| - return base.GetAsync(id, cancellationToken); |
| 29 | + if (id == EmptyActionResultId) |
| 30 | + { |
| 31 | + return NotFound(); |
| 32 | + } |
| 33 | + |
| 34 | + if (id == ActionResultWithErrorObjectId) |
| 35 | + { |
| 36 | + return NotFound(new ErrorObject(HttpStatusCode.NotFound) |
| 37 | + { |
| 38 | + Title = "No toothbrush with that ID exists." |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + if (id == ActionResultWithStringParameter) |
| 43 | + { |
| 44 | + return Conflict("Something went wrong."); |
| 45 | + } |
| 46 | + |
| 47 | + if (id == ObjectResultWithErrorObjectId) |
| 48 | + { |
| 49 | + return Error(new ErrorObject(HttpStatusCode.BadGateway)); |
| 50 | + } |
| 51 | + |
| 52 | + if (id == ObjectResultWithErrorCollectionId) |
| 53 | + { |
| 54 | + var errors = new[] |
| 55 | + { |
| 56 | + new ErrorObject(HttpStatusCode.PreconditionFailed), |
| 57 | + new ErrorObject(HttpStatusCode.Unauthorized), |
| 58 | + new ErrorObject(HttpStatusCode.ExpectationFailed) |
| 59 | + { |
| 60 | + Title = "This is not a very great request." |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + return Error(errors); |
| 65 | + } |
| 66 | + |
| 67 | + return await base.GetAsync(id, cancellationToken); |
21 | 68 | }
|
22 | 69 | }
|
23 | 70 | }
|
0 commit comments