public async Task <IActionResult> DeleteAsync(OpportunityDeleteViewModel model) { var opportunity = _context.Opportunities .Where(x => x.Id == model.Id) .SingleOrDefault(); if (opportunity == null) { return(RedirectToAction(nameof(Index))); } var volunteerHours = _context.VolunteerHours .Where(x => x.OpportunityId == opportunity.Id) .Count(); if (volunteerHours > 0) { TempData["Messages"] = $"\"{opportunity.Name}\" has volunteer hours logged against it and cannot be deleted."; return(RedirectToAction(nameof(Index))); //return RedirectToAction(nameof(Archive), new { opportunity.Id }); } var applications = _context.Applications .Where(x => x.Opportunity == opportunity) .Count(); if (applications > 0) { TempData["Messages"] = $"\"{opportunity.Name}\" has applications and cannot be deleted."; return(RedirectToAction(nameof(Index))); } _context.Remove(opportunity); await _context.SaveChangesAsync(); TempData["Messages"] = $"\"{opportunity.Name}\" has been deleted."; return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> DeleteAsync(OpportunityDeleteViewModel model) { var user = await _userManager.GetUserAsync(HttpContext.User); var organizationsManagedByUser = _context.OrganizationAdministratorUsers .Where(x => x.UserId == user.Id) .Select(x => x.OrganizationId) .ToList(); var opportunity = _context.Opportunities .Include(x => x.Organization) .Where(x => x.Id == model.Id && organizationsManagedByUser.Contains(x.Organization.Id)) .SingleOrDefault(); if (opportunity == null) { TempData["Messages"] = $"\"{opportunity.Name}\" not found for organizations you manage."; return(RedirectToAction(nameof(Index))); } var volunteerHours = _context.VolunteerHours .Where(x => x.OpportunityId == opportunity.Id) .ToList(); if (volunteerHours.Count > 0) { TempData["Messages"] = $"\"{opportunity.Name}\" has volunteer hours logged against it and cannot be deleted."; return(RedirectToAction(nameof(Index))); //return RedirectToAction(nameof(Archive), new { opportunity.Id }); } _context.Remove(opportunity); await _context.SaveChangesAsync(); TempData["Messages"] = $"\"{opportunity.Name}\" has been deleted."; return(RedirectToAction(nameof(Index))); }