Browse Source

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.
pull/175/head
Eduard Tomas 7 years ago
parent
commit
c85e880dad
1 changed files with 45 additions and 27 deletions
  1. +45
    -27
      src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs

+ 45
- 27
src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs View File

@ -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")]
[HttpPost]
//PUT api/v1/[controller]/items
[Route("items")]
[HttpPut]
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
{
CatalogBrandId = product.CatalogBrandId,
CatalogTypeId = product.CatalogTypeId,
Description = product.Description,
Name = product.Name,
PictureUri = product.PictureUri,
Price = product.Price
});
var item = new CatalogItem
{
CatalogBrandId = product.CatalogBrandId,
CatalogTypeId = product.CatalogTypeId,
Description = product.Description,
Name = product.Name,
PictureUri = product.PictureUri,
Price = product.Price
};
_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)


Loading…
Cancel
Save