public async Task <IActionResult> DeleteSubmissionFile(int submissionId) { DeleteSubmissionFileDto dto = new DeleteSubmissionFileDto { SubmissionId = submissionId }; ServiceResponse <string> response = await _submissionService.DeleteSubmissionFile(dto); if (response.Success) { return(Ok(response)); } return(NotFound(response)); }
public async Task <ServiceResponse <string> > DeleteSubmissionFile(DeleteSubmissionFileDto dto) { ServiceResponse <string> response = new ServiceResponse <string>(); Submission submission = await _context.Submissions.FirstOrDefaultAsync(s => s.Id == dto.SubmissionId); if (submission == null) { response.Data = "Bad Request"; response.Message = "There is no submission with this Id"; response.Success = false; return(response); } User user = await _context.Users.Include(u => u.ProjectGroups).FirstOrDefaultAsync(ss => ss.Id == GetUserId()); if (!user.ProjectGroups.Any(pgu => pgu.ProjectGroupId == submission.AffiliatedGroupId)) { response.Data = "Not allowed"; response.Message = "You are not allowed to delete file for this submission"; response.Success = false; return(response); } if (!submission.HasFile) { response.Data = "Not found"; response.Message = "There is no submission file for this submission"; response.Success = false; return(response); } var target = Path.Combine(_hostingEnvironment.ContentRootPath, string.Format("{0}/{1}/{2}/{3}/{4}", "StaticFiles/Submissions", submission.CourseId, submission.SectionId, submission.AffiliatedAssignmentId, submission.AffiliatedGroupId)); var filePath = Directory.GetFiles(target).FirstOrDefault(); submission.FilePath = null; submission.HasFile = false; File.Delete(filePath); response.Data = target; response.Message = "file succesfully deleted."; submission.UpdatedAt = DateTime.Now; _context.Submissions.Update(submission); await _context.SaveChangesAsync(); return(response); }