Catalog.API methods following more rest conventions:
Create/Update routed by POST/PUT & Location header returned A new GET endpoint for returning single item by id created to honour Location header of previous methods.
This commit is contained in:
parent
658904ce5c
commit
9ed2f1ce7b
@ -39,7 +39,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
.LongCountAsync();
|
.LongCountAsync();
|
||||||
|
|
||||||
var itemsOnPage = await _catalogContext.CatalogItems
|
var itemsOnPage = await _catalogContext.CatalogItems
|
||||||
.OrderBy(c=>c.Name)
|
.OrderBy(c => c.Name)
|
||||||
.Skip(pageSize * pageIndex)
|
.Skip(pageSize * pageIndex)
|
||||||
.Take(pageSize)
|
.Take(pageSize)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
@ -47,11 +47,29 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
itemsOnPage = ChangeUriPlaceholder(itemsOnPage);
|
itemsOnPage = ChangeUriPlaceholder(itemsOnPage);
|
||||||
|
|
||||||
var model = new PaginatedItemsViewModel<CatalogItem>(
|
var model = new PaginatedItemsViewModel<CatalogItem>(
|
||||||
pageIndex, pageSize, totalItems, itemsOnPage);
|
pageIndex, pageSize, totalItems, itemsOnPage);
|
||||||
|
|
||||||
return Ok(model);
|
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]
|
// GET api/v1/[controller]/items/withname/samplename[?pageSize=3&pageIndex=10]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("[action]/withname/{name:minlength(1)}")]
|
[Route("[action]/withname/{name:minlength(1)}")]
|
||||||
@ -131,9 +149,9 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
return Ok(items);
|
return Ok(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
//POST api/v1/[controller]/update
|
//PUT api/v1/[controller]/items
|
||||||
[Route("update")]
|
[Route("items")]
|
||||||
[HttpPost]
|
[HttpPut]
|
||||||
public async Task<IActionResult> UpdateProduct([FromBody]CatalogItem productToUpdate)
|
public async Task<IActionResult> UpdateProduct([FromBody]CatalogItem productToUpdate)
|
||||||
{
|
{
|
||||||
var catalogItem = await _catalogContext.CatalogItems
|
var catalogItem = await _catalogContext.CatalogItems
|
||||||
@ -141,13 +159,13 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
|
|
||||||
if (catalogItem == null)
|
if (catalogItem == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound(new { Message = $"Item with id {productToUpdate.Id} not found." });
|
||||||
}
|
}
|
||||||
|
|
||||||
var oldPrice = catalogItem.Price;
|
var oldPrice = catalogItem.Price;
|
||||||
var raiseProductPriceChangedEvent = oldPrice != productToUpdate.Price;
|
var raiseProductPriceChangedEvent = oldPrice != productToUpdate.Price;
|
||||||
|
|
||||||
|
|
||||||
// Update current product
|
// Update current product
|
||||||
catalogItem = productToUpdate;
|
catalogItem = productToUpdate;
|
||||||
_catalogContext.CatalogItems.Update(catalogItem);
|
_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
|
//Create Integration Event to be published through the Event Bus
|
||||||
var priceChangedEvent = new ProductPriceChangedIntegrationEvent(catalogItem.Id, productToUpdate.Price, oldPrice);
|
var priceChangedEvent = new ProductPriceChangedIntegrationEvent(catalogItem.Id, productToUpdate.Price, oldPrice);
|
||||||
|
|
||||||
// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
|
// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
|
||||||
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(priceChangedEvent);
|
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(priceChangedEvent);
|
||||||
|
|
||||||
// Publish through the Event Bus and mark the saved event as published
|
// Publish through the Event Bus and mark the saved event as published
|
||||||
await _catalogIntegrationEventService.PublishThroughEventBusAsync(priceChangedEvent);
|
await _catalogIntegrationEventService.PublishThroughEventBusAsync(priceChangedEvent);
|
||||||
}
|
}
|
||||||
else // Save updated product
|
else // Save updated product
|
||||||
{
|
{
|
||||||
await _catalogContext.SaveChangesAsync();
|
await _catalogContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok();
|
return CreatedAtAction(nameof(GetItemById), new { id = productToUpdate.Id }, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
//POST api/v1/[controller]/create
|
//POST api/v1/[controller]/items
|
||||||
[Route("create")]
|
[Route("items")]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> CreateProduct([FromBody]CatalogItem product)
|
public async Task<IActionResult> CreateProduct([FromBody]CatalogItem product)
|
||||||
{
|
{
|
||||||
_catalogContext.CatalogItems.Add(
|
var item = new CatalogItem
|
||||||
new CatalogItem
|
{
|
||||||
{
|
CatalogBrandId = product.CatalogBrandId,
|
||||||
CatalogBrandId = product.CatalogBrandId,
|
CatalogTypeId = product.CatalogTypeId,
|
||||||
CatalogTypeId = product.CatalogTypeId,
|
Description = product.Description,
|
||||||
Description = product.Description,
|
Name = product.Name,
|
||||||
Name = product.Name,
|
PictureUri = product.PictureUri,
|
||||||
PictureUri = product.PictureUri,
|
Price = product.Price
|
||||||
Price = product.Price
|
};
|
||||||
});
|
_catalogContext.CatalogItems.Add(item);
|
||||||
|
|
||||||
await _catalogContext.SaveChangesAsync();
|
await _catalogContext.SaveChangesAsync();
|
||||||
|
|
||||||
return Ok();
|
return CreatedAtAction(nameof(GetItemById), new { id = item.Id }, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
//DELETE api/v1/[controller]/id
|
//DELETE api/v1/[controller]/id
|
||||||
@ -202,13 +220,13 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
if (product == null)
|
if (product == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
_catalogContext.CatalogItems.Remove(product);
|
_catalogContext.CatalogItems.Remove(product);
|
||||||
|
|
||||||
await _catalogContext.SaveChangesAsync();
|
await _catalogContext.SaveChangesAsync();
|
||||||
|
|
||||||
return Ok();
|
return NoContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<CatalogItem> ChangeUriPlaceholder(List<CatalogItem> items)
|
private List<CatalogItem> ChangeUriPlaceholder(List<CatalogItem> items)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user