예제 #1
0
        public async Task <IActionResult> SubmitAssignment([FromForm] AddSubmissionwithAttachmentDto addwithAttachment)
        {
            ServiceResponse <GetSubmissionDto> response = await _submissionService.AddSubmission(addwithAttachment.addSubmissionDto);

            if (response.Success)
            {
                if (addwithAttachment.file != null)
                {
                    await _submissionService.SubmitAssignment(new AddSubmissionFileDto { File = addwithAttachment.file, SubmissionId = response.Data.Id });
                }
                return(Ok(response));
            }
            return(NotFound(response));
        }
예제 #2
0
        public HttpResponseMessage Create([FromBody] SubmissionAPIShortModel submissionAPIShortModel)
        {
            var submission = _submissionService.GetByAssigAndStudent(submissionAPIShortModel.AssignmentName, submissionAPIShortModel.StudentUsername);

            //if (submission.Assignment != null || submission.Student != null)
            //{
            //    return Request.CreateErrorResponse(HttpStatusCode.Conflict, "Submission already created!");
            //}

            //else
            if (submissionAPIShortModel == null)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
            else
            {
                var att1 = _submissionAPIShortModel.Map(submissionAPIShortModel);
                var att  = _submissionAPIMapper.Map(att1);
                _submissionService.AddSubmission(att);
                return(Request.CreateResponse(HttpStatusCode.Created, _submissionAPIMapper.Map(_submissionAPIShortModel.Map(submissionAPIShortModel))));
            }
        }
예제 #3
0
        public async Task <ServiceResponse <GetAssignmentDto> > SubmitAssignment(AddAssignmentDto assignmentDto)
        {
            ServiceResponse <GetAssignmentDto> response = new ServiceResponse <GetAssignmentDto>();

            if (assignmentDto == null)
            {
                response.Data    = null;
                response.Message = "There is no data";
                response.Success = false;
                return(response);
            }
            Course course = await _context.Courses.Include(c => c.Instructors).Include(c => c.Sections).ThenInclude(s => s.ProjectGroups)
                            .FirstOrDefaultAsync(c => c.Id == assignmentDto.CourseId);

            if (course == null)
            {
                response.Data    = null;
                response.Message = "There is no course under this id";
                response.Success = false;
                return(response);
            }
            if (course.Instructors.FirstOrDefault(u => u.UserId == GetUserId()) == null)
            {
                response.Data    = null;
                response.Message = "You are not allowed to post assignments for this course";
                response.Success = false;
                return(response);
            }
            if (assignmentDto.Title == null)
            {
                response.Data    = null;
                response.Message = "You should add a title to your submission";
                response.Success = false;
                return(response);
            }
            if (assignmentDto.AssignmenDescription == null)
            {
                response.Data    = null;
                response.Message = "You should add a description to your submission";
                response.Success = false;
                return(response);
            }
            if ((assignmentDto.DueDate - DateTime.Now).TotalDays < 3)
            {
                response.Data    = null;
                response.Message = "A deadline cannot be closer to publishment date less than 3 days";
                response.Success = false;
                return(response);
            }
            Assignment assignment = new Assignment
            {
                CreatedAt              = DateTime.Now,
                DueDate                = assignmentDto.DueDate,
                AfilliatedCourseId     = assignmentDto.CourseId,
                AcceptedTypes          = assignmentDto.AcceptedTypes,
                AssignmentDescription  = assignmentDto.AssignmenDescription,
                CanBeGradedByStudents  = assignmentDto.CanBeGradedByStudents,
                IsItGraded             = assignmentDto.IsItGraded,
                VisibilityOfSubmission = assignmentDto.VisibiltyOfSubmission,
                MaxFileSizeInBytes     = assignmentDto.MaxFileSizeInBytes,
                HasFile                = false,
                FilePath               = "",
                Title = assignmentDto.Title
            };

            await _context.Assignments.AddAsync(assignment);

            await _context.SaveChangesAsync();

            foreach (Section s in course.Sections)
            {
                foreach (ProjectGroup pg in s.ProjectGroups)
                {
                    await _submissionService.AddSubmission(new AddSubmissionDto
                    {
                        AssignmentId   = assignment.Id,
                        ProjectGroupId = pg.Id
                    });
                }
            }

            response.Data = _mapper.Map <GetAssignmentDto>(assignment);
            response.Data.FileEndpoint = string.Format("Assignment/File/{0}", assignment.Id);
            return(response);
        }
예제 #4
0
 public void Post([FromBody] SubmissionModel data)
 {
     SubmissionService.AddSubmission(data);
 }
        public async Task <IActionResult> DetailsAsync(IFormFile file, String taskId, SubmissionViewModel data)
        {
            try
            {
                data.TaskId        = Guid.Parse(Encryption.SymmetricDecrypt(taskId));
                data.UploaderEmail = User.Identity.Name;
                data.DateUploaded  = DateTime.Now;
                if (file != null)
                {
                    if (System.IO.Path.GetExtension(file.FileName) == ".pdf" && file.Length < 1048576)
                    {
                        //FF D8 >>>>> 255 216
                        string uniqueFilename;
                        //byte[] whitelist = new byte[] { 255, 216 };
                        byte[] whitelist = new byte[] { 37, 80 };

                        if (file != null)
                        {
                            MemoryStream userFile = new MemoryStream();

                            using (var f = file.OpenReadStream())
                            {
                                byte[] buffer = new byte[2];
                                f.Read(buffer, 0, 2);
                                for (int i = 0; i < whitelist.Length; i++)
                                {
                                    if (whitelist[i] == buffer[i])
                                    {
                                    }
                                    else
                                    {
                                        TempData["Error"] = "Task Not Submitted";
                                        ModelState.AddModelError("file", "File Not Valid");
                                        return(View());
                                    }
                                }

                                f.CopyTo(userFile);

                                string hash = Encryption.Hash(userFile.ToString());

                                f.Position = 0;

                                uniqueFilename = Guid.NewGuid() + Path.GetExtension(file.FileName);
                                data.FilePath  = uniqueFilename;
                                string absolutePath = _host.WebRootPath + @"\submissions\" + uniqueFilename;
                                try
                                {
                                    using (FileStream fsOut = new FileStream(absolutePath, FileMode.CreateNew, FileAccess.Write))
                                    {
                                        f.CopyTo(fsOut);
                                    }
                                    f.Close();



                                    file.CopyTo(userFile);
                                    data.Hash = hash;

                                    ApplicationUser user = await _userManager.FindByEmailAsync(User.Identity.Name);

                                    data.Signature = Encryption.SignData(userFile, user.PrivateKey);

                                    TempData["message"] = "Task Submitted Successfully";
                                    _submissionService.AddSubmission(data);
                                }
                                catch (Exception ex)
                                {
                                    TempData["error"] = "Error Uploading File";
                                    _logger.LogError(ex, "Error While Saving File");
                                    return(View("Error", new ErrorViewModel()
                                    {
                                        Message = "Error While Saving File. Try Again Later" + ex
                                    }));
                                }
                            }
                        }
                    }
                }

                return(Redirect("/Tasks/Index"));
            }
            catch (Exception e)
            {
                TempData["error"] = "Task Not Submitted";
                return(Redirect("/Tasks/Index"));
            }
        }