Fix issue 685
This commit is contained in:
parent
3f225e4be4
commit
5c06f6e500
@ -64,23 +64,26 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
{
|
{
|
||||||
var numIds = ids.Split(',')
|
var numIds = ids.Split(',')
|
||||||
.Select(id => (Ok: int.TryParse(id, out int x), Value: x));
|
.Select(id => (Ok: int.TryParse(id, out int x), Value: x));
|
||||||
|
|
||||||
if (!numIds.All(nid => nid.Ok))
|
if (!numIds.All(nid => nid.Ok))
|
||||||
{
|
{
|
||||||
return BadRequest("ids value invalid. Must be comma-separated list of numbers");
|
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();
|
var items = _catalogContext.CatalogItems.Where(ci => idsToSelect.Contains(ci.Id)).ToList();
|
||||||
|
|
||||||
items = ChangeUriPlaceholder(items);
|
items = ChangeUriPlaceholder(items);
|
||||||
return Ok(items);
|
|
||||||
|
|
||||||
|
return Ok(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("items/{id:int}")]
|
[Route("items/{id:int}")]
|
||||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||||
[ProducesResponseType(typeof(CatalogItem),(int)HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(CatalogItem), (int)HttpStatusCode.OK)]
|
||||||
public async Task<IActionResult> GetItemById(int id)
|
public async Task<IActionResult> GetItemById(int id)
|
||||||
{
|
{
|
||||||
if (id <= 0)
|
if (id <= 0)
|
||||||
@ -127,19 +130,44 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
return Ok(model);
|
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]
|
[HttpGet]
|
||||||
[Route("[action]/type/{catalogTypeId}/brand/{catalogBrandId}")]
|
[Route("[action]/type/{catalogTypeId}/brand/{catalogBrandId:int?}")]
|
||||||
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)]
|
[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;
|
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)
|
if (catalogBrandId.HasValue)
|
||||||
{
|
{
|
||||||
root = root.Where(ci => ci.CatalogBrandId == catalogBrandId);
|
root = root.Where(ci => ci.CatalogBrandId == catalogBrandId);
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
using System;
|
namespace WebMVC.Infrastructure
|
||||||
|
|
||||||
namespace WebMVC.Infrastructure
|
|
||||||
{
|
{
|
||||||
public static class API
|
public static class API
|
||||||
{
|
{
|
||||||
@ -55,11 +53,20 @@ namespace WebMVC.Infrastructure
|
|||||||
{
|
{
|
||||||
var filterQs = "";
|
var filterQs = "";
|
||||||
|
|
||||||
if (brand.HasValue || type.HasValue)
|
if (type.HasValue)
|
||||||
{
|
{
|
||||||
var brandQs = (brand.HasValue) ? brand.Value.ToString() : "null";
|
var brandQs = (brand.HasValue) ? brand.Value.ToString() : string.Empty;
|
||||||
var typeQs = (type.HasValue) ? type.Value.ToString() : "null";
|
filterQs = $"/type/{type.Value}/brand/{brandQs}";
|
||||||
filterQs = $"/type/{typeQs}/brand/{brandQs}";
|
|
||||||
|
}
|
||||||
|
else if (brand.HasValue)
|
||||||
|
{
|
||||||
|
var brandQs = (brand.HasValue) ? brand.Value.ToString() : string.Empty;
|
||||||
|
filterQs = $"/type/all/brand/{brandQs}";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
filterQs = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $"{baseUri}items{filterQs}?pageIndex={page}&pageSize={take}";
|
return $"{baseUri}items{filterQs}?pageIndex={page}&pageSize={take}";
|
||||||
|
@ -29,10 +29,14 @@ export class CatalogService {
|
|||||||
|
|
||||||
getCatalog(pageIndex: number, pageSize: number, brand: number, type: number): Observable<ICatalog> {
|
getCatalog(pageIndex: number, pageSize: number, brand: number, type: number): Observable<ICatalog> {
|
||||||
let url = this.catalogUrl;
|
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;
|
url = url + '?pageIndex=' + pageIndex + '&pageSize=' + pageSize;
|
||||||
|
|
||||||
return this.service.get(url).map((response: Response) => {
|
return this.service.get(url).map((response: Response) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user