Exemplo n.º 1
0
        public async Task <IActionResult> Submit(IFormFile file, int submissionId)
        {
            AddSubmissionFileDto dto = new AddSubmissionFileDto {
                File = file, SubmissionId = submissionId
            };
            ServiceResponse <string> response = await _submissionService.SubmitAssignment(dto);

            if (response.Success)
            {
                return(Ok(response));
            }
            return(NotFound(response));
        }
Exemplo n.º 2
0
        public async Task <ServiceResponse <string> > SubmitAssignment(AddSubmissionFileDto dto)
        {
            ServiceResponse <string> response = new ServiceResponse <string>();

            if (dto == null)
            {
                response.Data    = null;
                response.Message = "There is no data";
                response.Success = false;
                return(response);
            }
            Submission submission = await _context.Submissions.Include(s => s.AffiliatedAssignment).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 submit file to this assignment";
                response.Success = false;
                return(response);
            }
            if (submission.HasFile && DateTime.Compare(submission.AffiliatedAssignment.DueDate, DateTime.Now) < 0)
            {
                response.Data    = "Not allowed";
                response.Message = "You can not change your submission after deadline";
                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));

            Directory.CreateDirectory(target);
            if (dto.File.Length <= 0)
            {
                response.Success = false;
            }
            else
            {
                string oldfile   = Directory.GetFiles(target).FirstOrDefault();
                string extension = Path.GetExtension(dto.File.FileName);
                Course course    = await _context.Courses.FirstOrDefaultAsync(c => c.Id == submission.CourseId);

                Section section = await _context.Sections.FirstOrDefaultAsync(s => s.Id == submission.SectionId);

                var filePath = Path.Combine(target, string.Format("{0}_Section{1}_Group{2}_{3}"
                                                                  , course.Name.Trim().Replace(" ", "_"), section.SectionNo, submission.AffiliatedGroupId,
                                                                  submission.AffiliatedAssignment.Title.Trim().Replace(" ", "_")) + extension);
                submission.FilePath  = filePath;
                submission.UpdatedAt = DateTime.Now;
                submission.HasFile   = true;
                if (File.Exists(oldfile))
                {
                    File.Delete(oldfile);
                }
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await dto.File.CopyToAsync(stream);
                }
                response.Data    = target;
                response.Message = "file succesfully saved.";
                _context.Submissions.Update(submission);
                await _context.SaveChangesAsync();
            }

            return(response);
        }