@ -28,7 +28,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
_catalogIntegrationEventService = catalogIntegrationEventService ? ? throw new ArgumentNullException ( nameof ( catalogIntegrationEventService ) ) ;
_settings = settings . Value ;
( ( DbContext ) context ) . ChangeTracker . QueryTrackingBehavior = QueryTrackingBehavior . NoTracking ;
context . ChangeTracker . QueryTrackingBehavior = QueryTrackingBehavior . NoTracking ;
}
// GET api/v1/[controller]/items[?pageSize=3&pageIndex=10]
@ -36,11 +36,19 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
[Route("items")]
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(IEnumerable<CatalogItem>), (int)HttpStatusCode.OK)]
public async Task < IActionResult > Items ( [ FromQuery ] int pageSize = 1 0 , [ FromQuery ] int pageIndex = 0 , [ FromQuery ] string ids = null )
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
public async Task < IActionResult > ItemsAsync ( [ FromQuery ] int pageSize = 1 0 , [ FromQuery ] int pageIndex = 0 , string ids = null )
{
if ( ! string . IsNullOrEmpty ( ids ) )
{
return GetItemsByIds ( ids ) ;
var items = await GetItemsByIdsAsync ( ids ) ;
if ( ! items . Any ( ) )
{
return BadRequest ( "ids value invalid. Must be comma-separated list of numbers" ) ;
}
return Ok ( items ) ;
}
var totalItems = await _catalogContext . CatalogItems
@ -54,37 +62,36 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
itemsOnPage = ChangeUriPlaceholder ( itemsOnPage ) ;
var model = new PaginatedItemsViewModel < CatalogItem > (
pageIndex , pageSize , totalItems , itemsOnPage ) ;
var model = new PaginatedItemsViewModel < CatalogItem > ( pageIndex , pageSize , totalItems , itemsOnPage ) ;
return Ok ( model ) ;
}
private IActionResult GetItemsByIds ( string ids )
private async Task < List < CatalogItem > > GetItemsByIdsAsync ( string ids )
{
var numIds = ids . Split ( ',' )
. Select ( id = > ( Ok : int . TryParse ( id , out int x ) , Value : x ) ) ;
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" ) ;
return new List < CatalogItem > ( ) ;
}
var idsToSelect = numIds
. Select ( id = > id . Value ) ;
var items = _catalogContext . CatalogItems . Where ( ci = > idsToSelect . Contains ( ci . Id ) ) . ToList ( ) ;
var items = await _catalogContext . CatalogItems . Where ( ci = > idsToSelect . Contains ( ci . Id ) ) . ToListAsync ( ) ;
items = ChangeUriPlaceholder ( items ) ;
return Ok ( items ) ;
return items ;
}
[HttpGet]
[Route("items/{id:int}")]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
[ProducesResponseType(typeof(CatalogItem), (int)HttpStatusCode.OK)]
public async Task < I ActionResult> Get ItemById( int id )
public async Task < ActionResult < CatalogItem > > ItemByIdAsync ( int id )
{
if ( id < = 0 )
{
@ -95,11 +102,12 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
var baseUri = _settings . PicBaseUrl ;
var azureStorageEnabled = _settings . AzureStorageEnabled ;
item . FillProductUrl ( baseUri , azureStorageEnabled : azureStorageEnabled ) ;
if ( item ! = null )
{
return Ok ( item ) ;
return item ;
}
return NotFound ( ) ;
@ -107,11 +115,10 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
// GET api/v1/[controller]/items/withname/samplename[?pageSize=3&pageIndex=10]
[HttpGet]
[Route("[action] / withname / { name : minlength ( 1 ) } ")]
[Route("items/withname/{name:minlength(1)}")]
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)]
public async Task < I ActionResult> Items ( string name , [ FromQuery ] int pageSize = 1 0 , [ FromQuery ] int pageIndex = 0 )
public async Task < ActionResult < PaginatedItemsViewModel < CatalogItem > > > ItemsWithNameAsync ( string name , [ FromQuery ] int pageSize = 1 0 , [ FromQuery ] int pageIndex = 0 )
{
var totalItems = await _catalogContext . CatalogItems
. Where ( c = > c . Name . StartsWith ( name ) )
. LongCountAsync ( ) ;
@ -124,17 +131,14 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
itemsOnPage = ChangeUriPlaceholder ( itemsOnPage ) ;
var model = new PaginatedItemsViewModel < CatalogItem > (
pageIndex , pageSize , totalItems , itemsOnPage ) ;
return Ok ( model ) ;
return new PaginatedItemsViewModel < CatalogItem > ( pageIndex , pageSize , totalItems , itemsOnPage ) ;
}
// GET api/v1/[controller]/items/type/1/brand[?pageSize=3&pageIndex=10]
[HttpGet]
[Route("[action] / type / { catalogTypeId } / brand / { catalogBrandId : int? } ")]
[Route("items/type/{catalogTypeId}/brand/{catalogBrandId:int?}")]
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)]
public async Task < I ActionResult> Items ( int catalogTypeId , int? catalogBrandId , [ FromQuery ] int pageSize = 1 0 , [ FromQuery ] int pageIndex = 0 )
public async Task < ActionResult < PaginatedItemsViewModel < CatalogItem > > > ItemsByTypeIdAndBrandIdAsync ( int catalogTypeId , int? catalogBrandId , [ FromQuery ] int pageSize = 1 0 , [ FromQuery ] int pageIndex = 0 )
{
var root = ( IQueryable < CatalogItem > ) _catalogContext . CatalogItems ;
@ -155,16 +159,14 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
itemsOnPage = ChangeUriPlaceholder ( itemsOnPage ) ;
var model = new PaginatedItemsViewModel < CatalogItem > (
pageIndex , pageSize , totalItems , itemsOnPage ) ;
return Ok ( model ) ;
return new PaginatedItemsViewModel < CatalogItem > ( pageIndex , pageSize , totalItems , itemsOnPage ) ;
}
// GET api/v1/[controller]/items/type/all/brand[?pageSize=3&pageIndex=10]
[HttpGet]
[Route("[action] / type / all / brand / { catalogBrandId : int? } ")]
[Route("items/type/all/brand/{catalogBrandId:int?}")]
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)]
public async Task < I ActionResult> Items ( int? catalogBrandId , [ FromQuery ] int pageSize = 1 0 , [ FromQuery ] int pageIndex = 0 )
public async Task < ActionResult < PaginatedItemsViewModel < CatalogItem > > > ItemsByBrandIdAsync ( int? catalogBrandId , [ FromQuery ] int pageSize = 1 0 , [ FromQuery ] int pageIndex = 0 )
{
var root = ( IQueryable < CatalogItem > ) _catalogContext . CatalogItems ;
@ -183,34 +185,25 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
itemsOnPage = ChangeUriPlaceholder ( itemsOnPage ) ;
var model = new PaginatedItemsViewModel < CatalogItem > (
pageIndex , pageSize , totalItems , itemsOnPage ) ;
return Ok ( model ) ;
return new PaginatedItemsViewModel < CatalogItem > ( pageIndex , pageSize , totalItems , itemsOnPage ) ;
}
// GET api/v1/[controller]/CatalogTypes
[HttpGet]
[Route("[action] ")]
[ProducesResponseType(typeof(List<CatalogItem >), (int)HttpStatusCode.OK)]
public async Task < I ActionResult> CatalogTypes ( )
[Route("catalogtypes")]
[ProducesResponseType(typeof(List<CatalogType >), (int)HttpStatusCode.OK)]
public async Task < ActionResult < List < CatalogType > > > CatalogTypesAsync ( )
{
var items = await _catalogContext . CatalogTypes
. ToListAsync ( ) ;
return Ok ( items ) ;
return await _catalogContext . CatalogTypes . ToListAsync ( ) ;
}
// GET api/v1/[controller]/CatalogBrands
[HttpGet]
[Route("[action] ")]
[ProducesResponseType(typeof(List<CatalogItem >), (int)HttpStatusCode.OK)]
public async Task < I ActionResult> CatalogBrands ( )
[Route("catalogbrands")]
[ProducesResponseType(typeof(List<CatalogBrand >), (int)HttpStatusCode.OK)]
public async Task < ActionResult < List < CatalogBrand > > > CatalogBrandsAsync ( )
{
var items = await _catalogContext . CatalogBrands
. ToListAsync ( ) ;
return Ok ( items ) ;
return await _catalogContext . CatalogBrands . ToListAsync ( ) ;
}
//PUT api/v1/[controller]/items
@ -218,10 +211,9 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
[HttpPut]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType((int)HttpStatusCode.Created)]
public async Task < I ActionResult> UpdateProduct ( [ FromBody ] CatalogItem productToUpdate )
public async Task < ActionResult > UpdateProductAsync ( [ FromBody ] CatalogItem productToUpdate )
{
var catalogItem = await _catalogContext . CatalogItems
. SingleOrDefaultAsync ( i = > i . Id = = productToUpdate . Id ) ;
var catalogItem = await _catalogContext . CatalogItems . SingleOrDefaultAsync ( i = > i . Id = = productToUpdate . Id ) ;
if ( catalogItem = = null )
{
@ -231,7 +223,6 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
var oldPrice = catalogItem . Price ;
var raiseProductPriceChangedEvent = oldPrice ! = productToUpdate . Price ;
// Update current product
catalogItem = productToUpdate ;
_catalogContext . CatalogItems . Update ( catalogItem ) ;
@ -252,14 +243,14 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
await _catalogContext . SaveChangesAsync ( ) ;
}
return CreatedAtAction ( nameof ( Get ItemById) , new { id = productToUpdate . Id } , null ) ;
return CreatedAtAction ( nameof ( ItemByIdAsync ) , new { id = productToUpdate . Id } , null ) ;
}
//POST api/v1/[controller]/items
[Route("items")]
[HttpPost]
[ProducesResponseType((int)HttpStatusCode.Created)]
public async Task < I ActionResult> CreateProduct ( [ FromBody ] CatalogItem product )
public async Task < ActionResult > CreateProductAsync ( [ FromBody ] CatalogItem product )
{
var item = new CatalogItem
{
@ -270,18 +261,20 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
PictureFileName = product . PictureFileName ,
Price = product . Price
} ;
_catalogContext . CatalogItems . Add ( item ) ;
await _catalogContext . SaveChangesAsync ( ) ;
return CreatedAtAction ( nameof ( Get ItemById) , new { id = item . Id } , null ) ;
return CreatedAtAction ( nameof ( ItemByIdAsync ) , new { id = item . Id } , null ) ;
}
//DELETE api/v1/[controller]/id
[Route("{id}")]
[HttpDelete]
[ProducesResponseType((int)HttpStatusCode.NoContent)]
public async Task < IActionResult > DeleteProduct ( int id )
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task < ActionResult > DeleteProductAsync ( int id )
{
var product = _catalogContext . CatalogItems . SingleOrDefault ( x = > x . Id = = id ) ;