/// <summary>
        /// Formats postive responses from UseCases into APIResponses
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="controller"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static IActionResult StandardResponse <T>(this Microsoft.AspNetCore.Mvc.Controller controller, T result) where T : class
        {
            var apiResponse = new APIResponse <T>(result);

            //Use Extension Method to set a statusCode as well as an object
            return(controller.StatusCode(apiResponse.StatusCode, apiResponse));
        }
        /// <summary>
        /// Convert the status code, content of HttpResponseMessage to IActionResult,
        /// and copy the headers from response to HttpContext.Response.Headers.
        /// </summary>
        /// <param name="controller">The HttpResponse instance.</param>
        /// <param name="responseMessage">The HttpResponseMessage instance.</param>
        /// <returns>The action result.</returns>
        public static async Task <IActionResult> GetActionResultAsync(this Controller controller, HttpResponseMessage responseMessage)
        {
            controller.CopyResponseHeaders(responseMessage.Headers);

            var statusCode = (int)responseMessage.StatusCode;

            if (responseMessage.Content == null)
            {
                return(controller.StatusCode(statusCode));
            }
            else
            {
                var responseBody = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

                return(controller.StatusCode(statusCode, responseBody));
            }
        }
        /// <summary>
        /// Convert exception to action result.
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="exception">The exception.</param>
        /// <returns>The action result.</returns>
        public static IActionResult Exception(this Controller controller, Exception exception)
        {
            IActionResult result;

            if (exception is ServiceException e)
            {
                controller.CopyResponseHeaders(e.ResponseHeaders);

                int statusCode = (int)e.StatusCode;

                result = statusCode >= 300
                    ? controller.StatusCode(statusCode, e.ToString())
                    : controller.StatusCode((int)HttpStatusCode.InternalServerError, e.ToString());
            }
            else
            {
                result = controller.StatusCode((int)HttpStatusCode.InternalServerError, exception.ToString());
            }

            return(result);
        }
Example #4
0
        public static IActionResult ExecuteStandardResponse <T>(this Microsoft.AspNetCore.Mvc.Controller controller, IExecuteWrapper <T> result) where T  : class
        {
            var apiResponse = new APIResponse <T>(result);

            return(controller.StatusCode(apiResponse.StatusCode, apiResponse));
        }