public ActionResult Edit(int id, DetailedProblemViewModel problem) { if (problem != null && this.ModelState.IsValid) { var existingProblem = this.Data.Problems.All() .FirstOrDefault(x => x.Id == id); existingProblem.Name = problem.Name; existingProblem.MaximumPoints = problem.MaximumPoints; existingProblem.TimeLimit = problem.TimeLimit; existingProblem.MemoryLimit = problem.MemoryLimit; existingProblem.SourceCodeSizeLimit = problem.SourceCodeSizeLimit; existingProblem.ShowResults = problem.ShowResults; existingProblem.ShowDetailedFeedback = problem.ShowDetailedFeedback; existingProblem.Checker = this.Data.Checkers.All().FirstOrDefault(x => x.Name == problem.Checker); existingProblem.OrderBy = problem.OrderBy; this.Data.SaveChanges(); this.TempData.AddInfoMessage(GlobalResource.Problem_edited); return(this.RedirectToAction("Contest", new { id = existingProblem.ContestId })); } problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); return(this.View(problem)); }
private DetailedProblemViewModel PrepareProblemViewModelForCreate(Contest contest) { var problemOrder = GlobalConstants.ProblemDefaultOrderBy; var lastProblem = this.Data.Problems .All() .Where(x => x.ContestId == contest.Id) .OrderByDescending(x => x.OrderBy) .FirstOrDefault(); if (lastProblem != null) { problemOrder = lastProblem.OrderBy + 1; } var problem = new DetailedProblemViewModel(); problem.OrderBy = problemOrder; problem.ContestId = contest.Id; problem.ContestName = contest.Name; this.AddCheckersAndProblemGroupsToProblemViewModel(problem, contest.NumberOfProblemGroups); problem.SubmissionTypes = this.Data.SubmissionTypes .All() .Select(SubmissionTypeViewModel.ViewModel) .ToList(); return(problem); }
public ActionResult Edit(int id, DetailedProblemViewModel problem) { if (!this.CheckIfUserHasProblemPermissions(id)) { this.TempData[GlobalConstants.DangerMessage] = "Нямате привилегиите за това действие"; return(this.RedirectToAction("Index", "Contests", new { area = "Administration" })); } if (problem != null && this.ModelState.IsValid) { var existingProblem = this.Data.Problems.All().FirstOrDefault(x => x.Id == id); existingProblem.Name = problem.Name; existingProblem.MaximumPoints = problem.MaximumPoints; existingProblem.TimeLimit = problem.TimeLimit; existingProblem.MemoryLimit = problem.MemoryLimit; existingProblem.SourceCodeSizeLimit = problem.SourceCodeSizeLimit; existingProblem.ShowResults = problem.ShowResults; existingProblem.ShowDetailedFeedback = problem.ShowDetailedFeedback; existingProblem.Checker = this.Data.Checkers.All().FirstOrDefault(x => x.Name == problem.Checker); existingProblem.OrderBy = problem.OrderBy; existingProblem.SolutionSkeleton = problem.SolutionSkeletonData; existingProblem.ContestId = problem.ContestId; this.Data.SaveChanges(); this.TempData.AddInfoMessage(GlobalResource.Problem_edited); return(this.RedirectToAction("Contest", new { id = existingProblem.ContestId })); } problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); return(this.View(problem)); }
private bool IsValidProblem(DetailedProblemViewModel model) { var isValid = true; if (model.SubmissionTypes == null || !model.SubmissionTypes.Any(s => s.IsChecked)) { this.ModelState.AddModelError(nameof(model.SelectedSubmissionTypes), GlobalResource.Select_one_submission_type); isValid = false; } return(isValid); }
public ActionResult Create(int?id) { if (id == null) { this.TempData.AddDangerMessage(GlobalResource.Invalid_contest); return(this.RedirectToAction(GlobalConstants.Index)); } if (!this.CheckIfUserHasContestPermissions(id.Value)) { this.TempData[GlobalConstants.DangerMessage] = "Нямате привилегиите за това действие"; return(this.RedirectToAction("Index", "Contests", new { area = "Administration" })); } var contest = this.Data.Contests.All().FirstOrDefault(x => x.Id == id); if (contest == null) { this.TempData.AddDangerMessage(GlobalResource.Invalid_contest); return(this.RedirectToAction(GlobalConstants.Index)); } var checkers = this.Data.Checkers.All() .Select(x => x.Name); var lastOrderBy = -1; var lastProblem = this.Data.Problems.All().Where(x => x.ContestId == id); if (lastProblem.Any()) { lastOrderBy = lastProblem.Max(x => x.OrderBy); } var problem = new DetailedProblemViewModel { Name = "Име", MaximumPoints = 100, TimeLimit = 100, MemoryLimit = 16777216, AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name, Selected = checker.Name.Contains("Trim") }), OrderBy = lastOrderBy + 1, ContestId = contest.Id, ContestName = contest.Name, ShowResults = true, SourceCodeSizeLimit = 16384, ShowDetailedFeedback = false, }; return(this.View(problem)); }
public ActionResult Create(int id, HttpPostedFileBase testArchive, DetailedProblemViewModel problem) { if (problem != null && ModelState.IsValid) { var newProblem = new Problem { Name = problem.Name, ContestId = id, MaximumPoints = problem.MaximumPoints, MemoryLimit = problem.MemoryLimit, TimeLimit = problem.TimeLimit, SourceCodeSizeLimit = problem.SourceCodeSizeLimit, OrderBy = problem.OrderBy, Checker = this.Data.Checkers.All().Where(x => x.Name == problem.Checker).FirstOrDefault() }; if (problem.Resources != null && problem.Resources.Count() > 0) { this.AddResourcesToProblem(newProblem, problem.Resources); } if (testArchive != null && testArchive.ContentLength != 0) { try { this.AddTestsToProblem(newProblem, testArchive); } catch (Exception ex) { TempData.Add("DangerMessage", ex.Message); problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); return(View(problem)); } } this.Data.Problems.Add(newProblem); this.Data.SaveChanges(); TempData.Add("InfoMessage", "Задачата беше добавена успешно"); return(RedirectToAction("Contest", new { id = id })); } problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); return(View(problem)); }
private void AddCheckersAndProblemGroupsToProblemViewModel( DetailedProblemViewModel problem, short numberOfProblemGroups) { problem.AvailableCheckers = this.Data.Checkers.All() .Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name, Selected = checker.Name.Contains("Trim") }); if (numberOfProblemGroups > 0) { this.ViewBag.GroupNumberData = DropdownViewModel.GetFromRange(1, numberOfProblemGroups); } }
public ActionResult Create(int?id) { if (id == null) { this.TempData.AddDangerMessage(GlobalResource.Invalid_contest); return(this.RedirectToAction(GlobalConstants.Index)); } var contest = this.Data.Contests.All().FirstOrDefault(x => x.Id == id); if (contest == null) { this.TempData.AddDangerMessage(GlobalResource.Invalid_contest); return(this.RedirectToAction(GlobalConstants.Index)); } var checkers = this.Data.Checkers.All() .Select(x => x.Name); var lastOrderBy = -1; var lastProblem = this.Data.Problems.All().Where(x => x.ContestId == id); if (lastProblem.Any()) { lastOrderBy = lastProblem.Max(x => x.OrderBy); } var problem = new DetailedProblemViewModel { Name = "Име", MaximumPoints = 100, TimeLimit = 100, MemoryLimit = 16777216, AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }), OrderBy = lastOrderBy + 1, ContestId = contest.Id, ContestName = contest.Name, ShowResults = true, ShowDetailedFeedback = false, }; return(this.View(problem)); }
public ActionResult Create(int?id) { if (id == null) { TempData["DangerMessage"] = "Невалидно състезание"; return(RedirectToAction("Index")); } var contest = this.Data.Contests.All().FirstOrDefault(x => x.Id == id); if (contest == null) { TempData["DangerMessage"] = "Невалидно състезание"; return(RedirectToAction("Index")); } var checkers = this.Data.Checkers.All() .Select(x => x.Name); var lastOrderBy = -1; var lastProblem = this.Data.Problems.All().Where(x => x.ContestId == id); if (lastProblem.Count() > 0) { lastOrderBy = lastProblem.Max(x => x.OrderBy); } var problem = new DetailedProblemViewModel { Name = "Име", MaximumPoints = 100, TimeLimit = 1000, MemoryLimit = 16777216, AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }), OrderBy = lastOrderBy + 1, ContestId = contest.Id, ContestName = contest.Name, }; return(View(problem)); }
public ActionResult Edit(int id, DetailedProblemViewModel problem) { // TODO: Add validation with ModelState.IsValid var existingProblem = this.Data.Problems.All() .FirstOrDefault(x => x.Id == id); existingProblem.Name = problem.Name; existingProblem.MaximumPoints = problem.MaximumPoints; existingProblem.TimeLimit = problem.TimeLimit; existingProblem.MemoryLimit = problem.MemoryLimit; existingProblem.SourceCodeSizeLimit = problem.SourceCodeSizeLimit; existingProblem.Checker = this.Data.Checkers.All().FirstOrDefault(x => x.Name == problem.Checker); existingProblem.OrderBy = problem.OrderBy; this.Data.SaveChanges(); TempData["InfoMessage"] = "Задачата беше променена успешно"; return(RedirectToAction("Contest", new { id = existingProblem.ContestId })); }
public static Action <SubmissionTypeViewModel> ApplySelectedTo(DetailedProblemViewModel problem) { return(st => { var submissionViewModel = new SubmissionTypeViewModel { Id = st.Id, Name = st.Name, IsChecked = false, }; var selectedSubmission = problem.SelectedSubmissionTypes.FirstOrDefault(s => s.Id == st.Id); if (selectedSubmission != null) { submissionViewModel.IsChecked = true; } problem.SubmissionTypes.Add(submissionViewModel); }); }
public ActionResult Create(int id, HttpPostedFileBase testArchive, DetailedProblemViewModel problem) { if (problem.Resources != null && problem.Resources.Any()) { var validResources = problem.Resources .All(res => !string.IsNullOrEmpty(res.Name) && ((res.Type == ProblemResourceType.AuthorsSolution && res.File != null && res.File.ContentLength > 0) || (res.Type == ProblemResourceType.ProblemDescription && res.File != null && res.File.ContentLength > 0) || (res.Type == ProblemResourceType.Video && !string.IsNullOrEmpty(res.Link)))); if (!validResources) { this.ModelState.AddModelError("Resources", GlobalResource.Resources_not_complete); } } if (this.ModelState.IsValid) { var newProblem = new Problem { Name = problem.Name, ContestId = id, MaximumPoints = problem.MaximumPoints, MemoryLimit = problem.MemoryLimit, TimeLimit = problem.TimeLimit, SourceCodeSizeLimit = problem.SourceCodeSizeLimit, ShowResults = problem.ShowResults, ShowDetailedFeedback = problem.ShowDetailedFeedback, OrderBy = problem.OrderBy, Checker = this.Data.Checkers.All().FirstOrDefault(x => x.Name == problem.Checker) }; if (problem.Resources != null && problem.Resources.Any()) { this.AddResourcesToProblem(newProblem, problem.Resources); } if (testArchive != null && testArchive.ContentLength != 0) { try { this.AddTestsToProblem(newProblem, testArchive); } catch (Exception ex) { // TempData is not working with return this.View var systemMessages = new SystemMessageCollection { new SystemMessage { Content = ex.Message, Type = SystemMessageType.Error, Importance = 0 } }; this.ViewBag.SystemMessages = systemMessages; problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); return(this.View(problem)); } } this.Data.Problems.Add(newProblem); this.Data.SaveChanges(); this.TempData.AddInfoMessage(GlobalResource.Problem_added); return(this.RedirectToAction("Contest", new { id })); } problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); return(this.View(problem)); }
public ActionResult Edit(int id, DetailedProblemViewModel problem) { if (!this.CheckIfUserHasProblemPermissions(id)) { this.TempData.AddDangerMessage(GlobalConstants.NoPrivilegesMessage); return(this.RedirectToAction("Index", "Contests", new { area = "Administration" })); } var existingProblem = this.Data.Problems.All().FirstOrDefault(x => x.Id == id); if (existingProblem == null) { this.TempData.Add(GlobalConstants.DangerMessage, GlobalResource.Problem_not_found); return(this.RedirectToAction(nameof(this.Index))); } if (problem == null) { problem = this.PrepareProblemViewModelForEdit(id); problem.AvailableCheckers = this.Data.Checkers.All() .Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); return(this.View(problem)); } if (problem.AdditionalFiles != null && problem.AdditionalFiles.ContentLength != 0) { this.ValidateUploadedFile(nameof(problem.AdditionalFiles), problem.AdditionalFiles); } if (!this.ModelState.IsValid) { problem = this.PrepareProblemViewModelForEdit(id); problem.AvailableCheckers = this.Data.Checkers.All() .Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); this.Data.SubmissionTypes.All() .Select(SubmissionTypeViewModel.ViewModel) .ForEach(SubmissionTypeViewModel.ApplySelectedTo(problem)); return(this.View(problem)); } existingProblem = problem.GetEntityModel(existingProblem); existingProblem.Checker = this.Data.Checkers.All().FirstOrDefault(x => x.Name == problem.Checker); existingProblem.SolutionSkeleton = problem.SolutionSkeletonData; existingProblem.SubmissionTypes.Clear(); if (problem.AdditionalFiles != null && problem.AdditionalFiles.ContentLength != 0) { using (var archiveStream = new MemoryStream()) { problem.AdditionalFiles.InputStream.CopyTo(archiveStream); existingProblem.AdditionalFiles = archiveStream.ToArray(); } } problem.SubmissionTypes.ForEach(s => { if (s.IsChecked) { var submission = this.Data.SubmissionTypes.All().FirstOrDefault(t => t.Id == s.Id); existingProblem.SubmissionTypes.Add(submission); } }); this.Data.Problems.Update(existingProblem); this.Data.SaveChanges(); this.TempData.AddInfoMessage(GlobalResource.Problem_edited); return(this.RedirectToAction("Contest", new { id = existingProblem.ContestId })); }
public ActionResult Create(int id, DetailedProblemViewModel problem) { if (!this.CheckIfUserHasContestPermissions(id)) { this.TempData.AddDangerMessage(GlobalConstants.NoPrivilegesMessage); return(this.RedirectToAction("Index", "Contests", new { area = "Administration" })); } var contest = this.Data.Contests.All().FirstOrDefault(x => x.Id == id); if (contest == null) { this.TempData.AddDangerMessage(GlobalResource.Invalid_contest); return(this.RedirectToAction(nameof(this.Index))); } if (problem == null) { problem = this.PrepareProblemViewModelForCreate(contest); problem.AvailableCheckers = this.Data.Checkers.All() .Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); return(this.View(problem)); } if (problem.Resources != null && problem.Resources.Any()) { var validResources = problem.Resources .All(res => !string.IsNullOrEmpty(res.Name) && ((res.Type == ProblemResourceType.AuthorsSolution && res.File != null && res.File.ContentLength > 0) || (res.Type == ProblemResourceType.ProblemDescription && res.File != null && res.File.ContentLength > 0) || (res.Type == ProblemResourceType.Link && !string.IsNullOrEmpty(res.Link)))); if (!validResources) { this.ModelState.AddModelError("Resources", GlobalResource.Resources_not_complete); } } if (problem.AdditionalFiles != null && problem.AdditionalFiles.ContentLength != 0) { this.ValidateUploadedFile(nameof(problem.AdditionalFiles), problem.AdditionalFiles); } if (problem.Tests != null && problem.Tests.ContentLength != 0) { this.ValidateUploadedFile(nameof(problem.Tests), problem.Tests); } if (!this.IsValidProblem(problem) || !this.ModelState.IsValid) { problem.AvailableCheckers = this.Data.Checkers.All() .Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); return(this.View(problem)); } var newProblem = problem.GetEntityModel(); newProblem.Checker = this.Data.Checkers.All().FirstOrDefault(x => x.Name == problem.Checker); problem.SubmissionTypes.ForEach(s => { if (s.IsChecked) { var submission = this.Data.SubmissionTypes.All().FirstOrDefault(t => t.Id == s.Id); newProblem.SubmissionTypes.Add(submission); } }); if (problem.SolutionSkeletonData != null && problem.SolutionSkeletonData.Any()) { newProblem.SolutionSkeleton = problem.SolutionSkeletonData; } if (problem.Resources != null && problem.Resources.Any()) { this.AddResourcesToProblem(newProblem, problem.Resources); } if (problem.AdditionalFiles != null && problem.AdditionalFiles.ContentLength != 0) { newProblem.AdditionalFiles = problem.AdditionalFiles.ToByteArray(); } if (problem.Tests != null && problem.Tests.ContentLength != 0) { try { this.AddTestsToProblem(newProblem, problem.Tests); } catch (Exception ex) { // TempData is not working with return this.View var systemMessages = new SystemMessageCollection { new SystemMessage { Content = ex.Message, Type = SystemMessageType.Error, Importance = 0 } }; this.ViewBag.SystemMessages = systemMessages; problem.AvailableCheckers = this.Data.Checkers.All() .Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); return(this.View(problem)); } } this.Data.Problems.Add(newProblem); this.Data.SaveChanges(); this.TempData.AddInfoMessage(GlobalResource.Problem_added); return(this.RedirectToAction("Problem", "Tests", new { newProblem.Id })); }
public ActionResult Create(int id, HttpPostedFileBase testArchive, DetailedProblemViewModel problem) { if (problem.Resources != null && problem.Resources.Count() > 0) { var validResources = problem.Resources .All(res => !string.IsNullOrEmpty(res.Name) && ((res.Type == ProblemResourceType.AuthorsSolution && res.File != null && res.File.ContentLength > 0) || (res.Type == ProblemResourceType.ProblemDescription && res.File != null && res.File.ContentLength > 0) || (res.Type == ProblemResourceType.Video && !string.IsNullOrEmpty(res.Link)))); if (!validResources) { this.ModelState.AddModelError("Resources", "Ресурсите трябва да бъдат попълнени изцяло!"); } } if (problem != null && this.ModelState.IsValid) { var newProblem = new Problem { Name = problem.Name, ContestId = id, MaximumPoints = problem.MaximumPoints, MemoryLimit = problem.MemoryLimit, TimeLimit = problem.TimeLimit, SourceCodeSizeLimit = problem.SourceCodeSizeLimit, ShowResults = problem.ShowResults, OrderBy = problem.OrderBy, Checker = this.Data.Checkers.All().Where(x => x.Name == problem.Checker).FirstOrDefault() }; if (problem.Resources != null && problem.Resources.Count() > 0) { this.AddResourcesToProblem(newProblem, problem.Resources); } if (testArchive != null && testArchive.ContentLength != 0) { try { this.AddTestsToProblem(newProblem, testArchive); } catch (Exception ex) { TempData.Add(GlobalConstants.DangerMessage, ex.Message); problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); return(this.View(problem)); } } this.Data.Problems.Add(newProblem); this.Data.SaveChanges(); TempData.Add(GlobalConstants.InfoMessage, "Задачата беше добавена успешно"); return(this.RedirectToAction("Contest", new { id = id })); } problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }); return(this.View(problem)); }