< Summary - ZhEaIsNsAaBn.Libraries coverage (affected: 23 packages)

Information
Class: ZhEaIsNsAaBn.Utilities.APIResponses.BaseResponse<T>
Assembly: ZhEaIsNsAaBn.Utilities.APIResponses
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.APIResponses/BaseResponseOfT.cs
Line coverage
59%
Covered lines: 13
Uncovered lines: 9
Coverable lines: 22
Total lines: 98
Line coverage: 59%
Branch coverage
45%
Covered branches: 9
Total branches: 20
Branch coverage: 45%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BadRequest(...)100%210%
BadRequest(...)100%210%
FromValidation(...)0%2040%
FromValidation(...)0%2040%
FromValidation(...)50%22100%
Ok(...)100%210%
op_Explicit(...)100%22100%
op_Implicit(...)100%210%
op_Implicit(...)100%11100%
op_Implicit(...)83.33%66100%
op_Implicit(...)50%22100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.APIResponses/BaseResponseOfT.cs

#LineLine coverage
 1namespace ZhEaIsNsAaBn.Utilities.APIResponses;
 2
 3/// <summary><see cref="BaseResponse"/> with a <see cref="Data"/> payload.</summary>
 4/// <typeparam name="T">The payload type.</typeparam>
 5public 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>
 014    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>
 020    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    {
 027        if ( result is null ) { return new BaseResponse<ValidationResponse<T>> { Status = (int) HttpStatusCode.OK }; }
 28
 29
 030        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 )
 037        => errors is { Count: > 0 }
 038               ? new BaseResponse<ValidationResponse<T>> { Status = (int) HttpStatusCode.BadRequest, Data = errors }
 039               : 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 )
 345        => error is not null
 346               ? new BaseResponse<ValidationResponse<T>> { Status = (int) HttpStatusCode.BadRequest, Data = error }
 347               : 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>
 052    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    {
 6362        ArgumentNullException.ThrowIfNull( response );
 63
 7864        if ( response.IsSuccess ) { return new BaseResponse<T> { Status = (int) HttpStatusCode.OK, Data = response.Resul
 65
 66
 4867        var failed = FromErrors<BaseResponse<T>>( response.Errors );
 4868        failed.Data = response.Result;
 69
 70
 4871        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>
 076    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>
 380    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    {
 986        if ( result is null ) { return new BaseResponse<T> { Status = (int) HttpStatusCode.OK }; }
 87
 88
 989        var status = result is ValidationResponse { IsValid: false } ? HttpStatusCode.BadRequest : HttpStatusCode.OK;
 90
 91
 992        return new BaseResponse<T> { Status = (int) status, Data = result };
 93    }
 94
 95    /// <summary>The payload.</summary>
 96    /// <param name="result">The response.</param>
 397    public static implicit operator T?( BaseResponse<T> result ) => result is null ? default : result.Data;
 98}