Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 5c06f6e

Browse files
committed
Fix issue 685
1 parent 3f225e4 commit 5c06f6e

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,26 @@ private IActionResult GetItemsByIds(string ids)
6464
{
6565
var numIds = ids.Split(',')
6666
.Select(id => (Ok: int.TryParse(id, out int x), Value: x));
67+
6768
if (!numIds.All(nid => nid.Ok))
6869
{
6970
return BadRequest("ids value invalid. Must be comma-separated list of numbers");
7071
}
7172

72-
var idsToSelect = numIds.Select(id => id.Value);
73+
var idsToSelect = numIds
74+
.Select(id => id.Value);
75+
7376
var items = _catalogContext.CatalogItems.Where(ci => idsToSelect.Contains(ci.Id)).ToList();
7477

7578
items = ChangeUriPlaceholder(items);
76-
return Ok(items);
7779

80+
return Ok(items);
7881
}
7982

8083
[HttpGet]
8184
[Route("items/{id:int}")]
8285
[ProducesResponseType((int)HttpStatusCode.NotFound)]
83-
[ProducesResponseType(typeof(CatalogItem),(int)HttpStatusCode.OK)]
86+
[ProducesResponseType(typeof(CatalogItem), (int)HttpStatusCode.OK)]
8487
public async Task<IActionResult> GetItemById(int id)
8588
{
8689
if (id <= 0)
@@ -127,19 +130,44 @@ public async Task<IActionResult> Items(string name, [FromQuery]int pageSize = 10
127130
return Ok(model);
128131
}
129132

130-
// GET api/v1/[controller]/items/type/1/brand/null[?pageSize=3&pageIndex=10]
133+
// GET api/v1/[controller]/items/type/1/brand[?pageSize=3&pageIndex=10]
131134
[HttpGet]
132-
[Route("[action]/type/{catalogTypeId}/brand/{catalogBrandId}")]
135+
[Route("[action]/type/{catalogTypeId}/brand/{catalogBrandId:int?}")]
133136
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)]
134-
public async Task<IActionResult> Items(int? catalogTypeId, int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0)
137+
public async Task<IActionResult> Items(int catalogTypeId, int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0)
135138
{
136139
var root = (IQueryable<CatalogItem>)_catalogContext.CatalogItems;
137140

138-
if (catalogTypeId.HasValue)
141+
root = root.Where(ci => ci.CatalogTypeId == catalogTypeId);
142+
143+
if (catalogBrandId.HasValue)
139144
{
140-
root = root.Where(ci => ci.CatalogTypeId == catalogTypeId);
145+
root = root.Where(ci => ci.CatalogBrandId == catalogBrandId);
141146
}
142147

148+
var totalItems = await root
149+
.LongCountAsync();
150+
151+
var itemsOnPage = await root
152+
.Skip(pageSize * pageIndex)
153+
.Take(pageSize)
154+
.ToListAsync();
155+
156+
itemsOnPage = ChangeUriPlaceholder(itemsOnPage);
157+
158+
var model = new PaginatedItemsViewModel<CatalogItem>(
159+
pageIndex, pageSize, totalItems, itemsOnPage);
160+
161+
return Ok(model);
162+
}
163+
// GET api/v1/[controller]/items/type/all/brand[?pageSize=3&pageIndex=10]
164+
[HttpGet]
165+
[Route("[action]/type/all/brand/{catalogBrandId:int?}")]
166+
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)]
167+
public async Task<IActionResult> Items(int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0)
168+
{
169+
var root = (IQueryable<CatalogItem>)_catalogContext.CatalogItems;
170+
143171
if (catalogBrandId.HasValue)
144172
{
145173
root = root.Where(ci => ci.CatalogBrandId == catalogBrandId);

src/Web/WebMVC/Infrastructure/API.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
3-
namespace WebMVC.Infrastructure
1+
namespace WebMVC.Infrastructure
42
{
53
public static class API
64
{
@@ -55,11 +53,20 @@ public static string GetAllCatalogItems(string baseUri, int page, int take, int?
5553
{
5654
var filterQs = "";
5755

58-
if (brand.HasValue || type.HasValue)
56+
if (type.HasValue)
57+
{
58+
var brandQs = (brand.HasValue) ? brand.Value.ToString() : string.Empty;
59+
filterQs = $"/type/{type.Value}/brand/{brandQs}";
60+
61+
}
62+
else if (brand.HasValue)
63+
{
64+
var brandQs = (brand.HasValue) ? brand.Value.ToString() : string.Empty;
65+
filterQs = $"/type/all/brand/{brandQs}";
66+
}
67+
else
5968
{
60-
var brandQs = (brand.HasValue) ? brand.Value.ToString() : "null";
61-
var typeQs = (type.HasValue) ? type.Value.ToString() : "null";
62-
filterQs = $"/type/{typeQs}/brand/{brandQs}";
69+
filterQs = string.Empty;
6370
}
6471

6572
return $"{baseUri}items{filterQs}?pageIndex={page}&pageSize={take}";

src/Web/WebSPA/Client/modules/catalog/catalog.service.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ export class CatalogService {
2929

3030
getCatalog(pageIndex: number, pageSize: number, brand: number, type: number): Observable<ICatalog> {
3131
let url = this.catalogUrl;
32-
if (brand || type) {
33-
url = this.catalogUrl + '/type/' + ((type) ? type.toString() : 'null') + '/brand/' + ((brand) ? brand.toString() : 'null');
34-
}
3532

33+
if (type) {
34+
url = this.catalogUrl + '/type/' + type.toString() + '/brand/' + ((brand) ? brand.toString() : '');
35+
}
36+
else if (brand) {
37+
url = this.catalogUrl + '/type/all' + '/brand/' + ((brand) ? brand.toString() : '');
38+
}
39+
3640
url = url + '?pageIndex=' + pageIndex + '&pageSize=' + pageSize;
3741

3842
return this.service.get(url).map((response: Response) => {

0 commit comments

Comments
 (0)