public IActionResult Delete(int id, string redirect) { int userId = int.Parse(HttpContext.User.FindFirstValue("Id")); Question q = _questionsService.GetOne(id); try { foreach (var a in _answersService.GetAll(new IAnswersService.GetAllOptions { QuestionId = q.Id })) { if (!string.IsNullOrEmpty(a.Image)) { _storageService.Delete(a.Image); } } foreach (var a in _answersService.GetAll(new IAnswersService.GetAllOptions { QuestionId = q.Id })) { _commentsService.DeleteAll(userId, ICommentsService.CommentType.Answer, a.Id); } _answersService.DeleteAll(userId, id); if (!string.IsNullOrEmpty(q.Image)) { _storageService.Delete(q.Image); } _commentsService.DeleteAll(userId, ICommentsService.CommentType.Question, id); _questionsTagsService.DeleteAll(q.Id); _questionsService.Delete(userId, q.Id); } catch (AskMateNotAuthorizedException) { return(Forbid()); } if (redirect == null) { return(Redirect(Request.Headers["Referer"])); } return(LocalRedirect(redirect)); }
public IActionResult Delete(int id) { int userId = int.Parse(HttpContext.User.FindFirstValue("Id")); Answer a = _answersService.GetOne(id); try { if (!string.IsNullOrEmpty(a.Image)) { _storageService.Delete(a.Image); } _commentsService.DeleteAll(userId, ICommentsService.CommentType.Answer, id); _answersService.Delete(userId, a.Id); } catch (AskMateNotAuthorizedException) { return(Forbid()); } return(Redirect(Request.Headers["Referer"])); }