public ActionResult AddExpense(ExpenseAddViewModel model) { if (ModelState.IsValid) { if (model.Expense.ExpenseId != 0) { //update var expense = _expenseManager.Get(e => e.ExpenseId == model.Expense.ExpenseId); expense.ExpenseName = model.Expense.ExpenseName; expense.ExpenseDescription = model.Expense.ExpenseDescription; expense.ExpenseAmount = model.Expense.ExpenseAmount; expense.DateTime = model.Expense.DateTime; expense.CategoryId = model.Expense.CategoryId; _expenseManager.Update(expense); } else { //add _expenseManager.Add(model.Expense); } } else { ViewBag.Message = "Model yanlış"; return(View()); } return(RedirectToAction("ListExpense")); }
public async Task <IActionResult> Add([FromBody] ExpenseAddViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // confirm user exists var userId = _userManager.GetUserId(HttpContext.User); if (userId == null) { return(BadRequest(Errors.AddErrorToModelState( "add_expense_failure", "Unable to find a user for this expense", ModelState))); } // confirm class exists var budget = _db.Budgets .FirstOrDefault(x => x.Id == model.BudgetId && x.UserId == userId); if (budget == null) { return(BadRequest(Errors.AddErrorToModelState( "add_expense_failure", "Unable to find a class for this expense", ModelState))); } // add var newRecord = new Expense { Description = model.Description, Amount = model.Amount, IsForever = model.IsForever, StartDate = model.StartDate, EndDate = model.EndDate, Frequency = model.Frequency, RepeatMon = model.RepeatMon, RepeatTue = model.RepeatTue, RepeatWed = model.RepeatWed, RepeatThu = model.RepeatThu, RepeatFri = model.RepeatFri, RepeatSat = model.RepeatSat, RepeatSun = model.RepeatSun, Budget = budget, BudgetId = budget.Id }; _db.Expenses.Add(newRecord); await _db.SaveChangesAsync(); return(new OkObjectResult(newRecord.Id)); }
private void createBtn_Click(object sender, EventArgs e) { try { ExpenseAddViewModel vm = new ExpenseAddViewModel(); vm.ExpenseTypeId = (expenseTypeCombobox.SelectedItem as ExpenseTypeDTO).ExpenseTypeId; vm.Date = TimeHelpers.dateTimeToString(datePicker.Value); vm.Cost = (double)costPicker.Value; EventHelpers.RaiseEvent(this, ExpenseAddConfirmEventRaised, vm); Close(); } catch (Exception) { } }
public ActionResult AddExpense(int id = 0) { var model = new ExpenseAddViewModel(); CategoryManager categoryManager = new CategoryManager(); var categories = categoryManager.GetAll(); ViewBag.Categories = categories; if (id != 0) { var expense = _expenseManager.Get(e => e.ExpenseId == id); model.Expense = expense; } return(View(model)); }
private void OnExpenseAddConfirmEventRaised(object sender, ExpenseAddViewModel args) { try { ExpenseDTO dto = new ExpenseDTO(); dto.Cost = args.Cost; dto.Date = DateTime.Parse(args.Date); dto.ExpenseTypeId = args.ExpenseTypeId; dto.UserId = _session.GetUser().UserId; _expenseService.Create(dto); _expenseListPresenter.LoadAllExpensesFromDbToGrid(); _expenseStatisticsPresenter.Refresh(); } catch (DataAccessException ex) { EventHelpers.RaiseEvent(this, DataAccessExceptionEvent, ex); } }