Пример #1
0
        public async Task <ValidationResult> Validate(Sql.Models.Problem problem, string solution)
        {
            var testCaseResults = new List <TestCaseResult>();

            var solutionId = await WriteTestCases(problem.TestCases, solution);

            Thread.Sleep(500);
            var output = await _azureFunctionsService.StartFunction(solutionId);

            await _azureFunctionsService.DeleteFunction("/" + solutionId);

            try
            {
                var pesterResults = JsonConvert.DeserializeObject <List <PesterResult> >(JsonConvert.DeserializeObject <string>(output));
                foreach (var pesterResult in pesterResults)
                {
                    testCaseResults.Add(new TestCaseResult(pesterResult.Message, pesterResult.Success == "True"));
                }
            }
            catch
            {
                var pesterResult = JsonConvert.DeserializeObject <PesterResult>(output);
                testCaseResults.Add(new TestCaseResult(pesterResult.Message, pesterResult.Success == "True"));
            }

            return(new ValidationResult(testCaseResults));
        }
Пример #2
0
        public async Task <string> EditAsync(int id, Sql.Models.Problem problem)
        {
            if (problem == null)
            {
                throw new ArgumentNullException(nameof(problem));
            }

            if (string.IsNullOrEmpty(problem.Description) || string.IsNullOrWhiteSpace(problem.Name) || !problem.TestCases.Any())
            {
                throw new Exception("All details are required to create a problem");
            }

            var user = await GetRequestUser();

            var existingProblem = await Repository.Problem.Get(id);

            if (existingProblem == null)
            {
                throw new ArgumentNullException(nameof(problem));
            }

            if (user.UserId != existingProblem.Author.UserId)
            {
                throw new Exception("User is not author!");
            }

            existingProblem.TestCases   = problem.TestCases;
            existingProblem.Description = problem.Description;
            existingProblem.Language    = problem.Language;
            existingProblem.Name        = problem.Name;

            await Repository.SaveChangesAsync();

            return(Url.Action("Index", new { problem.ProblemId }));
        }
Пример #3
0
        public async Task <string> PostAsync(Sql.Models.Problem problem)
        {
            if (problem == null)
            {
                throw new ArgumentNullException(nameof(problem));
            }

            if (problem.Language.Length > 15)
            {
                throw new Exception("Language name cannot be over 15 characters.");
            }

            if (string.IsNullOrEmpty(problem.Description) || string.IsNullOrWhiteSpace(problem.Name) || !problem.TestCases.Any())
            {
                throw new Exception("All details are required to create a problem");
            }

            var currentUser = await GetRequestUser();

            problem.Author = currentUser;

            await Repository.Problem.Create(problem);

            return(Url.Action("Single", "Problem", new { problemName = problem.Name }));
        }
Пример #4
0
        public async Task <ValidationResult> Validate(Sql.Models.Problem problem, string solution)
        {
            var testCaseResults = new List <TestCaseResult>();

            foreach (var testCase in problem.TestCases)
            {
                string solutionContent = string.Empty;
                if (string.IsNullOrWhiteSpace(testCase.Input) ||
                    testCase.Input.Equals("None", StringComparison.OrdinalIgnoreCase) ||
                    testCase.Input.Equals("N\\A", StringComparison.OrdinalIgnoreCase) ||
                    testCase.Input.StartsWith("None", StringComparison.OrdinalIgnoreCase))
                {
                }
                else
                {
                    solutionContent  = testCase.Input;
                    solutionContent += Environment.NewLine;
                }

                solutionContent += solution;

                var csharpExectuor = new CSharpExecutor(_azureFunctionsService);
                var output         = await csharpExectuor.Execute(solutionContent);

                testCaseResults.Add(new TestCaseResult(testCase.Output, output.Trim()));
            }

            return(new ValidationResult(testCaseResults));
        }
Пример #5
0
        public async Task <ValidationResult> Validate(string language, Sql.Models.Problem problem, string solution)
        {
            var validator = _validators.FirstOrDefault(m => m.Language.Name.Equals(language, StringComparison.OrdinalIgnoreCase));

            if (validator != null)
            {
                return(await validator.Validate(problem, solution));
            }

            return(null);
        }
Пример #6
0
        private IActionResult ShowProblem(Sql.Models.Problem problem)
        {
            if (problem == null)
            {
                throw new Exception("Problem does not exist!");
            }
            var author = problem.Author;

            var language = _languageFactory.Get(problem.Language);

            return(View("Index", new ProblemDetails(problem, author, language, HttpContext.User.Identity.IsAuthenticated, HttpContext.User.Identity.Name)));
        }