public async Task <ActionResult <CurrentExpense> > PostCurrentExpense(CurrentExpenseInputModel currentExpenseInput) { var user = await _context.Users.FindAsync(GetUserId()); var currentExpense = new CurrentExpense { Date = currentExpenseInput.Date, User = user, ExpenseData = new ExpenseData { Amount = currentExpenseInput.Amount, Description = currentExpenseInput.Description } }; _context.CurrentExpenses.Add(currentExpense); await _context.SaveChangesAsync(); return(CreatedAtAction("GetCurrentExpense", new { id = currentExpense.Id }, currentExpense)); }
public async Task <IActionResult> PutCurrentExpense(int id, CurrentExpenseInputModel currentExpenseInput) { var uid = GetUserId(); var expenseToModify = await this.UserCurrentExpensesQueryBuilder(uid).Where(c => c.Id == id).FirstOrDefaultAsync(); if (expenseToModify == null) { throw new Exception("You don't own any current expense with that id"); } expenseToModify.Date = currentExpenseInput.Date; expenseToModify.ExpenseData = new ExpenseData { Amount = currentExpenseInput.Amount, Description = currentExpenseInput.Description }; _context.Entry(expenseToModify).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CurrentExpenseExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }