/// <summary> /// This allows you to return a CreatedAtRoute result for a Create /// </summary> /// <param name="status"></param> /// <param name="controller"></param> /// <param name="routeName">The values needed to work with the HttpGet to return the correct item</param> /// <param name="routeValues"></param> /// <param name="dto"></param> /// <returns></returns> public static ActionResult <T> Response <T>(this GenericBizRunner.IStatusGeneric status, ControllerBase controller, string routeName, object routeValues, T dto) { if (!status.HasErrors) { return(controller.CreatedAtRoute(routeName, routeValues, dto)); } //it has errors return(CreateBadRequestObjectResult(status.Errors)); }
/// <summary> /// This copies errors for general display where we are not returning to a page with the fields on them /// </summary> /// <param name="status"></param> /// <param name="modelState"></param> public static void CopyErrorsToModelState(this GenericBizRunner.IStatusGeneric status, ModelStateDictionary modelState) { if (!status.HasErrors) { return; } foreach (var error in status.Errors) { modelState.AddModelError("", error.ErrorMessage); } }
/// <summary> /// If the status has no errors then it will HTTP response with the status code provided in the /// validStatusCode property and the message from the status /// otherwise it will returns a HTTP 400 with the error information in the standard WebAPI format /// </summary> /// <param name="status"></param> /// <param name="validStatusCode">HTTP status code for non-error status</param> /// <returns></returns> public static ActionResult <WebApiMessageOnly> ResponseWithValidCode(this GenericBizRunner.IStatusGeneric status, int validStatusCode) { if (!status.HasErrors) { return new ObjectResult(new WebApiMessageOnly(status)) { StatusCode = validStatusCode } } ; //it has errors return(CreateBadRequestObjectResult(status.Errors)); }
/// <summary> /// This transfers error messages from the ValidationResult collection to the MVC modelState error dictionary. /// It looks for errors that have member names corresponding to the properties in the displayDto. /// This means that errors associated with a field on display will show next to the name. /// Other errors will be shown in the ValidationSummary /// </summary> /// <param name="status">The status that came back from the BizRunner</param> /// <param name="modelState">The MVC modelState to add errors to</param> /// <param name="displayDto">This is the Dto that will be used to display the error messages</param> /// <param name="modelName">When using razor pages you need to prefix the member name by the name of the model's property</param> public static void CopyErrorsToModelState <T>(this GenericBizRunner.IStatusGeneric status, ModelStateDictionary modelState, T displayDto, string modelName = null) { if (!status.HasErrors) { return; } if (displayDto == null) { status.CopyErrorsToModelState(modelState); return; } CopyErrorsWithFilterOnDto(status.Errors, modelState, displayDto, modelName); }
/// <summary> /// This is used to create a Message-plus-results response from GenericBizRunner /// </summary> /// <param name="status"></param> public WebApiMessageAndResult(GenericBizRunner.IStatusGeneric status, T results) { Message = status.Message; Results = results; }
/// <summary> /// This will return a result value, with the status Message /// 1. If there are no errors and the result is not null it will return a HTTP response with the status code provided /// in the validStatusCode property, plus a json containing the message from the status and the results object /// 2. If there are no errors but result is null it will return a HTTP 204 (NoContent) with the status Message /// 3. If there are errors it returns a HTTP 400 with the error information in the standard WebAPI format /// </summary> /// <typeparam name="T"></typeparam> /// <param name="status"></param> /// <param name="results"></param> /// <param name="validStatusCode">The status code to return when the status has no errors and the result is not null</param> /// <param name="nullResultStatusCode">Optional, default is 204: The status code to return if the there ar no errors, but the result is null</param> /// <returns></returns> public static ActionResult <WebApiMessageAndResult <T> > ResponseWithValidCode <T>(this GenericBizRunner.IStatusGeneric status, T results, int validStatusCode, int nullResultStatusCode = ResultIsNullStatusCode) { if (!status.HasErrors) { return new ObjectResult(new WebApiMessageAndResult <T>(status, results)) { StatusCode = results == null ? nullResultStatusCode : validStatusCode } } ; //it has errors return(CreateBadRequestObjectResult(status.Errors)); }
/// <summary> /// This will return a result value, with the status Message /// 1. If there are no errors and the result is not null it will return a HTTP 200 response /// plus a json containing the message from the status and the results object /// 2. If there are no errors but result is null it will return a HTTP 204 (NoContent) with the status Message /// 3. If there are errors it returns a HTTP 400 with the error information in the standard WebAPI format /// </summary> /// <typeparam name="T"></typeparam> /// <param name="status"></param> /// <param name="results"></param> /// <returns></returns> public static ActionResult <WebApiMessageAndResult <T> > Response <T>(this GenericBizRunner.IStatusGeneric status, T results) { return(status.ResponseWithValidCode(results, OkStatusCode)); }
//----------------------------------------------------- // -- Now the GenericBizRunner versions /// <summary> /// This will return a HTTP 200 with the status message if Valid, /// otherwise it will returns a HTTP 400 with the error information in the standard WebAPI format /// </summary> /// <param name="status"></param> /// <returns></returns> public static ActionResult <WebApiMessageOnly> Response(this GenericBizRunner.IStatusGeneric status) { return(status.ResponseWithValidCode(OkStatusCode)); }
public static void CheckResponseWithValidCode <T>(this ActionResult <WebApiMessageAndResult <T> > actionResult, GenericBizRunner.IStatusGeneric status, T results, int validStatusCode, int nullResultStatusCode = CreateResponse.ResultIsNullStatusCode) { actionResult.ShouldNotBeNull(); var result = actionResult.Result as ObjectResult; if (!status.HasErrors) { Assert.NotNull(result); result.ShouldNotBeNull(); result.StatusCode.ShouldEqual(results == null ? nullResultStatusCode : validStatusCode); var returnClass = result.Value as WebApiMessageOnly; returnClass?.Message.ShouldEqual(status.Message); } else { CheckErrorResponse(result as BadRequestObjectResult, status.Errors); } }
public static void CheckResponse <T>(this ActionResult <WebApiMessageAndResult <T> > actionResult, GenericBizRunner.IStatusGeneric status, T results) { actionResult.CheckResponseWithValidCode <T>(status, results, CreateResponse.OkStatusCode); }
//---------------------------------------------------- //Now the GenericBixRunner public static void CheckResponse(this ActionResult <WebApiMessageOnly> actionResult, GenericBizRunner.IStatusGeneric status) { actionResult.CheckResponseWithValidCode(status, CreateResponse.OkStatusCode); }
/// <summary> /// This is used to create a Message-only response from GenericBizRunner /// </summary> /// <param name="status"></param> public WebApiMessageOnly(GenericBizRunner.IStatusGeneric status) { Message = status.Message; }