public int SaveSolution(Solution Solution) { if (Solution.SolutionID == 0) { Solution = context.Solutions.Add(Solution); } else { context.Entry(Solution).State = EntityState.Modified; } context.SaveChanges(); context.Entry(Solution).Reload(); return Solution.SolutionID; }
public ActionResult AddSolution(AddSolutionViewModel Model) { if (ModelState.IsValid) { if (Model.SolutionFile.ContentLength > 262144) { TempData["ErrorMessage"] = "Максимальный размер файла - 256 кб"; return RedirectToAction("Problem", new { tournamentID = Model.TournamentID, problemID = Model.ProblemID }); } Tournament tournament = repository.Tournaments.FirstOrDefault(t => t.TournamentID == Model.TournamentID); Problem problem = repository.Problems.FirstOrDefault(p => p.ProblemID == Model.ProblemID); if (tournament != null && problem != null && (Roles.IsUserInRole("Judge") || Roles.IsUserInRole("Administrator") || (tournament.EndDate >= DateTime.Now && tournament.StartDate <= DateTime.Now))) { for (int i = 0; i < 1; i++) { int userID = WebSecurity.GetUserId(User.Identity.Name); Solution solution = new Solution { UserID = userID, TournamentID = Model.TournamentID, ProblemID = Model.ProblemID, FileName = Path.GetFileName(Model.SolutionFile.FileName), DataType = Model.SolutionFile.ContentType, ProgrammingLanguage = (ProgrammingLanguages)Model.ProgrammingLanguageID, SendTime = DateTime.Now, Result = TestResults.Waiting }; repository.SaveSolution(solution); string relativePath = Path.Combine( LocalPath.RelativeSolutionsDirectory, userID.ToString(), solution.ProblemID.ToString()); string absolutePath = Path.Combine( LocalPath.AbsoluteSolutionsDirectory, userID.ToString(), solution.ProblemID.ToString()); if (!Directory.Exists(absolutePath)) Directory.CreateDirectory(absolutePath); string fileName = solution.SolutionID.ToString(); absolutePath = Path.Combine(absolutePath, fileName); relativePath = Path.Combine(relativePath, fileName); Model.SolutionFile.SaveAs(absolutePath); solution.Path = relativePath; if (problem.CheckPending == true) { solution.Result = TestResults.CHKP; } repository.SaveSolution(solution); if (problem.CheckPending == false) { TestersSingleton.Instance.AddSolutionForChecking( Model.ProblemID, solution.SolutionID, solution.ProgrammingLanguage, tournament.Format, problem.Type, relativePath, solution.FileName); } logger.Info("User " + WebSecurity.GetUserId(User.Identity.Name) + " \"" + User.Identity.Name + "\" add solution: PL = " + solution.ProgrammingLanguage + ", TournamentID = " + Model.TournamentID + ", ProblemID = " + Model.ProblemID + ", SolutionID = " + solution.SolutionID); } } } return RedirectToAction("Problem", new { tournamentID = Model.TournamentID, problemID = Model.ProblemID }); }
public void DeleteSolutionTestResults(Solution Solution) { foreach (var item in context.SolutionTestResults.Where(str => str.SolutionID == Solution.SolutionID)) { context.Entry(item).State = EntityState.Deleted; } context.SaveChanges(); return; }