diff --git a/src/MusicStore.Spa/Apis/AlbumsApiController.cs b/src/MusicStore.Spa/Apis/AlbumsApiController.cs index 3193370fd4..5b5e78a603 100644 --- a/src/MusicStore.Spa/Apis/AlbumsApiController.cs +++ b/src/MusicStore.Spa/Apis/AlbumsApiController.cs @@ -19,43 +19,37 @@ namespace MusicStore.Apis //[Route("api/albums")] public async Task Paged(int page = 1, int pageSize = 50, string sortBy = null) { - var pagedAlbums = await _storeContext.Albums + var albums = await _storeContext.Albums //.Include(a => a.Genre) //.Include(a => a.Artist) .SortBy(sortBy, a => a.Title) .ToPagedListAsync(page, pageSize); - return new SmartJsonResult - { - Data = pagedAlbums - }; + return Json(albums); } //[Route("api/albums/all")] public async Task All() { - return new SmartJsonResult - { - Data = await _storeContext.Albums - //.Include(a => a.Genre) - //.Include(a => a.Artist) - .OrderBy(a => a.Title) - .ToListAsync() - }; + var albums = await _storeContext.Albums + //.Include(a => a.Genre) + //.Include(a => a.Artist) + .OrderBy(a => a.Title) + .ToListAsync(); + + return Json(albums); } //[Route("api/albums/mostPopular")] public async Task MostPopular(int count = 6) { count = count > 0 && count < 20 ? count : 6; + var albums = await _storeContext.Albums + .OrderByDescending(a => a.OrderDetails.Count()) + .Take(count) + .ToListAsync(); - return new SmartJsonResult - { - Data = await _storeContext.Albums - .OrderByDescending(a => a.OrderDetails.Count()) - .Take(count) - .ToListAsync() - }; + return Json(albums); } //[Route("api/albums/{albumId:int}")] @@ -73,10 +67,7 @@ namespace MusicStore.Apis // TODO: Add null checking and return 404 in that case - return new SmartJsonResult - { - Data = album - }; + return Json(album); } //[Route("api/albums")] diff --git a/src/MusicStore.Spa/Apis/ArtistsApiController.cs b/src/MusicStore.Spa/Apis/ArtistsApiController.cs index ff6b34f49c..2f8420c77b 100644 --- a/src/MusicStore.Spa/Apis/ArtistsApiController.cs +++ b/src/MusicStore.Spa/Apis/ArtistsApiController.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNet.Mvc; @@ -19,10 +18,11 @@ namespace MusicStore.Apis //[Route("api/artists/lookup")] public async Task Lookup() { - return new SmartJsonResult - { - Data = await _storeContext.Artists.OrderBy(a => a.Name).ToListAsync() - }; + var artists = await _storeContext.Artists + .OrderBy(a => a.Name) + .ToListAsync(); + + return Json(artists); } } } \ No newline at end of file diff --git a/src/MusicStore.Spa/Apis/GenresApiController.cs b/src/MusicStore.Spa/Apis/GenresApiController.cs index 5ddd30d85d..89e0fe22a0 100644 --- a/src/MusicStore.Spa/Apis/GenresApiController.cs +++ b/src/MusicStore.Spa/Apis/GenresApiController.cs @@ -17,12 +17,11 @@ namespace MusicStore.Apis //[Route("api/genres/lookup")] public async Task Lookup() { - return new SmartJsonResult - { - Data = await _storeContext.Genres - .Select(g => new { g.GenreId, g.Name }) - .ToListAsync() - }; + var genres = await _storeContext.Genres + .Select(g => new { g.GenreId, g.Name }) + .ToListAsync(); + + return Json(genres); } //[Route("api/genres/menu")] @@ -30,25 +29,25 @@ namespace MusicStore.Apis { count = count > 0 && count < 20 ? count : 9; - return new SmartJsonResult - { - Data = await _storeContext.Genres - .OrderByDescending(g => g.Albums.Sum(a => a.OrderDetails.Sum(od => od.Quantity))) - .Take(count) - .ToListAsync() - }; + var genres = await _storeContext.Genres + .OrderByDescending(g => + g.Albums.Sum(a => + a.OrderDetails.Sum(od => od.Quantity))) + .Take(count) + .ToListAsync(); + + return Json(genres); } //[Route("api/genres")] public async Task GenreList() { - return new SmartJsonResult - { - Data = await _storeContext.Genres - //.Include(g => g.Albums) - .OrderBy(g => g.Name) - .ToListAsync() - }; + var genres = await _storeContext.Genres + //.Include(g => g.Albums) + .OrderBy(g => g.Name) + .ToListAsync(); + + return Json(genres); } //[Route("api/genres/{genreId:int}/albums")] @@ -61,10 +60,7 @@ namespace MusicStore.Apis //.OrderBy(a => a.Genre.Name) .ToListAsync(); - return new SmartJsonResult - { - Data = albums - }; + return Json(albums); } } } \ No newline at end of file diff --git a/src/MusicStore.Spa/Infrastructure/ApiResult.cs b/src/MusicStore.Spa/Infrastructure/ApiResult.cs index dbdb9e1ac5..f43848d021 100644 --- a/src/MusicStore.Spa/Infrastructure/ApiResult.cs +++ b/src/MusicStore.Spa/Infrastructure/ApiResult.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.ModelBinding; using Newtonsoft.Json; @@ -18,10 +17,10 @@ namespace Microsoft.AspNet.Mvc 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 - })); + { + FieldName = m.Key, + ErrorMessage = me.ErrorMessage + })); } } @@ -43,11 +42,12 @@ namespace Microsoft.AspNet.Mvc public override void ExecuteResult(ActionContext context) { - var json = new SmartJsonResult + if (StatusCode.HasValue) { - StatusCode = StatusCode, - Data = this - }; + context.HttpContext.Response.StatusCode = StatusCode.Value; + } + + var json = new JsonResult(this); json.ExecuteResult(context); }