public async Task <IActionResult> Edit(int id) { Expense expenseToEdit = await _dbContext.Expenses.Include(x => x.Category).FirstOrDefaultAsync(x => x.Id == id); var categories = await _dbContext.Categories.ToListAsync(); ExpenseEditViewModel evm = new ExpenseEditViewModel(); foreach (Category category in categories) { evm.Category.Add(new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem() { Value = category.Id.ToString(), Text = category.Name }); } evm.Amount = (decimal)expenseToEdit.Amount; evm.CategoryId = expenseToEdit.Category.Id; evm.Description = (string)expenseToEdit.Description; evm.Date = (DateTime)expenseToEdit.Date; return(View(evm)); }
public IActionResult Edit([FromRoute] int id, ExpenseEditViewModel vm) { if (!TryValidateModel(vm)) { return(View(vm)); } var expense = new ExpenseDto { Description = vm.Description, Date = vm.Date, Value = vm.Value, Categorie = vm.Categorie, }; var dbExpense = _expensesService.GetById(id); if (vm.Photo == null) { expense.PhotoUrl = dbExpense.PhotoUrl; } else { if (!string.IsNullOrEmpty(dbExpense.PhotoUrl)) { _photoService.DeletePhoto(dbExpense.PhotoUrl); } expense.PhotoUrl = _photoService.UploadPhoto(vm.Photo); } _expensesService.Update(id, expense); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Edit(int id) { Expense expenseFromDb = await _expenseDatabase.Expenses.Include(expense => expense.ExpenseProducts).FirstOrDefaultAsync(m => m.Id == id); if (expenseFromDb.UserId == User.FindFirstValue(ClaimTypes.NameIdentifier)) { ExpenseEditViewModel vm = new ExpenseEditViewModel { Amount = expenseFromDb.Amount, Date = expenseFromDb.Date, Description = expenseFromDb.Description, Category = expenseFromDb.Category, PaymentStatusId = expenseFromDb.PaymentStatusId, SelectedProducts = expenseFromDb.ExpenseProducts.Select(x => x.ProductId).ToArray(), }; var products = await _expenseDatabase.Products.ToListAsync(); vm.Products = products.Select(tag => new SelectListItem() { Value = tag.Id.ToString(), Text = tag.Name }).ToList(); return(View(vm)); } else { return(RedirectToAction("NoSneakyStuff")); } }
public async Task <IActionResult> Edit(int id, ExpenseEditViewModel editedExpense) { Expense domainExpense = await _expenseDbContext.Expenses.Include(e => e.PersonExpenses).FirstOrDefaultAsync(e => e.Id == id); _expenseDbContext.PersonExpenses.RemoveRange(domainExpense.PersonExpenses); domainExpense.Omschrijving = editedExpense.Omschrijving; domainExpense.Datum = editedExpense.Datum; domainExpense.Bedrag = editedExpense.Bedrag; domainExpense.Categorie = editedExpense.Categorie; domainExpense.PaidStatusId = editedExpense.SelectedPaidStatus; domainExpense.PersonExpenses = editedExpense.SelectedPersons.Select(person => new PersonExpense() { PersonId = person }).ToList(); if (editedExpense.Photo != null) { string uniqueFileName = UploadExpensePhoto(editedExpense.Photo); domainExpense.PhotoUrl = "/photos/" + uniqueFileName; } _expenseDbContext.Update(domainExpense); await _expenseDbContext.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Edit(int id) { var expense = await _expenseDbContext.Expenses.FindAsync(id); var paidStatuses = await _expenseDbContext.PaidStatuses.ToListAsync(); ExpenseEditViewModel showExpense = new ExpenseEditViewModel() { Omschrijving = expense.Omschrijving, Datum = expense.Datum, Bedrag = expense.Bedrag, Categorie = expense.Categorie, PhotoUrl = expense.PhotoUrl, }; foreach (PaidStatus paidStatus in paidStatuses) { showExpense.PaidStatuses.Add(new SelectListItem() { Value = paidStatus.Id.ToString(), Text = paidStatus.Name }); } var persons = await _expenseDbContext.Persons.ToListAsync(); showExpense.Persons = persons.Select(person => new SelectListItem() { Value = person.Id.ToString(), Text = person.Name }).ToList(); return(View(showExpense)); }
public async Task <IActionResult> Edit(ExpenseEditViewModel model) { if (!ModelState.IsValid) { return(this.View(model)); } await this.expenseService.Edit(model); return(this.RedirectToAction("All")); }
private void OnExpenseEditConfirmEventRaised(object sender, ExpenseEditViewModel args) { ExpenseDTO expenseDTO = (ExpenseDTO)_expenseDtoBindingSource.Current; expenseDTO.Cost = args.Cost; expenseDTO.Date = DateTime.Parse(args.Date); expenseDTO.ExpenseTypeId = args.ExpenseTypeId; _expenseService.Update(expenseDTO); LoadAllExpensesFromDbToGrid(); EventHelpers.RaiseEvent(this, RefreshStatisticsEvent, null); }
private void saveChangedBtn_Click(object sender, EventArgs e) { try { ExpenseEditViewModel vm = new ExpenseEditViewModel(); vm.ExpenseTypeId = (expenseTypeComboBox.SelectedItem as ExpenseTypeDTO).ExpenseTypeId; vm.Date = TimeHelpers.dateTimeToString(datePicker.Value); vm.Cost = (double)costNumericPicker.Value; EventHelpers.RaiseEvent(this, ExpenseEditConfirmEventRaised, vm); Close(); } catch (Exception) { } }
public async Task <IActionResult> Edit([FromBody] ExpenseEditViewModel 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))); } var expense = _db.Expenses .Include(x => x.Budget) .FirstOrDefault(x => x.Budget.UserId == userId && x.Id == model.Id); if (expense == null) { return(BadRequest(Errors.AddErrorToModelState( "add_expense_failure", "Unable to find this expense", ModelState))); } // Update expense.Description = model.Description; expense.Amount = model.Amount; expense.IsForever = model.IsForever; expense.StartDate = model.StartDate; expense.EndDate = model.EndDate; expense.Frequency = model.Frequency; expense.RepeatMon = model.RepeatMon; expense.RepeatTue = model.RepeatTue; expense.RepeatWed = model.RepeatWed; expense.RepeatThu = model.RepeatThu; expense.RepeatFri = model.RepeatFri; expense.RepeatSat = model.RepeatSat; expense.RepeatSun = model.RepeatSun; await _db.SaveChangesAsync(); return(new OkObjectResult(new { message = "success" })); }
public async Task Edit(ExpenseEditViewModel model) { var expense = new Expense { Id = model.Id, ExpenceDate = model.ExpenceDate, ExpenceDescription = model.ExpenceDescription, ExpenceValue = model.ExpenceValue, LawCaseId = model.LawCaseId, Payee = model.Payee, }; this.dbContext.Update(expense); await this.dbContext.SaveChangesAsync(); }
public async Task <IActionResult> Edit(int id, ExpenseEditViewModel vm) { Expense changedExpense = await _dbContext.Expenses.Include(x => x.Category).FirstOrDefaultAsync(x => x.Id == id); changedExpense.Amount = vm.Amount; changedExpense.CategoryId = vm.CategoryId; changedExpense.Description = vm.Description; changedExpense.Date = vm.Date; var expense = _dbContext.Expenses.SingleOrDefault(a => a.Id == id); _dbContext.Remove(expense); _dbContext.Expenses.Update(changedExpense); await _dbContext.SaveChangesAsync(); return(RedirectToAction("Index")); }
public IActionResult Edit([FromForm] ExpenseEditViewModel expense, [FromRoute] int id) { if (TryValidateModel(expense)) { _expenseDatabase.Update(id, new Expense { Bedrag = expense.Bedrag, Categorie = expense.Categorie, Datum = expense.Datum, Omschrijving = expense.Omschrijving, PhotoUrl = expense.PhotoUrl }); return(RedirectToAction(nameof(Index))); } else { return(View(expense)); } }
public IActionResult Edit([FromRoute] int id, [FromForm] ExpenseEditViewModel form) { if (form.Photo != null) { DeletePhoto(form.photoUrl); string uniqueFileName = UploadPhoto(form.Photo); form.photoUrl = "/expense-pics/" + uniqueFileName; } _expenseDatabase.Update(id, new Expense { Omschrijving = form.Omschrijving, Bedrag = form.Bedrag, Categorie = form.Categorie, Datum = form.Datum, PhotoUrl = form.photoUrl }); return(RedirectToAction(nameof(Index))); }
public IActionResult Edit(int id, ExpenseEditViewModel vm) { if (!TryValidateModel(vm)) { return(View(vm)); } var expenseFromDb = _expenseDatabase.Expenses.Find(id); expenseFromDb.Amount = vm.Amount; expenseFromDb.Date = vm.Date; expenseFromDb.Description = vm.Description; expenseFromDb.Category = vm.Category; expenseFromDb.PaymentStatusId = vm.PaymentStatusId; _expenseDatabase.Expenses.Update(expenseFromDb); _expenseDatabase.SaveChanges(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Edit(int id) { string userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var expense = await _expenseDbContext.Expenses .Include(x => x.ExpenseTags) .FirstOrDefaultAsync(x => x.Id == id && x.ExpenseAppUserId == userId); var categories = await _expenseDbContext.Categories.ToListAsync(); var tags = await _expenseDbContext.Tags.ToListAsync(); var expenseEdit = new ExpenseEditViewModel { Amount = expense.Amount, Description = expense.Description, Date = expense.Date, SelectedCategory = expense.Category.Id, Categories = categories.Select(item => new SelectListItem { Value = item.Id.ToString(), Text = item.Description, }), Tags = tags.Select(item => new SelectListItem { Value = item.Id.ToString(), Text = item.Name, Selected = expense.ExpenseTags.Where(i => i.TagId == item.Id).Any() }) }; if (!string.IsNullOrEmpty(expense.PhotoPath)) { expenseEdit.FilePath = expense.PhotoPath; } return(View(expenseEdit)); }
public async Task <IActionResult> Edit(int id, ExpenseEditViewModel vm) { if (!TryValidateModel(vm)) { return(View(vm)); } string userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var origExpense = await _expenseDbContext.Expenses .Include(x => x.ExpenseTags) .FirstOrDefaultAsync(x => x.Id == id && x.ExpenseAppUserId == userId); _expenseDbContext.ExpenseTags.RemoveRange(origExpense.ExpenseTags); origExpense.Description = vm.Description; origExpense.Date = vm.Date; origExpense.Amount = vm.Amount; origExpense.CategoryId = vm.SelectedCategory; origExpense.ExpenseTags = vm.SelectedTags.Select(id => new ExpenseTag { TagId = id }).ToList(); if (vm.File != null) { if (!string.IsNullOrEmpty(origExpense.PhotoPath)) { _photoService.DeletePhoto(origExpense.PhotoPath); } origExpense.PhotoPath = _photoService.AddPhoto(vm.File); } await _expenseDbContext.SaveChangesAsync(); return(RedirectToAction("Detail", new { Id = id })); }
public async Task <IActionResult> Index() { List <ExpenseListViewModel> XpList = new List <ExpenseListViewModel>(); IEnumerable <Expense> expenses = await _dbContext.Expenses .Include(x => x.Category).Where(expense => expense.ExpenseUserId == User.FindFirstValue(ClaimTypes.NameIdentifier)).ToListAsync(); IEnumerable <Expense> sortedExpenses = expenses.OrderBy(x => x.Date); var expense = new ExpenseEditViewModel(); foreach (var thing in sortedExpenses) { ExpenseListViewModel Xp = new ExpenseListViewModel() { Id = thing.Id, Category = thing.Category.Name, Description = (string)thing.Description, Date = (DateTime)thing.Date, Amount = (decimal)thing.Amount, PhotoUrl = thing.PhotoUrl }; XpList.Add(Xp); } return(View(XpList)); }
public async Task ExpenseEditTest() { // Arrange var optionBuilder = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase("testDb"); var dbContext = new ApplicationDbContext(optionBuilder.Options); var service = new ExpenseService(dbContext); var model = new ExpenseInputViewModel { ExpenceDate = DateTime.UtcNow.Date, ExpenceDescription = "заповед за изпълнение", Payee = "РРС100", ExpenceValue = 27 }; await service.CreateAsync(model); int modelId = await dbContext.Expenses.Where(x => x.Id != 0).Select(x => x.Id).FirstOrDefaultAsync(); var editModel = new ExpenseEditViewModel { Id = modelId, ExpenceDate = model.ExpenceDate.AddYears(1), ExpenceDescription = "заповед за изпълнение+editted", ExpenceValue = model.ExpenceValue + 100, Payee = "РРС100+editted", }; // Act var result = service.Edit(editModel); // Assert Assert.NotNull(result); // Assert.True(result.IsCompletedSuccessfully); }
public ExpenseEditPage(ExpenseEditViewModel viewModel) { InitializeComponent(); BindingContext = this.viewModel = viewModel; }