Example #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            var task = await _context.Tasks.FindAsync(TaskId);

            if (SourceCodeFile == null || SourceCodeFile.Length == 0 || task.Contest.Status == Contest.ContestState.Completed)
            {
                return(RedirectToPage());
            }

            string sourceCode;

            using (var stream = SourceCodeFile.OpenReadStream())
                sourceCode = GetSourceCode(stream);

            var fileName  = Path.GetFileName(SourceCodeFile.FileName);
            var extension = Path.GetExtension(fileName).Substring(1);

            ProgrammingLanguage language;

            try
            {
                language = ProgrammingLanguageFactory.Get(extension);
            }
            catch
            {
                return(RedirectToPage());
            }

            var username = User.Identity.Name;
            var command  = CreateSolutionCommand.Create(sourceCode, language.LanguageCode, task.Id, username);
            await _createSolutionCommandHandler.HandleAsync(command);

            return(RedirectToPage());
        }
        public async Task HandleAsync(VerifySolutionCommand command)
        {
            var solution = await _dbContext.Solutions.SingleAsync(s => s.Id == command.SolutionId);

            if (solution.CompilationResults.Count > 0 || solution.Logs.Count > 0)
            {
                return;
            }

            var tests = solution.Task.Tests.ToList();
            var programmingLanguage = ProgrammingLanguageFactory.Get(solution.Language);

            foreach (var test in tests)
            {
                var query       = CompilationQuery.Create(solution.SourceCode, test.Input, programmingLanguage);
                var queryResult = await _compilationQueryHandler.HandleAsync(query);

                if (queryResult.ExecutionSuccessful)
                {
                    var compilationResult = new CompilationResultMapper(queryResult.Output).Map();
                    compilationResult.Solution = solution;
                    compilationResult.Test     = test;
                    await _dbContext.CompilationResults.AddAsync(compilationResult);
                }
                else
                {
                    if (queryResult.Error.Error == "offline")
                    {
                        throw new CompilationServiceOfflineException("Compilation service not available.");
                    }

                    var log = new LogMapper(queryResult.Error).Map();
                    log.Solution = solution;
                    log.Test     = test;
                    await _dbContext.Logs.AddAsync(log);
                }
            }

            await _dbContext.SaveChangesAsync();
        }