Beispiel #1
0
        public async Task <ServiceResponse <string> > DeleteFile(DeleteAssignmentFileDto dto)
        {
            ServiceResponse <string> response = new ServiceResponse <string>();
            User user = await _context.Users.Include(u => u.ProjectGroups).ThenInclude(pgu => pgu.ProjectGroup).FirstOrDefaultAsync(u => u.Id == GetUserId());

            Assignment assignment = await _context.Assignments.FirstOrDefaultAsync(a => a.Id == dto.AssignmentId);

            if (assignment == null || assignment.FilePath == null)
            {
                response.Data    = "No submission";
                response.Message = "There is no submission under this id";
                response.Success = false;
                return(response);
            }
            Course course = await _context.Courses.Include(pg => pg.Instructors).FirstOrDefaultAsync(c => c.Id == assignment.AfilliatedCourseId && c.Instructors.Any(pgu => pgu.UserId == user.Id));

            if (course == null)
            {
                response.Data    = "Not allowed";
                response.Message = "You are not allowed to post file for this assignment";
                response.Success = false;
                return(response);
            }
            File.Delete(assignment.FilePath);
            assignment.FilePath  = null;
            assignment.CreatedAt = DateTime.MinValue;
            assignment.HasFile   = false;
            response.Message     = "file succesfully deleted.";
            _context.Assignments.Update(assignment);
            await _context.SaveChangesAsync();

            return(response);
        }
Beispiel #2
0
        public async Task <IActionResult> DeleteFile(int assignmentId)
        {
            DeleteAssignmentFileDto dto = new DeleteAssignmentFileDto {
                AssignmentId = assignmentId
            };
            ServiceResponse <string> response = await _assignmentService.DeleteFile(dto);

            if (response.Success)
            {
                return(Ok(response));
            }
            return(NotFound(response));
        }