Ejemplo n.º 1
0
        public async Task <IActionResult> RunCode(ViewModels.RunSubmissionViewModel runSubmission)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
                return(BadRequest(new { errors = errors }));
            }

            if (runSubmission.TaskId == null)
            {
                var viewModelResult = new ViewModels.CompilationResult {
                    IsCopileSuccessful     = false,
                    CompilationFailMessage = "Select a task.",
                    TestCases = null
                };
                return(PartialView("SubmissionCompile", viewModelResult));
            }
            var result = await _submissionService.GetJDoodleResult(runSubmission.Code);

            if (result.output.Contains("Compilation failed"))
            {
                var viewModelResult = new ViewModels.CompilationResult {
                    IsCopileSuccessful     = false,
                    CompilationFailMessage = result.output,
                    TestCases = null
                };

                var userSubmission = new ViewModels.SubmissionViewModel {
                    Nickname   = runSubmission.Nickname,
                    Submission = runSubmission.Code,
                    Task       = runSubmission.TaskId.Value,
                    IsSuccess  = false
                };

                await _submissionService.SaveUserSubmission(userSubmission);

                return(PartialView("SubmissionCompile", viewModelResult));
            }
            else
            {
                var viewModel = new ViewModels.CompilationResult {
                    IsCopileSuccessful   = true,
                    UserSubmissionResult = result.output.Trim()
                };

                viewModel.TestCases = await _submissionService.GetTestCases(runSubmission.TaskId.Value, result.output.Trim());

                var userSubmission = new ViewModels.SubmissionViewModel {
                    Nickname   = runSubmission.Nickname,
                    Submission = runSubmission.Code,
                    Task       = runSubmission.TaskId.Value,
                    IsSuccess  = !viewModel.TestCases.Any(x => x.IsTestPassed == false)
                };

                await _submissionService.SaveUserSubmission(userSubmission);

                return(PartialView("SubmissionCompile", viewModel));
            }
        }
Ejemplo n.º 2
0
        public async Task SaveUserSubmission(ViewModels.SubmissionViewModel submission)
        {
            var submitionFilename = Path.Combine("..\\..\\..\\UserSubmissions", GenerateFilename(submission.Nickname, submission.Task));

            File.WriteAllLines(Path.Combine(_basePath, submitionFilename), submission.Submission.Split("\n"));

            var userSubmission = new Models.UserSubmission {
                IsSuccess = submission.IsSuccess,
                Nickname  = submission.Nickname,
                UserSubmissionFilePath = submitionFilename,
                TaskId = submission.Task
            };

            _dbContext.UsersSubmissions.Add(userSubmission);
            await _dbContext.SaveChangesAsync();
        }