mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-22 17:47:53 +00:00
Define ValidationErrorResult in SpaServices; use it in MusicStore
This commit is contained in:
30
Microsoft.AspNet.SpaServices/ValidationErrorResult.cs
Normal file
30
Microsoft.AspNet.SpaServices/ValidationErrorResult.cs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.SpaServices
|
||||||
|
{
|
||||||
|
public class ValidationErrorResult : ObjectResult {
|
||||||
|
public const int DefaultStatusCode = 400;
|
||||||
|
|
||||||
|
public ValidationErrorResult(ModelStateDictionary modelState, int errorStatusCode = DefaultStatusCode)
|
||||||
|
: base(CreateResultObject(modelState))
|
||||||
|
{
|
||||||
|
if (!modelState.IsValid) {
|
||||||
|
this.StatusCode = errorStatusCode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IDictionary<string, IEnumerable<string>> CreateResultObject(ModelStateDictionary modelState)
|
||||||
|
{
|
||||||
|
if (modelState.IsValid) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return modelState
|
||||||
|
.Where(m => m.Value.Errors.Any())
|
||||||
|
.ToDictionary(m => m.Key, m => m.Value.Errors.Select(me => me.ErrorMessage));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
"projectUrl": "",
|
"projectUrl": "",
|
||||||
"licenseUrl": "",
|
"licenseUrl": "",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"Microsoft.AspNet.Mvc": "6.0.0-rc1-*",
|
||||||
"Microsoft.AspNet.Routing": "1.0.0-rc1-*"
|
"Microsoft.AspNet.Routing": "1.0.0-rc1-*"
|
||||||
},
|
},
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using Microsoft.Data.Entity;
|
|||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
using MusicStore.Infrastructure;
|
using MusicStore.Infrastructure;
|
||||||
|
using Microsoft.AspNet.SpaServices;
|
||||||
|
|
||||||
namespace MusicStore.Apis
|
namespace MusicStore.Apis
|
||||||
{
|
{
|
||||||
@@ -95,7 +96,7 @@ namespace MusicStore.Apis
|
|||||||
if (!ModelState.IsValid)
|
if (!ModelState.IsValid)
|
||||||
{
|
{
|
||||||
// Return the model errors
|
// Return the model errors
|
||||||
return new ApiResult(ModelState);
|
return new ValidationErrorResult(ModelState);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the changes to the DB
|
// Save the changes to the DB
|
||||||
@@ -105,11 +106,10 @@ namespace MusicStore.Apis
|
|||||||
|
|
||||||
// TODO: Handle missing record, key violations, concurrency issues, etc.
|
// TODO: Handle missing record, key violations, concurrency issues, etc.
|
||||||
|
|
||||||
return new ApiResult
|
return new ObjectResult(new {
|
||||||
{
|
|
||||||
Data = dbAlbum.AlbumId,
|
Data = dbAlbum.AlbumId,
|
||||||
Message = "Album created successfully."
|
Message = "Album created successfully."
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{albumId:int}/update")]
|
[HttpPut("{albumId:int}/update")]
|
||||||
@@ -118,18 +118,16 @@ namespace MusicStore.Apis
|
|||||||
if (!ModelState.IsValid)
|
if (!ModelState.IsValid)
|
||||||
{
|
{
|
||||||
// Return the model errors
|
// Return the model errors
|
||||||
return new ApiResult(ModelState);
|
return new ValidationErrorResult(ModelState);
|
||||||
}
|
}
|
||||||
|
|
||||||
var dbAlbum = await _storeContext.Albums.SingleOrDefaultAsync(a => a.AlbumId == albumId);
|
var dbAlbum = await _storeContext.Albums.SingleOrDefaultAsync(a => a.AlbumId == albumId);
|
||||||
|
|
||||||
if (dbAlbum == null)
|
if (dbAlbum == null)
|
||||||
{
|
{
|
||||||
return new ApiResult
|
return new ObjectResult(new {
|
||||||
{
|
|
||||||
StatusCode = 404,
|
|
||||||
Message = string.Format("The album with ID {0} was not found.", albumId)
|
Message = string.Format("The album with ID {0} was not found.", albumId)
|
||||||
};
|
}) { StatusCode = 404 };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the changes to the DB
|
// Save the changes to the DB
|
||||||
@@ -138,10 +136,9 @@ namespace MusicStore.Apis
|
|||||||
|
|
||||||
// TODO: Handle missing record, key violations, concurrency issues, etc.
|
// TODO: Handle missing record, key violations, concurrency issues, etc.
|
||||||
|
|
||||||
return new ApiResult
|
return new ObjectResult (new {
|
||||||
{
|
|
||||||
Message = "Album updated successfully."
|
Message = "Album updated successfully."
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{albumId:int}")]
|
[HttpDelete("{albumId:int}")]
|
||||||
@@ -161,10 +158,9 @@ namespace MusicStore.Apis
|
|||||||
// TODO: Handle missing record, key violations, concurrency issues, etc.
|
// TODO: Handle missing record, key violations, concurrency issues, etc.
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ApiResult
|
return new ObjectResult (new {
|
||||||
{
|
|
||||||
Message = "Album deleted successfully."
|
Message = "Album deleted successfully."
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
using Microsoft.AspNet.Mvc;
|
|
||||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
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.Any()))
|
|
||||||
{
|
|
||||||
StatusCode = 400;
|
|
||||||
Message = "The model submitted was invalid. Please correct the specified errors and try again.";
|
|
||||||
ModelErrors = modelState
|
|
||||||
.Where(m => m.Value.Errors.Any())
|
|
||||||
.ToDictionary(m => m.Key, m => m.Value.Errors.Select(me => 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 IDictionary<string, IEnumerable<string>> ModelErrors { get; set; }
|
|
||||||
|
|
||||||
public override Task ExecuteResultAsync(ActionContext context)
|
|
||||||
{
|
|
||||||
if (StatusCode.HasValue)
|
|
||||||
{
|
|
||||||
context.HttpContext.Response.StatusCode = StatusCode.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ObjectResult(this).ExecuteResultAsync(context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -71,7 +71,7 @@ export class AlbumEdit {
|
|||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
this.changesSaved = true;
|
this.changesSaved = true;
|
||||||
} else {
|
} else {
|
||||||
var errors = (<ValidationResponse>(response.json())).ModelErrors;
|
var errors = <ValidationResponse>(response.json());
|
||||||
Object.keys(errors).forEach(key => {
|
Object.keys(errors).forEach(key => {
|
||||||
errors[key].forEach(errorMessage => {
|
errors[key].forEach(errorMessage => {
|
||||||
// TODO: There has to be a better API for this
|
// TODO: There has to be a better API for this
|
||||||
@@ -101,6 +101,5 @@ export class AlbumEdit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface ValidationResponse {
|
interface ValidationResponse {
|
||||||
Message: string;
|
[propertyName: string]: string[];
|
||||||
ModelErrors: { [key: string]: string[] };
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user