Exemplo n.º 1
0
        public async Task <IActionResult> SubmitAssignment(int courseId, IFormFile assignment)
        {
            var fileExtensions = new[] { ".zip", ".pdf" };

            var checkExtensions = Path.GetExtension(assignment.FileName);

            if (!fileExtensions.Contains(checkExtensions) || assignment.Length > AssignmentConstants.ASSIGNMENT_FILE_LENGTH)
            {
                this.TempData[AssignmentMessagesConstants.TEMPDATA_ERROR_MESSAGE] = AssignmentMessagesConstants.ASSIGNMENT_FILE_EXTENSION_AND_SIZE_MESSAGE;

                return(RedirectToAction(nameof(Details), new { courseId }));
            }

            var assignmentContent = await assignment.ConvetToByteArrayAsync();

            var userId = this.userManager.GetUserId(User);

            var submitAssignment = await this.assignmentService.SaveAssignmentAsync(courseId, userId, assignmentContent);

            if (!submitAssignment)
            {
                return(BadRequest());
            }

            this.TempData[AssignmentMessagesConstants.TEMPDATA_SUCCESS_MESSAGE] = $"'{assignment.FileName}' {AssignmentMessagesConstants.ASSIGNMENT_FILE_SUCCESSFULLY_UPLOADED_MESSAGE}";

            return(RedirectToAction(nameof(Details), new { courseId }));
        }