using System.Collections.Generic; using BTCPayServer.Client.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; namespace BTCPayServer.Controllers.GreenField { public static class GreenFieldUtils { public static IActionResult CreateValidationError(this ControllerBase controller, ModelStateDictionary modelState) { return controller.UnprocessableEntity(modelState.ToGreenfieldValidationError()); } public static List ToGreenfieldValidationError(this ModelStateDictionary modelState) { List errors = new List(); foreach (var error in modelState) { foreach (var errorMessage in error.Value.Errors) { errors.Add(new GreenfieldValidationError(error.Key, errorMessage.ErrorMessage)); } } return errors; } public static IActionResult CreateAPIError(this ControllerBase controller, string errorCode, string errorMessage) { return controller.BadRequest(new GreenfieldAPIError(errorCode, errorMessage)); } public static IActionResult CreateAPIError(this ControllerBase controller, int httpCode, string errorCode, string errorMessage) { return controller.StatusCode(httpCode, new GreenfieldAPIError(errorCode, errorMessage)); } } }