namespace Ds.Modules.DsEntity.helper { public class ServiceResponse { private ServiceResponse(int statusCode, List errors) { StatusCode = statusCode; Errors = errors; } private ServiceResponse(Exception ex) { Errors = new List { ex.Message.ToString() }; } private ServiceResponse(int statusCode, T data) { StatusCode = statusCode; Data = data; } public bool Success { get { return Errors == null || Errors.Count == 0; } } public T Data { get; set; } public int StatusCode { get; set; } = 200; public List Errors { get; set; } = new List(); public static ServiceResponse ReturnException(Exception ex) { return new ServiceResponse(ex); } public static ServiceResponse ReturnFailed(int statusCode, List errors) { return new ServiceResponse(statusCode, errors); } public static ServiceResponse ReturnFailed(int statusCode, string errorMessage) { return new ServiceResponse(statusCode, new List { errorMessage }); } public static ServiceResponse ReturnSuccess() { return new ServiceResponse(200, null); } public static ServiceResponse ReturnResultWith200(T data) { return new ServiceResponse(200, data); } public static ServiceResponse ReturnResultWith201(T data) { return new ServiceResponse(201, data); } public static ServiceResponse ReturnResultWith204() { return new ServiceResponse(204, null); } public static ServiceResponse Return500() { return new ServiceResponse(500, new List { "An unexpected fault happened. Try again later." }); } public static ServiceResponse Return409(string message) { return new ServiceResponse(409, new List { message }); } public static ServiceResponse Return422(string message) { return new ServiceResponse(422, new List { message }); } public static ServiceResponse Return404() { return new ServiceResponse(404, new List { "Not Found" }); } public static ServiceResponse Return404(string message) { return new ServiceResponse(404, new List { message }); } public static ServiceResponse Return400(string message) { return new ServiceResponse(400, new List { message }); } } }