Initial state

This commit is contained in:
SteveSandersonMS
2015-11-02 10:30:36 -08:00
parent 0e1fa2e09d
commit f693bd60e3
110 changed files with 6722 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.ModelBinding;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MusicStore.Infrastructure
{
public class ApiResult : ActionResult
{
public ApiResult(ModelStateDictionary modelState)
: this()
{
if (modelState.Any(m => m.Value.Errors.Count > 0))
{
StatusCode = 400;
Message = "The model submitted was invalid. Please correct the specified errors and try again.";
ModelErrors = modelState
.SelectMany(m => m.Value.Errors.Select(me => new ModelError
{
FieldName = m.Key,
ErrorMessage = me.ErrorMessage
}));
}
}
public ApiResult()
{
}
[JsonIgnore]
public int? StatusCode { get; set; }
public string Message { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public object Data { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public IEnumerable<ModelError> ModelErrors { get; set; }
public override Task ExecuteResultAsync(ActionContext context)
{
if (StatusCode.HasValue)
{
context.HttpContext.Response.StatusCode = StatusCode.Value;
}
var json = new JsonResult(this);
return json.ExecuteResultAsync(context);
}
public class ModelError
{
public string FieldName { get; set; }
public string ErrorMessage { get; set; }
}
}
}