| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | |
| | | 4 | | namespace ZhEaIsNsAaBn.Utilities.APIResponses; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// The <see cref="ObjectResult"/> returned by <see cref="BaseResponse.ToAPI"/>. When it executes, a status that came fr |
| | | 8 | | /// resolved again with the app's <see cref="APIResponsesSettings"/> from the request services (defaults when none are r |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class BaseResponseResult : ObjectResult |
| | | 11 | | { |
| | | 12 | | private readonly BaseResponse _response; |
| | | 13 | | |
| | | 14 | | |
| | | 15 | | /// <summary>Creates the result.</summary> |
| | | 16 | | /// <param name="response">The response body.</param> |
| | | 17 | | /// <exception cref="ArgumentNullException"><paramref name="response"/> is <see langword="null"/>.</exception> |
| | 69 | 18 | | public BaseResponseResult( BaseResponse response ) : base( response ) |
| | | 19 | | { |
| | 69 | 20 | | ArgumentNullException.ThrowIfNull( response ); |
| | | 21 | | |
| | 69 | 22 | | _response = response; |
| | 69 | 23 | | StatusCode = response.Status; |
| | 69 | 24 | | } |
| | | 25 | | |
| | | 26 | | |
| | | 27 | | /// <summary>Resolves the status (see <see cref="APIResponsesSettings"/>), then writes the response.</summary> |
| | | 28 | | /// <param name="context">The action context.</param> |
| | | 29 | | /// <returns>A task that completes when the response is written.</returns> |
| | | 30 | | public override Task ExecuteResultAsync( ActionContext context ) |
| | | 31 | | { |
| | 66 | 32 | | ArgumentNullException.ThrowIfNull( context ); |
| | | 33 | | |
| | 66 | 34 | | if ( _response.StatusFromErrors ) |
| | | 35 | | { |
| | 60 | 36 | | var settings = context.HttpContext.RequestServices?.GetService<IOptions<APIResponsesSettings>>()?.Value; |
| | | 37 | | |
| | 60 | 38 | | if ( settings is not null ) |
| | | 39 | | { |
| | 60 | 40 | | _response.Status = (int) settings.StatusFor( _response.Errors ); |
| | 60 | 41 | | StatusCode = _response.Status; |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | |
| | 66 | 46 | | return base.ExecuteResultAsync( context ); |
| | | 47 | | } |
| | | 48 | | } |