public async Task <IActionResult> Delete(DeleteCategoryInputModel input) { try { var user = await this.userManager.GetUserAsync(this.User); if (!this.ModelState.IsValid) { if (!await this.walletsService.IsUserOwnWalletAsync(user.Id, input.WalletId)) { return(this.Redirect($"/Home/Error?message={GlobalConstants.NoPermissionForEditWallet}")); } if (!await this.categoriesService.IsUserOwnCategoryAsync(user.Id, input.OldCategoryId)) { return(this.Redirect($"/Home/Error?message={GlobalConstants.NoPermissionForViewOrEditCategory}")); } var modelInfo = await this.categoriesService.GetCategoryInfoForDeleteAsync(input.OldCategoryId, input.WalletId); input.WalletId = modelInfo.WalletId; input.WalletName = modelInfo.WalletName; input.OldCategoryName = modelInfo.OldCategoryName; input.OldCategoryBadgeColor = Enum.Parse <BadgeColor>(modelInfo.OldCategoryBadgeColor.ToString()); input.Categories = modelInfo.Categories .Select(c => new DeleteCategoryNameAndIdViewModel { BadgeColor = Enum.Parse <BadgeColor>(c.BadgeColor.ToString()), CategoryId = c.CategoryId, CategoryName = c.CategoryName, }) .ToList(); return(this.View(input)); } if (!await this.walletsService.IsUserOwnWalletAsync(user.Id, input.WalletId)) { return(this.Redirect($"/Home/Error?message={GlobalConstants.NoPermissionForEditWallet}")); } if (!await this.categoriesService.IsUserOwnCategoryAsync(user.Id, input.OldCategoryId)) { return(this.Redirect($"/Home/Error?message={GlobalConstants.NoPermissionForViewOrEditCategory}")); } if (!await this.categoriesService.IsUserOwnCategoryAsync(user.Id, input.NewCategoryId) && input.NewCategoryId != -1) { return(this.Redirect($"/Home/Error?message={GlobalConstants.NoPermissionForViewOrEditCategory}")); } await this.categoriesService.RemoveAsync(input.OldCategoryId, input.NewCategoryId); return(this.Redirect($"/Wallets/Categories/{input.WalletId}")); } catch (Exception ex) { return(this.Redirect($"/Home/Error?message={ex.Message}")); } }
public async Task DeleteShouldDeleteCategoryAndReturnRedirect() { // Arrange this.FillDatabase(); this.deleteModel = new DeleteCategoryInputModel { OldCategoryId = 4, OldCategoryName = "Parties", WalletId = 5, WalletName = "Holiday Wallet", NewCategoryId = -1, OldCategoryBadgeColor = ViewModels.Records.Enums.BadgeColor.Warning, }; // Act var result = await this.controller.Delete(this.deleteModel); // Assert var redirectResult = Assert.IsType <RedirectResult>(result); Assert.Equal("W", redirectResult.Url[1].ToString()); var category = this.db.Categories.FirstOrDefault(c => c.Id == 4); Assert.Null(category); Assert.Equal(1, this.db.Wallets.FirstOrDefault(w => w.Id == 5).Categories.Count()); }
public async Task <IActionResult> Delete(DeleteCategoryInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } await this.categoryService.DeleteCategoryAsync(input.Id); this.TempData[SuccessNotification] = SuccessfullyDeletedCategory; return(this.RedirectToAction("All")); }
public async Task <IActionResult> Delete(DeleteCategoryInputModel input) { var category = await this .categoriesService .GetByIdAsync <DeleteCategoryInputModel>(input.Id); if (category == null) { return(this.NotFound()); } await this.categoriesService.DeleteAsync(input.Id); return(this.RedirectToAction("/")); }
public IActionResult Delete(DeleteCategoryInputModel model) { return(null); }