From 8a689c45f251e0c8d4498cddad01c5c4d418b42d Mon Sep 17 00:00:00 2001 From: dsanz Date: Tue, 21 Mar 2017 13:10:40 +0100 Subject: [PATCH] Add Delete and Create actions to the CatalogController. --- .../Controllers/CatalogController.cs | 50 ++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs index 431928e0b..4add23b35 100644 --- a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs +++ b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs @@ -130,20 +130,21 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers return Ok(items); } + [Route("edit")] [HttpPost] - public async Task Post([FromBody]CatalogItem value) + public async Task 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) { return NotFound(); } - if (item.Price != value.Price) + if (item.Price != product.Price) { var oldPrice = item.Price; - item.Price = value.Price; + item.Price = product.Price; _context.CatalogItems.Update(item); var @event = new ProductPriceChangedIntegrationEvent(item.Id, item.Price, oldPrice); @@ -158,10 +159,47 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers eventLogEntry.State = EventStateEnum.Published; _context.IntegrationEventLog.Update(eventLogEntry); await _context.SaveChangesAsync(); - } + } return Ok(); - } + } + + [Route("create")] + [HttpPost] + public async Task 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 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 ComposePicUri(List items) { var baseUri = _settings.Value.ExternalCatalogBaseUrl;