public SurveySubmission InsertSurveySubmission(SurveySubmission surveySubmission)
        {
            if (surveySubmission == null)
            {
                throw new ArgumentNullException("surveySubmission", "Parameter surveySubmission cannot be null.");
            }

            var addedSurveySubmission = this.surveyContext.SurveySubmissions.Add(surveySubmission);

            this.surveyContext.SaveChanges();

            return(addedSurveySubmission);
        }
Exemplo n.º 2
0
        public HttpResponseMessage Post(Guid surveyId, SurveySubmission submission)
        {
            var survey = this.surveyRepository.Get(surveyId);

            if (survey == null)
            {
                throw this.WebException(HttpStatusCode.NotFound);
            }

            var surveySubmission = new SurveySubmission
            {
                Survey = survey
            };

            submission.Answers.ToList().ForEach(
                a =>
            {
                var question = survey.Questions.FirstOrDefault(q => q.Id == a.Question.Id);
                if (question != null)
                {
                    surveySubmission.Answers.Add(
                        new SurveyAnswer
                    {
                        Value    = a.Value,
                        Question = question
                    });
                }
            });

            var addedSurveySubmission = this.surveyRepository.InsertSurveySubmission(surveySubmission);

            // Notify Submission to SignalR clients
            var hubContext = GlobalHost.ConnectionManager.GetHubContext <SurveyHub>();

            if (hubContext.Clients != null)
            {
                var answers = this.submissionSummaryService.GetSubmissionsSummary(surveyId);
                hubContext.Clients.updateSurveyResults(new { SurveyId = surveyId, GroupedAnswers = answers });
            }

            // Response Message
            var response  = this.Request.CreateResponse <SurveySubmission>(HttpStatusCode.Created, addedSurveySubmission);
            var surveyUri = Url.Link("SubmissionsApi", new { SurveyId = surveyId, SubmissionId = addedSurveySubmission.Id });

            response.Headers.Location = new Uri(surveyUri);

            return(response);
        }
Exemplo n.º 3
0
        public async Task <Result> Submit([FromBody] SurveySubmission response)
        {
            var survey = await _db.Surveys
                         .Include(x => x.questions)
                         .ThenInclude(x => x.answers)
                         .ThenInclude(x => x.answercards)
                         .ThenInclude(x => x.card)
                         .SingleOrDefaultAsync(x => x.id == response.surveyid);

            var choices = survey.questions
                          .Select(x => {
                var c = new Choice {
                    question = x,
                    type     = x.type
                };

                var q_and_a = response.choices.SingleOrDefault(y => y.Key == x.id);

                switch (x.type)
                {
                case AnswerType.MultiLineText:
                case AnswerType.SingleLineText:
                    c.text = q_and_a.Value.choice;
                    break;

                case AnswerType.SelectBox:
                case AnswerType.RadioButtons:
                    int.TryParse(q_and_a.Value.choice, out int result);

                    c.answer = _db.Answers.SingleOrDefault(y => y.id == result);

                    break;

                case AnswerType.Checkbox:
                    c.choiceanswers = q_and_a.Value?.selections?
                                      .Where(y => y.Value)
                                      .Select(y => new ChoiceAnswer {
                        answerid = int.Parse(y.Key)
                    }).ToList();
                    break;

                default:
                    break;
                }

                return(c);
            })
                          .ToList();

            var submission = new Submission {
                surveyid = survey.id,
                answers  = choices
            };

            _db.Submissions.Add(submission);

            await _db.SaveChangesAsync();

            if (response.cards != null && response.cards.Any())
            {
                var choiceid = submission.answers.SingleOrDefault(x => x.type == AnswerType.Cards);

                var cards = response.cards
                            .Where(x => x.Value == true)
                            .Select(x => new CardChoice {
                    cardid   = x.Key,
                    choiceid = choiceid.id
                })
                            .ToList();

                choiceid.cardchoices = cards;

                await _db.SaveChangesAsync();
            }

            return(Result.Success());
        }