<commit_before>﻿namespace SurveySystem.Web.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web.Mvc;

    using AutoMapper.Internal;

    using SurveySystem.Data;
    using SurveySystem.Data.Models;
    using SurveySystem.Web.Models.Survey;

    public class SurveyController : BaseController
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        [HttpGet]
        public ViewResult New()
        {
            return this.View();
        }

        [HttpPost]
        public ViewResult New(NewSurveyRequest request)
        {
            if (!this.ModelState.IsValid)
            {
                return this.View(request);
            }

            var survey = new Survey
            {
                IsAnonymous = request.IsAnonymous,
                BeginsOn = request.BeginsOn,
                Title = request.Title
            };

            for (int i = 0; i < request.Questions.Count; i++)
            {
                var questionDetails = request.Questions[i];
                var question = new Question
                {
                    Text = questionDetails.Text,
                    QuestionType = questionDetails.Type,
                    SequenceNumber = i
                };

                if (question.QuestionType != QuestionType.FreeText)
                {
                    question.QuestionAnswers = this.CreateQuestionAnswers(question, questionDetails.Answer);
                }

                survey.Questions.Add(question);
            }

            this.db.Surveys.Add(survey);
            this.db.SaveChanges();

            return this.View();
        }

        [HttpGet]
        public ViewResult Submit(int id = 1)
        {
            var submission = this.GetSubmission(id);
            return this.View(submission);
        }

        [HttpPost]
        public ViewResult Submit(SurveySubmission submission)
        {
            return this.View(submission);
        }

        private List<QuestionAnswer> CreateQuestionAnswers(Question question, string answers)
        {
            var questionAnswers = answers.Split(
                new[] { Environment.NewLine },
                StringSplitOptions.RemoveEmptyEntries);

            return questionAnswers.Select(x => new QuestionAnswer { Question = question, Text = x }).ToList();
        }

        private SurveySubmission GetSubmission(int id)
        {
            var survey = this.db.Surveys.Include(x => x.Questions).First(x => x.Id == id);

            var result = survey.Questions
                .GroupBy(x => x.QuestionType, (type, values) => new { Type = type, Questions = values })
                .ToDictionary(x => x.Type, x => x.Questions);

            var freeTextQuestions = new List<FreeTextQuestion>();
            var radioButtonQuestions = new List<RadioButtonQuestion>();
            var checkBoxQuestions = new List<CheckBoxQuestion>();

            if (result.ContainsKey(QuestionType.FreeText))
            {
                freeTextQuestions = result[QuestionType.FreeText]
                    .Select(x => new FreeTextQuestion { SequentialNumber = x.SequenceNumber, Text = x.Text }).ToList();<commit_msg>Added todos<commit_after>﻿namespace SurveySystem.Web.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web.Mvc;

    using AutoMapper.Internal;

    using SurveySystem.Data;
    using SurveySystem.Data.Models;
    using SurveySystem.Web.Models.Survey;

    public class SurveyController : BaseController
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        [HttpGet]
        public ViewResult New()
        {
            return this.View();
        }

        [HttpPost]
        public ViewResult New(NewSurveyRequest request)
        {
            if (!this.ModelState.IsValid)
            {
                return this.View(request);
            }

            var survey = new Survey
            {
                IsAnonymous = request.IsAnonymous,
                BeginsOn = request.BeginsOn,
                Title = request.Title
            };

            // TODO: validate this!
            for (int i = 0; i < request.Questions.Count; i++)
            {
                var questionDetails = request.Questions[i];
                var question = new Question
                {
                    Text = questionDetails.Text,
                    QuestionType = questionDetails.Type,
                    SequenceNumber = i
                };

                if (question.QuestionType != QuestionType.FreeText)
                {
                    question.QuestionAnswers = this.CreateQuestionAnswers(question, questionDetails.Answer);
                }

                survey.Questions.Add(question);
            }

            this.db.Surveys.Add(survey);
            this.db.SaveChanges();

            return this.View();
        }

        [HttpGet]
        public ViewResult Submit(int id = 1)
        {
            // TODO: validate date!
            var submission = this.GetSubmission(id);
            return this.View(submission);
        }

        [HttpPost]
        public ViewResult Submit(int id, SurveySubmission submission)
        {
            // TODO: validate user input (id + answers)!
            return this.View(submission);
        }

        private List<QuestionAnswer> CreateQuestionAnswers(Question question, string answers)
        {
            var questionAnswers = answers.Split(
                new[] { Environment.NewLine },
                StringSplitOptions.RemoveEmptyEntries);

            return questionAnswers.Select(x => new QuestionAnswer { Question = question, Text = x }).ToList();
        }

        private SurveySubmission GetSubmission(int id)
        {
            var survey = this.db.Surveys.Include(x => x.Questions).First(x => x.Id == id);

            var result = survey.Questions
                .GroupBy(x => x.QuestionType, (type, values) => new { Type = type, Questions = values })
                .ToDictionary(x => x.Type, x => x.Questions);

            var freeTextQuestions = new List<FreeTextQuestion>();
            var radioButtonQuestions = new List<RadioButtonQuestion>();
            var checkBoxQuestions = new List<CheckBoxQuestion>();

            if (result.ContainsKey(QuestionType.FreeText))
            {
                freeTextQuestions = result[QuestionType.FreeText]
                    .Select(x => new FreeTextQuestion { SequentialNumber = x.SequenceNumber, Text = x.Text }).ToList();
{'fraction_non_alphanumeric': 0.09056830109461689, 'fraction_numerical': 0.000599790073474284, 'mean_word_length': 1.6637380191693292, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6197196', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1590, 'n_words': 362}