From d25272e6511f4b14712f4f7133c13f35c647d13b Mon Sep 17 00:00:00 2001 From: Ferenc Hammerl Date: Tue, 20 Oct 2020 20:29:22 +0000 Subject: [PATCH] Add update method --- .../Controllers/ExpenseController.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ExpenseTracker.Api/Controllers/ExpenseController.cs b/ExpenseTracker.Api/Controllers/ExpenseController.cs index 6db62b2..429b7f7 100644 --- a/ExpenseTracker.Api/Controllers/ExpenseController.cs +++ b/ExpenseTracker.Api/Controllers/ExpenseController.cs @@ -44,6 +44,20 @@ public async Task Create(Expense expense) await ctx.SaveChangesAsync(); return Ok(expense); } + + [HttpPut] + public async Task Update(Expense expense) + { + var expenseToUpdate = await ctx.FindAsync(expense.ExpenseID); + if (expenseToUpdate == null) + { + return NotFound(); + } + ctx.Entry(expenseToUpdate).CurrentValues.SetValues(expense); + ctx.SaveChanges(); + return Ok(expense); + } + } } }