using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace iiie.WebApiUtils.BusinessManager
{
    [AttributeUsage(AttributeTargets.Method)]
    public class CheckModelForNullAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var failed = actionContext.ActionArguments.Where(x => x.Value == null);
            if (failed.Any())
            {
                var failedString = string.Join(", ", failed.Select(x => x.Key));
                actionContext.Response = actionContext.Request.CreateErrorResponse(
                    HttpStatusCode.BadRequest, "Missing parameters: " + failedString);
            }
        }
    }
}
