X Tutup
Skip to content

Commit 906a17e

Browse files
Define ValidationErrorResult in SpaServices; use it in MusicStore
1 parent 78efc77 commit 906a17e

File tree

5 files changed

+44
-69
lines changed

5 files changed

+44
-69
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Microsoft.AspNet.Mvc;
4+
using Microsoft.AspNet.Mvc.ModelBinding;
5+
6+
namespace Microsoft.AspNet.SpaServices
7+
{
8+
public class ValidationErrorResult : ObjectResult {
9+
public const int DefaultStatusCode = 400;
10+
11+
public ValidationErrorResult(ModelStateDictionary modelState, int errorStatusCode = DefaultStatusCode)
12+
: base(CreateResultObject(modelState))
13+
{
14+
if (!modelState.IsValid) {
15+
this.StatusCode = errorStatusCode;
16+
}
17+
}
18+
19+
private static IDictionary<string, IEnumerable<string>> CreateResultObject(ModelStateDictionary modelState)
20+
{
21+
if (modelState.IsValid) {
22+
return null;
23+
} else {
24+
return modelState
25+
.Where(m => m.Value.Errors.Any())
26+
.ToDictionary(m => m.Key, m => m.Value.Errors.Select(me => me.ErrorMessage));
27+
}
28+
}
29+
}
30+
}

Microsoft.AspNet.SpaServices/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"projectUrl": "",
1111
"licenseUrl": "",
1212
"dependencies": {
13+
"Microsoft.AspNet.Mvc": "6.0.0-rc1-*",
1314
"Microsoft.AspNet.Routing": "1.0.0-rc1-*"
1415
},
1516
"frameworks": {

samples/angular/MusicStore/Apis/AlbumsApiController.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using AutoMapper;
77
using MusicStore.Models;
88
using MusicStore.Infrastructure;
9+
using Microsoft.AspNet.SpaServices;
910

1011
namespace MusicStore.Apis
1112
{
@@ -95,7 +96,7 @@ public async Task<ActionResult> CreateAlbum([FromBody]AlbumChangeDto album)
9596
if (!ModelState.IsValid)
9697
{
9798
// Return the model errors
98-
return new ApiResult(ModelState);
99+
return new ValidationErrorResult(ModelState);
99100
}
100101

101102
// Save the changes to the DB
@@ -105,11 +106,10 @@ public async Task<ActionResult> CreateAlbum([FromBody]AlbumChangeDto album)
105106

106107
// TODO: Handle missing record, key violations, concurrency issues, etc.
107108

108-
return new ApiResult
109-
{
109+
return new ObjectResult(new {
110110
Data = dbAlbum.AlbumId,
111111
Message = "Album created successfully."
112-
};
112+
});
113113
}
114114

115115
[HttpPut("{albumId:int}/update")]
@@ -118,18 +118,16 @@ public async Task<ActionResult> UpdateAlbum(int albumId, [FromBody] AlbumChangeD
118118
if (!ModelState.IsValid)
119119
{
120120
// Return the model errors
121-
return new ApiResult(ModelState);
121+
return new ValidationErrorResult(ModelState);
122122
}
123123

124124
var dbAlbum = await _storeContext.Albums.SingleOrDefaultAsync(a => a.AlbumId == albumId);
125125

126126
if (dbAlbum == null)
127127
{
128-
return new ApiResult
129-
{
130-
StatusCode = 404,
128+
return new ObjectResult(new {
131129
Message = string.Format("The album with ID {0} was not found.", albumId)
132-
};
130+
}) { StatusCode = 404 };
133131
}
134132

135133
// Save the changes to the DB
@@ -138,10 +136,9 @@ public async Task<ActionResult> UpdateAlbum(int albumId, [FromBody] AlbumChangeD
138136

139137
// TODO: Handle missing record, key violations, concurrency issues, etc.
140138

141-
return new ApiResult
142-
{
139+
return new ObjectResult (new {
143140
Message = "Album updated successfully."
144-
};
141+
});
145142
}
146143

147144
[HttpDelete("{albumId:int}")]
@@ -161,10 +158,9 @@ public async Task<ActionResult> DeleteAlbum(int albumId)
161158
// TODO: Handle missing record, key violations, concurrency issues, etc.
162159
}
163160

164-
return new ApiResult
165-
{
161+
return new ObjectResult (new {
166162
Message = "Album deleted successfully."
167-
};
163+
});
168164
}
169165
}
170166

samples/angular/MusicStore/Infrastructure/ApiResult.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

samples/angular/MusicStore/wwwroot/ng-app/components/admin/album-edit/album-edit.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class AlbumEdit {
7171
if (response.status === 200) {
7272
this.changesSaved = true;
7373
} else {
74-
var errors = (<ValidationResponse>(response.json())).ModelErrors;
74+
var errors = <ValidationResponse>(response.json());
7575
Object.keys(errors).forEach(key => {
7676
errors[key].forEach(errorMessage => {
7777
// TODO: There has to be a better API for this
@@ -101,6 +101,5 @@ export class AlbumEdit {
101101
}
102102

103103
interface ValidationResponse {
104-
Message: string;
105-
ModelErrors: { [key: string]: string[] };
104+
[propertyName: string]: string[];
106105
}

0 commit comments

Comments
 (0)
X Tutup