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 void CreateSampleSolution_SolutionPresentInDatabase() { var givenSourceCode = "{...}"; var givenTaskId = SampleTask.Id; var givenLanguage = "cpp"; var givenUsername = SampleUser.UserName; var command = CreateSolutionCommand.Create(givenSourceCode, givenLanguage, givenTaskId, givenUsername); Sut.HandleAsync(command).Wait(); var solutions = Context.Solutions .Where(s => s.Task == SampleTask); solutions.Any(s => s.IdentityUser == SampleUser && s.SourceCode == givenSourceCode && s.Language == givenLanguage) .Should().BeTrue(); }
public async Task <List <AnswerOptionsDto> > Handle(CreateSolutionCommand request, CancellationToken cancellationToken) { var quiz = await _context.Quizzes .Where(x => x.Id == request.QuizId) .FirstOrDefaultAsync(); if (quiz == null) { throw new RestException(HttpStatusCode.NotFound, "Quiz Not Found"); } var solution = new Solution { Quiz = quiz, CreatedAt = DateTime.Now }; await using var transaction = await _context.Database.BeginTransactionAsync(); try { await _context.Solutions.AddAsync(solution); if (await _context.SaveChangesAsync() < 1) { await transaction.RollbackAsync(); throw new RestException(HttpStatusCode.BadRequest, "Error savig solution changes"); } foreach (var answerOptions in request.AnswerOptions) { var question = await _context.Questions .Where(x => x.Id == answerOptions.QuestionId) .FirstOrDefaultAsync(); if (question == null) { await transaction.RollbackAsync(); throw new RestException(HttpStatusCode.NotFound, "Error question not found"); } var answer = new Answer(); if (answerOptions.Description != null) { answer.Description = answerOptions.Description; } answer.Solution = solution; answer.Question = question; _context.Answers.AddAsync(answer); if (await _context.SaveChangesAsync() < 1) { await transaction.RollbackAsync(); throw new RestException(HttpStatusCode.BadRequest, "Error savig solution changes"); } answerOptions.AnswerId = answer.Id; if (answerOptions.OptionsId != null) { foreach (var optionId in answerOptions.OptionsId) { var option = await _context.Options .Where(x => x.Id == optionId) .FirstOrDefaultAsync(); if (option == null) { await transaction.RollbackAsync(); throw new RestException(HttpStatusCode.NotFound, "Error invalid option changes"); } await _context.AnswerOptions.AddAsync(new AnswerOption { Answer = answer, Option = option }); } if (await _context.SaveChangesAsync() < 1) { await transaction.RollbackAsync(); throw new RestException(HttpStatusCode.BadRequest, "Error savig answer option changes changes"); } } answerOptions.SolutionId = solution.Id; answerOptions.CreatedAt = solution.CreatedAt; } await transaction.CommitAsync(); return(request.AnswerOptions); } catch (Exception e) { Console.WriteLine(e); throw; } }