Last active
April 1, 2021 00:06
-
-
Save Kurtz1993/97edb04788e3b1913c73894c50e3794f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net; | |
namespace App.Models | |
{ | |
public class ApiResponse<T> where T : class | |
{ | |
public T Data { get; set; } | |
public string Message { get; set; } | |
public HttpStatusCode StatusCode { get; set; } | |
public bool Success => (int)StatusCode >= 200 && (int)StatusCode < 300; | |
/// <summary> | |
/// Creates a simple ApiResponse object with status 200 (OK) and the given data. | |
/// </summary> | |
/// <param name="data">Data to be returned to the client.</param> | |
public ApiResponse(T data) | |
{ | |
StatusCode = HttpStatusCode.OK; | |
Data = data; | |
} | |
/// <summary> | |
/// Creates an ApiResponse object with the given status code and message | |
/// </summary> | |
/// <param name="statusCode">HttpStatusCode of the response.</param> | |
/// <param name="message">Any message for the client.</param> | |
public ApiResponse(HttpStatusCode statusCode, string message = null) | |
{ | |
StatusCode = statusCode; | |
Message = message; | |
} | |
/// <summary> | |
/// Creates a ApiResponse object with the given parameters. | |
/// </summary> | |
/// <param name="statusCode">HttpStatusCode of the response.</param> | |
/// <param name="data">Data to be returned to the client.</param> | |
/// <param name="message">Any message for the client.</param> | |
public ApiResponse(HttpStatusCode statusCode, T data = null, string message = null) | |
{ | |
StatusCode = statusCode; | |
Data = data; | |
Message = message; | |
} | |
} | |
public class ApiMessageResponse | |
{ | |
public string Message { get; set; } | |
public HttpStatusCode StatusCode { get; set; } | |
public bool Success => (int)StatusCode >= 200 && (int)StatusCode < 300; | |
/// <summary> | |
/// Creates an ApiResponse object with the given status code and message | |
/// </summary> | |
/// <param name="statusCode">HttpStatusCode of the response.</param> | |
/// <param name="message">Any message for the client.</param> | |
public ApiMessageResponse(HttpStatusCode statusCode, string message = null) | |
{ | |
StatusCode = statusCode; | |
Message = message; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment