Skip to content

Commit c85e880

Browse files
committed
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.
1 parent 6ea2e1e commit c85e880

1 file changed

Lines changed: 45 additions & 27 deletions

File tree

src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,37 @@ public async Task<IActionResult> Items([FromQuery]int pageSize = 10, [FromQuery]
3939
.LongCountAsync();
4040

4141
var itemsOnPage = await _catalogContext.CatalogItems
42-
.OrderBy(c=>c.Name)
42+
.OrderBy(c => c.Name)
4343
.Skip(pageSize * pageIndex)
4444
.Take(pageSize)
4545
.ToListAsync();
4646

4747
itemsOnPage = ChangeUriPlaceholder(itemsOnPage);
4848

4949
var model = new PaginatedItemsViewModel<CatalogItem>(
50-
pageIndex, pageSize, totalItems, itemsOnPage);
50+
pageIndex, pageSize, totalItems, itemsOnPage);
5151

5252
return Ok(model);
5353
}
5454

55+
[HttpGet]
56+
[Route("items/{id:int}")]
57+
public async Task<IActionResult> GetItemById(int id)
58+
{
59+
if (id <= 0)
60+
{
61+
return BadRequest();
62+
}
63+
64+
var item = await _catalogContext.CatalogItems.SingleOrDefaultAsync(ci => ci.Id == id);
65+
if (item != null)
66+
{
67+
return Ok(item);
68+
}
69+
70+
return NotFound();
71+
}
72+
5573
// GET api/v1/[controller]/items/withname/samplename[?pageSize=3&pageIndex=10]
5674
[HttpGet]
5775
[Route("[action]/withname/{name:minlength(1)}")]
@@ -131,23 +149,23 @@ public async Task<IActionResult> CatalogBrands()
131149
return Ok(items);
132150
}
133151

134-
//POST api/v1/[controller]/update
135-
[Route("update")]
136-
[HttpPost]
152+
//PUT api/v1/[controller]/items
153+
[Route("items")]
154+
[HttpPut]
137155
public async Task<IActionResult> UpdateProduct([FromBody]CatalogItem productToUpdate)
138156
{
139157
var catalogItem = await _catalogContext.CatalogItems
140158
.SingleOrDefaultAsync(i => i.Id == productToUpdate.Id);
141159

142160
if (catalogItem == null)
143161
{
144-
return NotFound();
162+
return NotFound(new { Message = $"Item with id {productToUpdate.Id} not found." });
145163
}
146164

147165
var oldPrice = catalogItem.Price;
148166
var raiseProductPriceChangedEvent = oldPrice != productToUpdate.Price;
149-
150-
167+
168+
151169
// Update current product
152170
catalogItem = productToUpdate;
153171
_catalogContext.CatalogItems.Update(catalogItem);
@@ -156,40 +174,40 @@ public async Task<IActionResult> UpdateProduct([FromBody]CatalogItem productToUp
156174
{
157175
//Create Integration Event to be published through the Event Bus
158176
var priceChangedEvent = new ProductPriceChangedIntegrationEvent(catalogItem.Id, productToUpdate.Price, oldPrice);
159-
177+
160178
// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
161179
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(priceChangedEvent);
162-
180+
163181
// Publish through the Event Bus and mark the saved event as published
164182
await _catalogIntegrationEventService.PublishThroughEventBusAsync(priceChangedEvent);
165183
}
166184
else // Save updated product
167185
{
168186
await _catalogContext.SaveChangesAsync();
169-
}
187+
}
170188

171-
return Ok();
189+
return CreatedAtAction(nameof(GetItemById), new { id = productToUpdate.Id }, null);
172190
}
173191

174-
//POST api/v1/[controller]/create
175-
[Route("create")]
192+
//POST api/v1/[controller]/items
193+
[Route("items")]
176194
[HttpPost]
177195
public async Task<IActionResult> CreateProduct([FromBody]CatalogItem product)
178196
{
179-
_catalogContext.CatalogItems.Add(
180-
new CatalogItem
181-
{
182-
CatalogBrandId = product.CatalogBrandId,
183-
CatalogTypeId = product.CatalogTypeId,
184-
Description = product.Description,
185-
Name = product.Name,
186-
PictureUri = product.PictureUri,
187-
Price = product.Price
188-
});
197+
var item = new CatalogItem
198+
{
199+
CatalogBrandId = product.CatalogBrandId,
200+
CatalogTypeId = product.CatalogTypeId,
201+
Description = product.Description,
202+
Name = product.Name,
203+
PictureUri = product.PictureUri,
204+
Price = product.Price
205+
};
206+
_catalogContext.CatalogItems.Add(item);
189207

190208
await _catalogContext.SaveChangesAsync();
191209

192-
return Ok();
210+
return CreatedAtAction(nameof(GetItemById), new { id = item.Id }, null);
193211
}
194212

195213
//DELETE api/v1/[controller]/id
@@ -202,13 +220,13 @@ public async Task<IActionResult> DeleteProduct(int id)
202220
if (product == null)
203221
{
204222
return NotFound();
205-
}
223+
}
206224

207225
_catalogContext.CatalogItems.Remove(product);
208226

209227
await _catalogContext.SaveChangesAsync();
210228

211-
return Ok();
229+
return NoContent();
212230
}
213231

214232
private List<CatalogItem> ChangeUriPlaceholder(List<CatalogItem> items)

0 commit comments

Comments
 (0)