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
6ea2e1ed8b
commit
c85e880dad
@ -52,6 +52,24 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
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,7 +159,7 @@ 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;
|
||||||
@ -168,16 +186,15 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
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,
|
||||||
@ -185,11 +202,12 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
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
|
||||||
@ -208,7 +226,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
|
|
||||||
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