public IActionResult Create(SolvingViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var sqlTask = _context.SqlTasks .FirstOrDefault(t => t.Id == model.SqlTaskId); if (sqlTask == null) { ModelState.AddModelError("task", $"Task {model.SqlTaskId} not found!"); return(View(model)); } var newSolving = new SqlSolving { SqlTaskId = sqlTask.Id, IsCorrect = false, ApplicationUserId = Me.Id, Solving = model.MySolving, SolvedAt = DateTime.Now }; _context.SqlSolvings.Add(newSolving); var completeQuery = model.Creates + Sep + model.Inserts + Sep + model.MySolving; var myOutput = ""; _sandboxService.TryExecuteQuery(completeQuery, ref myOutput); _sandboxService.FlushDatabase(); if (myOutput == sqlTask.SolvingOutput) { newSolving.IsCorrect = true; _context.SaveChanges(); return(RedirectToAction("Index")); } ModelState.AddModelError("sql", myOutput); _context.SaveChanges(); return(View(model)); }
public IActionResult Create(TaskViewModel model) { var sep = "\n\n\n"; string output = ""; var completeQuery = model.Creates + sep + model.Inserts + sep + model.Solving; var succeed = _sandboxService.TryExecuteQuery(completeQuery, ref output); _sandboxService.FlushDatabase(); if (succeed) { var newSqlTask = new SqlTask { Name = model.Name, Description = model.Description, Creates = model.Creates, Inserts = model.Inserts, Solving = model.Solving, IsWriteAction = model.IsWriteAction, SolvingOutput = output }; _context.SqlTasks.Add(newSqlTask); _context.SaveChanges(); return(RedirectToAction("Index")); } else { ModelState.AddModelError("sql", output); return(View(model)); } }