From 5e45509a7298affd65ebd4d9cd66140014063cb4 Mon Sep 17 00:00:00 2001 From: Ferenc Hammerl Date: Tue, 20 Oct 2020 20:29:36 +0000 Subject: [PATCH] Add delete method --- ExpenseTracker.Api/Controllers/ExpenseController.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ExpenseTracker.Api/Controllers/ExpenseController.cs b/ExpenseTracker.Api/Controllers/ExpenseController.cs index 429b7f7..cfab280 100644 --- a/ExpenseTracker.Api/Controllers/ExpenseController.cs +++ b/ExpenseTracker.Api/Controllers/ExpenseController.cs @@ -1,4 +1,4 @@ -using System.Threading.Tasks; +using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using ExpenseTracker.Models; @@ -58,6 +58,17 @@ public async Task Update(Expense expense) return Ok(expense); } + [HttpDelete("{id}")] + public async Task Delete(int id) + { + var expense = await ctx.FindAsync(id); + if (expense == null) + { + return NotFound(); + } + ctx.Expenses.Remove(expense); + ctx.SaveChanges(); + return NoContent(); } } }