From c85e880dad4b89541068e1edfa84c31eef7d809b Mon Sep 17 00:00:00 2001 From: Eduard Tomas Date: Tue, 18 Apr 2017 17:58:52 +0200 Subject: [PATCH] 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. --- .../Controllers/CatalogController.cs | 72 ++++++++++++------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs index bd5966a9c..374d8ec7c 100644 --- a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs +++ b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs @@ -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( - pageIndex, pageSize, totalItems, itemsOnPage); + pageIndex, pageSize, totalItems, itemsOnPage); return Ok(model); } + [HttpGet] + [Route("items/{id:int}")] + public async Task 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 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 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 ChangeUriPlaceholder(List items)