public static EditProblemViewModel GetEditProblemViewModel(Problem problem) { var epvm = new EditProblemViewModel(); epvm.Author = problem.Author.UserName; epvm.Name = problem.Name; epvm.Text = problem.Text; epvm.SelectedCategoryId = problem.CategoryId; epvm.IsBlocked = problem.IsBlocked; var sb = new StringBuilder(); foreach (var tag in problem.Tags) { sb.Append(tag.Name); sb.Append(','); } epvm.TagsString = sb.ToString(); sb.Clear(); foreach (var ans in problem.CorrectAnswers) { sb.Append(ans.Text); sb.Append(';'); } sb.Remove(sb.Length - 1, 1); epvm.Answers = sb.ToString(); return epvm; }
public ActionResult Edit(EditProblemViewModel problemView) { if (ModelState.IsValidField("Answers")) UpdateProblemFromView(problemView); else return View(problemView); problemView.Author = User.Identity.Name; return RedirectToAction("Index"); }
public void UpdateProblemFromView(EditProblemViewModel problemView) { Problem problem = problemRepository.GetByID(problemView.Id); problem.CategoryId = problemView.SelectedCategoryId; problem.Name = problemView.Name; problem.Text = problemView.Text; problem.IsBlocked = problemView.IsBlocked; while (problem.CorrectAnswers.Count != 0) answerRepository.Delete(problem.CorrectAnswers.ElementAt(0)); problem.CorrectAnswers = GetAnswersFromString(problemView.Answers, problem); problem.Tags = new Collection<Tag>(); problemRepository.Update(problem); if (!problemView.TagsString.IsNullOrWhiteSpace()) { foreach (var tag in problemView.TagsString.Split(',')) { problem.Tags.Add(tagRepository.GetByName(tag)); } } problemRepository.Update(problem); }
public void AddProblemFromView(EditProblemViewModel problemView) { var problem = new Problem { CategoryId = problemView.SelectedCategoryId, Name = problemView.Name, Text = problemView.Text, AuthorId = UserManager.FindByName(User.Identity.Name).Id, Tags = new Collection<Tag>(), IsBlocked = problemView.IsBlocked }; foreach (var tag in problemView.TagsString.Split(',')) { problem.Tags.Add(tagRepository.GetByName(tag)); } problemRepository.Insert(problem); problem.CorrectAnswers = GetAnswersFromString(problemView.Answers, problem); problemRepository.Update(problem); }