Add StatusExtensions with HTTP status mapping methods
Added StatusExtensions.cs with extension methods for Status and HttpStatusCode: - ToHttpStatusCode maps Status to nullable HttpStatusCode if possible. - IsSuccess checks if a Status or HttpStatusCode represents a successful response. - Handles both direct Status values and those convertible to HTTP codes.
This commit is contained in:
32
src/ReC.Domain/Constants/StatusExtensions.cs
Normal file
32
src/ReC.Domain/Constants/StatusExtensions.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Net;
|
||||
|
||||
namespace ReC.Domain.Constants;
|
||||
|
||||
public static class StatusExtensions
|
||||
{
|
||||
public static HttpStatusCode? ToHttpStatusCode(this Status status)
|
||||
{
|
||||
int code = (int)status;
|
||||
|
||||
if (Enum.IsDefined(typeof(HttpStatusCode), code))
|
||||
{
|
||||
return (HttpStatusCode)code;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool IsSuccess(this HttpStatusCode code)
|
||||
{
|
||||
int value = (int)code;
|
||||
return value >= 200 && value <= 299;
|
||||
}
|
||||
|
||||
public static bool IsSuccess(this Status status)
|
||||
=> status switch
|
||||
{
|
||||
Status.QuerySuccess => true,
|
||||
Status.QueryFailed => false,
|
||||
_ => status.ToHttpStatusCode() is HttpStatusCode httpStatus && httpStatus.IsSuccess()
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user