using IRaCIS.Core.Infrastructure.Extention;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;


namespace IRaCIS.Core.Application.Filter
{
    public class ModelActionFilter : ActionFilterAttribute, IActionFilter
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {

                var validationErrors = context.ModelState
                                              .Keys
                                              .SelectMany(k => context.ModelState[k]!.Errors)
                                              .Select(e => e.ErrorMessage)
                                              .ToArray();
     
                context.Result = new JsonResult(ResponseOutput.NotOk("提供给接口的参数无效。" +JsonConvert.SerializeObject( validationErrors)));
            }
        }
    }

}
