@@ -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 ) ;
0 commit comments