Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
0
        public ActionResult Edit(EditProblemViewModel problemView)
        {
            if (ModelState.IsValidField("Answers"))
                UpdateProblemFromView(problemView);
            else
                return View(problemView);

            problemView.Author = User.Identity.Name;
            return RedirectToAction("Index");
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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);
        }