Skip to content

Commit 8a689c4

Browse files
committed
Add Delete and Create actions to the CatalogController.
1 parent ca02961 commit 8a689c4

1 file changed

Lines changed: 44 additions & 6 deletions

File tree

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

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,21 @@ public async Task<IActionResult> CatalogBrands()
130130
return Ok(items);
131131
}
132132

133+
[Route("edit")]
133134
[HttpPost]
134-
public async Task<IActionResult> Post([FromBody]CatalogItem value)
135+
public async Task<IActionResult> EditProduct([FromBody]CatalogItem product)
135136
{
136-
var item = await _context.CatalogItems.SingleOrDefaultAsync(i => i.Id == value.Id);
137+
var item = await _context.CatalogItems.SingleOrDefaultAsync(i => i.Id == product.Id);
137138

138139
if (item == null)
139140
{
140141
return NotFound();
141142
}
142143

143-
if (item.Price != value.Price)
144+
if (item.Price != product.Price)
144145
{
145146
var oldPrice = item.Price;
146-
item.Price = value.Price;
147+
item.Price = product.Price;
147148
_context.CatalogItems.Update(item);
148149

149150
var @event = new ProductPriceChangedIntegrationEvent(item.Id, item.Price, oldPrice);
@@ -158,10 +159,47 @@ public async Task<IActionResult> Post([FromBody]CatalogItem value)
158159
eventLogEntry.State = EventStateEnum.Published;
159160
_context.IntegrationEventLog.Update(eventLogEntry);
160161
await _context.SaveChangesAsync();
161-
}
162+
}
162163

163164
return Ok();
164-
}
165+
}
166+
167+
[Route("create")]
168+
[HttpPost]
169+
public async Task<IActionResult> CreateProduct([FromBody]CatalogItem product)
170+
{
171+
_context.CatalogItems.Add(
172+
new CatalogItem
173+
{
174+
CatalogBrandId = product.CatalogBrandId,
175+
CatalogTypeId = product.CatalogTypeId,
176+
Description = product.Description,
177+
Name = product.Name,
178+
PictureUri = product.PictureUri,
179+
Price = product.Price
180+
});
181+
182+
await _context.SaveChangesAsync();
183+
184+
return Ok();
185+
}
186+
187+
[Route("{id}")]
188+
[HttpDelete]
189+
public async Task<IActionResult> DeleteProduct(int id)
190+
{
191+
var product = _context.CatalogItems.SingleOrDefault(x => x.Id == id);
192+
193+
if (product == null)
194+
{
195+
return NotFound();
196+
}
197+
198+
_context.CatalogItems.Remove(product);
199+
await _context.SaveChangesAsync();
200+
201+
return Ok();
202+
}
165203

166204
private List<CatalogItem> ComposePicUri(List<CatalogItem> items) {
167205
var baseUri = _settings.Value.ExternalCatalogBaseUrl;

0 commit comments

Comments
 (0)