Browse Source

Fix issue 685

pull/693/head
Unai Zorrilla Castro 6 years ago
parent
commit
5c06f6e500
3 changed files with 57 additions and 18 deletions
  1. +36
    -8
      src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs
  2. +14
    -7
      src/Web/WebMVC/Infrastructure/API.cs
  3. +7
    -3
      src/Web/WebSPA/Client/modules/catalog/catalog.service.ts

+ 36
- 8
src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs View File

@ -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<IActionResult> 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<CatalogItem>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> Items(int? catalogTypeId, int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0)
public async Task<IActionResult> Items(int catalogTypeId, int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0)
{
var root = (IQueryable<CatalogItem>)_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<CatalogItem>(
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<CatalogItem>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> Items(int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0)
{
var root = (IQueryable<CatalogItem>)_catalogContext.CatalogItems;
if (catalogBrandId.HasValue)
{
root = root.Where(ci => ci.CatalogBrandId == catalogBrandId);


+ 14
- 7
src/Web/WebMVC/Infrastructure/API.cs View File

@ -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}";


+ 7
- 3
src/Web/WebSPA/Client/modules/catalog/catalog.service.ts View File

@ -29,10 +29,14 @@ export class CatalogService {
getCatalog(pageIndex: number, pageSize: number, brand: number, type: number): Observable<ICatalog> {
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) => {


Loading…
Cancel
Save