public void OnActionExecuting(ActionExecutingContext context) { var budgetId = (int)context.ActionArguments["id"]; if (!_service.DoesBudgetExist(budgetId)) { context.Result = new NotFoundResult(); } }
public async Task <IActionResult> DeleteBudget(int id) { // Authorization var budget = _service.GetBudget(id); var authResult = await _authService.AuthorizeAsync(User, budget, "CanViewBudget"); if (!authResult.Succeeded) { _log.LogWarning($"{_userService.GetUserName(User)} tried to delete budget #{id}, but wasn't authorized."); return(Forbid()); } var model = _service.GetBudgetDetail(id); if (!_service.DoesBudgetExist(id)) { _log.LogWarning($"User with ID '{_userService.GetUserName(User)}' attempted to delete Budget #{id}, but it wasn't found."); return(NotFound()); } _service.DeleteBudget(id); return(RedirectToAction(nameof(ViewBudgets))); }
public void TestDoesBudgetExist() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(connection) .Options; // Insert seed data into the database using one instance of the context using (var context = new ApplicationDbContext(options)) { context.Database.EnsureCreated(); context.Budgets.Add(new UserBudget { BudgetId = 1, Name = "Budget1" }); context.SaveChanges(); } // should return true when budget was persisted using (var context = new ApplicationDbContext(options)) { var service = new BudgetService(context); var exist = service.DoesBudgetExist(1); Assert.True(exist); } // should return false when budget doesn't exist using (var context = new ApplicationDbContext(options)) { var service = new BudgetService(context); var exist = service.DoesBudgetExist(6); Assert.False(exist); } }