diff --git a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs index 20d1b1215..584f7a0af 100644 --- a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs +++ b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs @@ -64,23 +64,26 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers { var numIds = ids.Split(',') .Select(id => (Ok: int.TryParse(id, out int x), Value: x)); + if (!numIds.All(nid => nid.Ok)) { return BadRequest("ids value invalid. Must be comma-separated list of numbers"); } - var idsToSelect = numIds.Select(id => id.Value); + var idsToSelect = numIds + .Select(id => id.Value); + var items = _catalogContext.CatalogItems.Where(ci => idsToSelect.Contains(ci.Id)).ToList(); items = ChangeUriPlaceholder(items); - return Ok(items); + return Ok(items); } [HttpGet] [Route("items/{id:int}")] [ProducesResponseType((int)HttpStatusCode.NotFound)] - [ProducesResponseType(typeof(CatalogItem),(int)HttpStatusCode.OK)] + [ProducesResponseType(typeof(CatalogItem), (int)HttpStatusCode.OK)] public async Task GetItemById(int id) { if (id <= 0) @@ -127,19 +130,44 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers return Ok(model); } - // GET api/v1/[controller]/items/type/1/brand/null[?pageSize=3&pageIndex=10] + // GET api/v1/[controller]/items/type/1/brand[?pageSize=3&pageIndex=10] [HttpGet] - [Route("[action]/type/{catalogTypeId}/brand/{catalogBrandId}")] + [Route("[action]/type/{catalogTypeId}/brand/{catalogBrandId:int?}")] [ProducesResponseType(typeof(PaginatedItemsViewModel), (int)HttpStatusCode.OK)] - public async Task Items(int? catalogTypeId, int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) + public async Task Items(int catalogTypeId, int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) { var root = (IQueryable)_catalogContext.CatalogItems; - if (catalogTypeId.HasValue) + root = root.Where(ci => ci.CatalogTypeId == catalogTypeId); + + if (catalogBrandId.HasValue) { - root = root.Where(ci => ci.CatalogTypeId == catalogTypeId); + root = root.Where(ci => ci.CatalogBrandId == catalogBrandId); } + var totalItems = await root + .LongCountAsync(); + + var itemsOnPage = await root + .Skip(pageSize * pageIndex) + .Take(pageSize) + .ToListAsync(); + + itemsOnPage = ChangeUriPlaceholder(itemsOnPage); + + var model = new PaginatedItemsViewModel( + pageIndex, pageSize, totalItems, itemsOnPage); + + return Ok(model); + } + // GET api/v1/[controller]/items/type/all/brand[?pageSize=3&pageIndex=10] + [HttpGet] + [Route("[action]/type/all/brand/{catalogBrandId:int?}")] + [ProducesResponseType(typeof(PaginatedItemsViewModel), (int)HttpStatusCode.OK)] + public async Task Items(int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) + { + var root = (IQueryable)_catalogContext.CatalogItems; + if (catalogBrandId.HasValue) { root = root.Where(ci => ci.CatalogBrandId == catalogBrandId); diff --git a/src/Web/WebMVC/Infrastructure/API.cs b/src/Web/WebMVC/Infrastructure/API.cs index d6f485e21..b601282e0 100644 --- a/src/Web/WebMVC/Infrastructure/API.cs +++ b/src/Web/WebMVC/Infrastructure/API.cs @@ -1,6 +1,4 @@ -using System; - -namespace WebMVC.Infrastructure +namespace WebMVC.Infrastructure { public static class API { @@ -55,11 +53,20 @@ namespace WebMVC.Infrastructure { var filterQs = ""; - if (brand.HasValue || type.HasValue) + if (type.HasValue) + { + var brandQs = (brand.HasValue) ? brand.Value.ToString() : string.Empty; + filterQs = $"/type/{type.Value}/brand/{brandQs}"; + + } + else if (brand.HasValue) + { + var brandQs = (brand.HasValue) ? brand.Value.ToString() : string.Empty; + filterQs = $"/type/all/brand/{brandQs}"; + } + else { - var brandQs = (brand.HasValue) ? brand.Value.ToString() : "null"; - var typeQs = (type.HasValue) ? type.Value.ToString() : "null"; - filterQs = $"/type/{typeQs}/brand/{brandQs}"; + filterQs = string.Empty; } return $"{baseUri}items{filterQs}?pageIndex={page}&pageSize={take}"; diff --git a/src/Web/WebSPA/Client/modules/catalog/catalog.service.ts b/src/Web/WebSPA/Client/modules/catalog/catalog.service.ts index fc5bc4c5e..3983c3cad 100644 --- a/src/Web/WebSPA/Client/modules/catalog/catalog.service.ts +++ b/src/Web/WebSPA/Client/modules/catalog/catalog.service.ts @@ -29,10 +29,14 @@ export class CatalogService { getCatalog(pageIndex: number, pageSize: number, brand: number, type: number): Observable { let url = this.catalogUrl; - if (brand || type) { - url = this.catalogUrl + '/type/' + ((type) ? type.toString() : 'null') + '/brand/' + ((brand) ? brand.toString() : 'null'); - } + if (type) { + url = this.catalogUrl + '/type/' + type.toString() + '/brand/' + ((brand) ? brand.toString() : ''); + } + else if (brand) { + url = this.catalogUrl + '/type/all' + '/brand/' + ((brand) ? brand.toString() : ''); + } + url = url + '?pageIndex=' + pageIndex + '&pageSize=' + pageSize; return this.service.get(url).map((response: Response) => {