@ -39,7 +39,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
. LongCountAsync ( ) ;
var itemsOnPage = await _catalogContext . CatalogItems
. OrderBy ( c = > c . Name )
. OrderBy ( c = > c . Name )
. Skip ( pageSize * pageIndex )
. Take ( pageSize )
. ToListAsync ( ) ;
@ -47,11 +47,29 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
itemsOnPage = ChangeUriPlaceholder ( itemsOnPage ) ;
var model = new PaginatedItemsViewModel < CatalogItem > (
pageIndex , pageSize , totalItems , itemsOnPage ) ;
pageIndex , pageSize , totalItems , itemsOnPage ) ;
return Ok ( model ) ;
}
[HttpGet]
[Route("items/{id:int}")]
public async Task < IActionResult > GetItemById ( int id )
{
if ( id < = 0 )
{
return BadRequest ( ) ;
}
var item = await _catalogContext . CatalogItems . SingleOrDefaultAsync ( ci = > ci . Id = = id ) ;
if ( item ! = null )
{
return Ok ( item ) ;
}
return NotFound ( ) ;
}
// GET api/v1/[controller]/items/withname/samplename[?pageSize=3&pageIndex=10]
[HttpGet]
[Route("[action] / withname / { name : minlength ( 1 ) } ")]
@ -131,9 +149,9 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
return Ok ( items ) ;
}
//POST api/v1/[controller]/update
[Route("update ")]
[HttpPos t]
//PUT api/v1/[controller]/items
[Route("items ")]
[HttpPu t]
public async Task < IActionResult > UpdateProduct ( [ FromBody ] CatalogItem productToUpdate )
{
var catalogItem = await _catalogContext . CatalogItems
@ -141,13 +159,13 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
if ( catalogItem = = null )
{
return NotFound ( ) ;
return NotFound ( new { Message = $"Item with id {productToUpdate.Id} not found." } ) ;
}
var oldPrice = catalogItem . Price ;
var raiseProductPriceChangedEvent = oldPrice ! = productToUpdate . Price ;
// Update current product
catalogItem = productToUpdate ;
_catalogContext . CatalogItems . Update ( catalogItem ) ;
@ -156,40 +174,40 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
{
//Create Integration Event to be published through the Event Bus
var priceChangedEvent = new ProductPriceChangedIntegrationEvent ( catalogItem . Id , productToUpdate . Price , oldPrice ) ;
// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
await _catalogIntegrationEventService . SaveEventAndCatalogContextChangesAsync ( priceChangedEvent ) ;
// Publish through the Event Bus and mark the saved event as published
await _catalogIntegrationEventService . PublishThroughEventBusAsync ( priceChangedEvent ) ;
}
else // Save updated product
{
await _catalogContext . SaveChangesAsync ( ) ;
}
}
return Ok ( ) ;
return CreatedAtAction ( nameof ( GetItemById ) , new { id = productToUpdate . Id } , null ) ;
}
//POST api/v1/[controller]/create
[Route("create ")]
//POST api/v1/[controller]/items
[Route("items ")]
[HttpPost]
public async Task < IActionResult > CreateProduct ( [ FromBody ] CatalogItem product )
{
_catalogContext . CatalogItems . Add (
new CatalogItem
{
CatalogBrand Id = product . CatalogBrand Id ,
CatalogTypeId = product . CatalogTypeId ,
Description = product . Description ,
Name = product . Name ,
Pictur eUri = product . Pictur eUri ,
Price = product . Price
} ) ;
var item = new CatalogItem
{
CatalogBrandId = product . CatalogBrandId ,
CatalogType Id = product . CatalogType Id ,
Description = product . Description ,
Name = product . Name ,
PictureUri = product . PictureUri ,
Pr ice = product . Pr ice
} ;
_catalogContext . CatalogItems . Add ( item ) ;
await _catalogContext . SaveChangesAsync ( ) ;
return Ok ( ) ;
return CreatedAtAction ( nameof ( GetItemById ) , new { id = item . Id } , null ) ;
}
//DELETE api/v1/[controller]/id
@ -202,13 +220,13 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
if ( product = = null )
{
return NotFound ( ) ;
}
}
_catalogContext . CatalogItems . Remove ( product ) ;
await _catalogContext . SaveChangesAsync ( ) ;
return Ok ( ) ;
return NoContent ( ) ;
}
private List < CatalogItem > ChangeUriPlaceholder ( List < CatalogItem > items )