2020-06-08 16:40:58 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using BTCPayServer.Client.Models;
|
2020-06-03 11:58:49 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-06-08 16:40:58 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
2020-06-03 11:58:49 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers.GreenField
|
|
|
|
{
|
|
|
|
public static class GreenFieldUtils
|
|
|
|
{
|
2020-06-08 16:40:58 +02:00
|
|
|
public static IActionResult CreateValidationError(this ControllerBase controller, ModelStateDictionary modelState)
|
2021-04-26 05:37:56 +02:00
|
|
|
{
|
|
|
|
return controller.UnprocessableEntity(modelState.ToGreenfieldValidationError());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<GreenfieldValidationError> ToGreenfieldValidationError(this ModelStateDictionary modelState)
|
2020-06-03 11:58:49 +02:00
|
|
|
{
|
2020-06-08 16:40:58 +02:00
|
|
|
List<GreenfieldValidationError> errors = new List<GreenfieldValidationError>();
|
|
|
|
foreach (var error in modelState)
|
|
|
|
{
|
|
|
|
foreach (var errorMessage in error.Value.Errors)
|
|
|
|
{
|
|
|
|
errors.Add(new GreenfieldValidationError(error.Key, errorMessage.ErrorMessage));
|
|
|
|
}
|
|
|
|
}
|
2021-04-26 05:37:56 +02:00
|
|
|
|
|
|
|
return errors;
|
2020-06-03 11:58:49 +02:00
|
|
|
}
|
2021-04-26 05:37:56 +02:00
|
|
|
|
2020-06-08 16:40:58 +02:00
|
|
|
public static IActionResult CreateAPIError(this ControllerBase controller, string errorCode, string errorMessage)
|
2020-06-03 11:58:49 +02:00
|
|
|
{
|
2020-06-08 16:40:58 +02:00
|
|
|
return controller.BadRequest(new GreenfieldAPIError(errorCode, errorMessage));
|
2020-06-03 11:58:49 +02:00
|
|
|
}
|
2021-04-08 08:57:01 +02:00
|
|
|
public static IActionResult CreateAPIError(this ControllerBase controller, int httpCode, string errorCode, string errorMessage)
|
|
|
|
{
|
|
|
|
return controller.StatusCode(httpCode, new GreenfieldAPIError(errorCode, errorMessage));
|
|
|
|
}
|
2020-06-03 11:58:49 +02:00
|
|
|
}
|
|
|
|
}
|