public async Task <IActionResult> DeleteFile(int commentId) { DeleteCommentFileDto dto = new DeleteCommentFileDto { CommentId = commentId }; ServiceResponse <string> response = await _commentService.DeleteFile(dto); if (response.Success) { return(Ok(response)); } return(NotFound(response)); }
public async Task <ServiceResponse <string> > DeleteFile(DeleteCommentFileDto dto) { ServiceResponse <string> response = new ServiceResponse <string>(); User user = await _context.Users.FirstOrDefaultAsync(u => u.Id == GetUserId()); Comment comment = await _context.Comments.Include(c => c.CommentedSubmission).FirstOrDefaultAsync(s => s.Id == dto.CommentId); if (comment == null) { response.Data = null; response.Message = "There is no comment with this Id"; response.Success = false; return(response); } if (comment.CommentedUserId != GetUserId()) { response.Data = null; response.Message = "You are not authorized for this endpoint"; response.Success = false; return(response); } if (!comment.FileAttachmentAvailability) { response.Data = null; response.Message = "This user has not yet submitted his comment as a file for this group yet"; response.Success = false; return(response); } var target = Path.Combine(_hostingEnvironment.ContentRootPath, string.Format("{0}/{1}/{2}/{3}/{4}", "StaticFiles/Feedbacks", comment.CommentedSubmission.CourseId, comment.CommentedSubmission.SectionId, comment.CommentedSubmissionId, user.Id)); Directory.CreateDirectory(target); var filePath = Path.Combine(target, user.Name.Trim().Replace(" ", "_") + "_Feedback.pdf"); comment.FilePath = null; comment.FileAttachmentAvailability = false; File.Delete(filePath); response.Data = target; response.Message = "file succesfully deleted."; comment.CreatedAt = DateTime.MinValue; _context.Comments.Update(comment); await _context.SaveChangesAsync(); return(response); }