Add Delete and Create actions to the CatalogController.
This commit is contained in:
parent
ca029618e6
commit
8a689c45f2
@ -130,20 +130,21 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
return Ok(items);
|
return Ok(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Route("edit")]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> Post([FromBody]CatalogItem value)
|
public async Task<IActionResult> EditProduct([FromBody]CatalogItem product)
|
||||||
{
|
{
|
||||||
var item = await _context.CatalogItems.SingleOrDefaultAsync(i => i.Id == value.Id);
|
var item = await _context.CatalogItems.SingleOrDefaultAsync(i => i.Id == product.Id);
|
||||||
|
|
||||||
if (item == null)
|
if (item == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.Price != value.Price)
|
if (item.Price != product.Price)
|
||||||
{
|
{
|
||||||
var oldPrice = item.Price;
|
var oldPrice = item.Price;
|
||||||
item.Price = value.Price;
|
item.Price = product.Price;
|
||||||
_context.CatalogItems.Update(item);
|
_context.CatalogItems.Update(item);
|
||||||
|
|
||||||
var @event = new ProductPriceChangedIntegrationEvent(item.Id, item.Price, oldPrice);
|
var @event = new ProductPriceChangedIntegrationEvent(item.Id, item.Price, oldPrice);
|
||||||
@ -163,6 +164,43 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Route("create")]
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<IActionResult> CreateProduct([FromBody]CatalogItem product)
|
||||||
|
{
|
||||||
|
_context.CatalogItems.Add(
|
||||||
|
new CatalogItem
|
||||||
|
{
|
||||||
|
CatalogBrandId = product.CatalogBrandId,
|
||||||
|
CatalogTypeId = product.CatalogTypeId,
|
||||||
|
Description = product.Description,
|
||||||
|
Name = product.Name,
|
||||||
|
PictureUri = product.PictureUri,
|
||||||
|
Price = product.Price
|
||||||
|
});
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Route("{id}")]
|
||||||
|
[HttpDelete]
|
||||||
|
public async Task<IActionResult> DeleteProduct(int id)
|
||||||
|
{
|
||||||
|
var product = _context.CatalogItems.SingleOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
|
if (product == null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
_context.CatalogItems.Remove(product);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
private List<CatalogItem> ComposePicUri(List<CatalogItem> items) {
|
private List<CatalogItem> ComposePicUri(List<CatalogItem> items) {
|
||||||
var baseUri = _settings.Value.ExternalCatalogBaseUrl;
|
var baseUri = _settings.Value.ExternalCatalogBaseUrl;
|
||||||
items.ForEach(x =>
|
items.ForEach(x =>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user