| | | 1 | | namespace ZhEaIsNsAaBn.Utilities.APIResponses; |
| | | 2 | | |
| | | 3 | | /// <summary><see cref="BaseResponse"/> with a <see cref="Data"/> payload.</summary> |
| | | 4 | | /// <typeparam name="T">The payload type.</typeparam> |
| | | 5 | | public class BaseResponse< T > : BaseResponse |
| | | 6 | | { |
| | | 7 | | /// <summary>The payload.</summary> |
| | | 8 | | public T? Data { get; set; } |
| | | 9 | | |
| | | 10 | | |
| | | 11 | | /// <summary>A 400 response with <paramref name="errors"/>.</summary> |
| | | 12 | | /// <param name="errors">The errors.</param> |
| | | 13 | | /// <returns>The response.</returns> |
| | 0 | 14 | | public static new BaseResponse<T> BadRequest( List<Error> errors ) => new() { Status = (int) HttpStatusCode.BadReque |
| | | 15 | | |
| | | 16 | | /// <summary>A 400 response with <paramref name="errors"/> and a payload.</summary> |
| | | 17 | | /// <param name="data">The payload.</param> |
| | | 18 | | /// <param name="errors">The errors.</param> |
| | | 19 | | /// <returns>The response.</returns> |
| | 0 | 20 | | public static BaseResponse<T> BadRequest( T data, List<Error> errors ) => new() { Status = (int) HttpStatusCode.BadR |
| | | 21 | | |
| | | 22 | | /// <summary>200 with the result when it is valid (or <see langword="null"/>), 400 with the result when it isn't.</s |
| | | 23 | | /// <param name="result">The validation result.</param> |
| | | 24 | | /// <returns>The response.</returns> |
| | | 25 | | public static BaseResponse<ValidationResponse<T>> FromValidation( ValidationResponse<T>? result ) |
| | | 26 | | { |
| | 0 | 27 | | if ( result is null ) { return new BaseResponse<ValidationResponse<T>> { Status = (int) HttpStatusCode.OK }; } |
| | | 28 | | |
| | | 29 | | |
| | 0 | 30 | | return new BaseResponse<ValidationResponse<T>> { Status = (int) ( result.IsValid ? HttpStatusCode.OK : HttpStatu |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary>A 400 response carrying the validation <paramref name="errors"/>, or 200 when there are none.</summary> |
| | | 34 | | /// <param name="errors">The column errors.</param> |
| | | 35 | | /// <returns>The response.</returns> |
| | | 36 | | public static new BaseResponse<ValidationResponse<T>> FromValidation( List<ColumnValidationError>? errors ) |
| | 0 | 37 | | => errors is { Count: > 0 } |
| | 0 | 38 | | ? new BaseResponse<ValidationResponse<T>> { Status = (int) HttpStatusCode.BadRequest, Data = errors } |
| | 0 | 39 | | : new BaseResponse<ValidationResponse<T>> { Status = (int) HttpStatusCode.OK }; |
| | | 40 | | |
| | | 41 | | /// <summary>A 400 response carrying the validation <paramref name="error"/>, or 200 when it is <see langword="null" |
| | | 42 | | /// <param name="error">The column error.</param> |
| | | 43 | | /// <returns>The response.</returns> |
| | | 44 | | public static new BaseResponse<ValidationResponse<T>> FromValidation( ColumnValidationError? error ) |
| | 3 | 45 | | => error is not null |
| | 3 | 46 | | ? new BaseResponse<ValidationResponse<T>> { Status = (int) HttpStatusCode.BadRequest, Data = error } |
| | 3 | 47 | | : new BaseResponse<ValidationResponse<T>> { Status = (int) HttpStatusCode.OK }; |
| | | 48 | | |
| | | 49 | | /// <summary>A 200 response with <paramref name="data"/>.</summary> |
| | | 50 | | /// <param name="data">The payload.</param> |
| | | 51 | | /// <returns>The response.</returns> |
| | 0 | 52 | | public static BaseResponse<T> Ok( T data ) => new() { Status = (int) HttpStatusCode.OK, Data = data }; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// 200 with the result on success (also when the result is <c>0</c>, <see langword="false"/>, <see langword="null"/ |
| | | 56 | | /// list); on failure the errors and the result, with the status mapped from the first error (see <see cref="APIResp |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="response">The layer result.</param> |
| | | 59 | | /// <exception cref="ArgumentNullException"><paramref name="response"/> is <see langword="null"/>.</exception> |
| | | 60 | | public static explicit operator BaseResponse<T>( LayerResponse<T> response ) |
| | | 61 | | { |
| | 63 | 62 | | ArgumentNullException.ThrowIfNull( response ); |
| | | 63 | | |
| | 78 | 64 | | if ( response.IsSuccess ) { return new BaseResponse<T> { Status = (int) HttpStatusCode.OK, Data = response.Resul |
| | | 65 | | |
| | | 66 | | |
| | 48 | 67 | | var failed = FromErrors<BaseResponse<T>>( response.Errors ); |
| | 48 | 68 | | failed.Data = response.Result; |
| | | 69 | | |
| | | 70 | | |
| | 48 | 71 | | return failed; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary>A failed response with one error; status mapped from it (see <see cref="APIResponsesSettings"/>).</summ |
| | | 75 | | /// <param name="error">The error.</param> |
| | 0 | 76 | | public static implicit operator BaseResponse<T>( Error error ) => FromErrors<BaseResponse<T>>( [ error ] ); |
| | | 77 | | |
| | | 78 | | /// <summary>A failed response with the errors; status mapped from the first one (see <see cref="APIResponsesSetting |
| | | 79 | | /// <param name="errors">The errors.</param> |
| | 3 | 80 | | public static implicit operator BaseResponse<T>( List<Error> errors ) => FromErrors<BaseResponse<T>>( errors ); |
| | | 81 | | |
| | | 82 | | /// <summary>200 with the payload; an invalid <see cref="ValidationResponse"/> payload gives 400.</summary> |
| | | 83 | | /// <param name="result">The payload.</param> |
| | | 84 | | public static implicit operator BaseResponse<T>( T? result ) |
| | | 85 | | { |
| | 9 | 86 | | if ( result is null ) { return new BaseResponse<T> { Status = (int) HttpStatusCode.OK }; } |
| | | 87 | | |
| | | 88 | | |
| | 9 | 89 | | var status = result is ValidationResponse { IsValid: false } ? HttpStatusCode.BadRequest : HttpStatusCode.OK; |
| | | 90 | | |
| | | 91 | | |
| | 9 | 92 | | return new BaseResponse<T> { Status = (int) status, Data = result }; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary>The payload.</summary> |
| | | 96 | | /// <param name="result">The response.</param> |
| | 3 | 97 | | public static implicit operator T?( BaseResponse<T> result ) => result is null ? default : result.Data; |
| | | 98 | | } |