示例#1
0
        /// <summary>
        /// Create a survey based on template or existing survey.
        /// Notes
        ///     You cannot specify both template\_id and from\_survey\_id
        /// Endpoint : https://api.surveymonkey.net/v2/surveys/create_survey?api_key=your_api_key
        /// Example Request
        /// curl -H 'Authorization:bearer XXXYYYZZZ' -H 'Content-Type: application/json' https://api.surveymonkey.net/v2/surveys/create_survey?api_key=your_api_key --data-binary '{"template_id": "568", "survey_title": "Ice and Fire Event"}'
        /// </summary>
        public CreateSurveyResponse CreateSurvey(BasicRequestData requestData)
        {
            CreateSurveyResponse surveyList;
            BasicRequestData     thisRequest = new BasicRequestData();

            // This request expects a from_survey_id not the standard survey_id
            if (requestData.SurveyID != null)
            {
                requestData.FromSurveyID = requestData.SurveyID;
                requestData.SurveyID     = null;
            }

            // This request can only take one of FromSurveyID or TemplateID not both
            if ((requestData.FromSurveyID != null) && (requestData.TemplateID != null))
            {
                surveyList              = new CreateSurveyResponse();
                surveyList.Status       = -2;
                surveyList.ErrorMessage = "Specify either from_survey_id or template_id, not both.";
            }
            else if (requestData.SurveyTitle == null)
            {
                surveyList              = new CreateSurveyResponse();
                surveyList.Status       = -3;
                surveyList.ErrorMessage = "Survey Title (survey_title) is required for this call.";
            }
            else if ((requestData.FromSurveyID == null) && (requestData.TemplateID == null))
            {
                surveyList              = new CreateSurveyResponse();
                surveyList.Status       = -4;
                surveyList.ErrorMessage = "One of from_survey_id or template_id must be specified for this call.";
            }
            else
            {
                thisRequest.FromSurveyID = requestData.FromSurveyID;
                thisRequest.TemplateID   = requestData.TemplateID;
                thisRequest.SurveyTitle  = requestData.SurveyTitle;
                JsonResponse             = MakeApiRequest(CREATE_SURVEY, requestData);
                surveyList = JsonConvert.DeserializeObject <CreateSurveyResponse>(JsonResponse);
            }

            return(surveyList);
        }
        public static CreateSurveyResponse Unmarshall(UnmarshallerContext context)
        {
            CreateSurveyResponse createSurveyResponse = new CreateSurveyResponse();

            createSurveyResponse.HttpResponse   = context.HttpResponse;
            createSurveyResponse.RequestId      = context.StringValue("CreateSurvey.RequestId");
            createSurveyResponse.Success        = context.BooleanValue("CreateSurvey.Success");
            createSurveyResponse.Code           = context.StringValue("CreateSurvey.Code");
            createSurveyResponse.Message        = context.StringValue("CreateSurvey.Message");
            createSurveyResponse.HttpStatusCode = context.IntegerValue("CreateSurvey.HttpStatusCode");

            CreateSurveyResponse.CreateSurvey_Survey survey = new CreateSurveyResponse.CreateSurvey_Survey();
            survey.Id                      = context.StringValue("CreateSurvey.Survey.Id");
            survey.ScenarioUuid            = context.StringValue("CreateSurvey.Survey.ScenarioUuid");
            survey.Name                    = context.StringValue("CreateSurvey.Survey.Name");
            survey.Description             = context.StringValue("CreateSurvey.Survey.Description");
            survey.Role                    = context.StringValue("CreateSurvey.Survey.Role");
            survey.Round                   = context.IntegerValue("CreateSurvey.Survey.Round");
            survey.HotWords                = context.StringValue("CreateSurvey.Survey.HotWords");
            survey.SpeechOptimizationParam = context.StringValue("CreateSurvey.Survey.SpeechOptimizationParam");
            survey.GlobalQuestions         = context.StringValue("CreateSurvey.Survey.GlobalQuestions");

            CreateSurveyResponse.CreateSurvey_Survey.CreateSurvey_Flow flow = new CreateSurveyResponse.CreateSurvey_Survey.CreateSurvey_Flow();
            flow.FlowId      = context.StringValue("CreateSurvey.Survey.Flow.FlowId");
            flow.IsPublished = context.BooleanValue("CreateSurvey.Survey.Flow.IsPublished");
            flow.FlowJson    = context.StringValue("CreateSurvey.Survey.Flow.FlowJson");
            survey.Flow      = flow;

            CreateSurveyResponse.CreateSurvey_Survey.CreateSurvey_AsrCustomModel asrCustomModel = new CreateSurveyResponse.CreateSurvey_Survey.CreateSurvey_AsrCustomModel();
            asrCustomModel.Corpora           = context.StringValue("CreateSurvey.Survey.AsrCustomModel.Corpora");
            asrCustomModel.CustomModelStatus = context.IntegerValue("CreateSurvey.Survey.AsrCustomModel.CustomModelStatus");
            survey.AsrCustomModel            = asrCustomModel;
            createSurveyResponse.Survey      = survey;

            return(createSurveyResponse);
        }
        public IHttpActionResult PostSurvey(CreateSurveyRequest surveyRequest)
        {
            bool   resetDb                = Request.Headers.Contains("X-SimpleSurvey-ResetDB");
            string newSurveyLocation      = string.Empty;
            CreateSurveyResponse response = new CreateSurveyResponse();

            try
            {
                using (var db = new SurveyContext())
                {
                    if (resetDb)
                    {
                        db.Database.Delete();
                    }

                    // Create the survey
                    SimpleSurvey newSurvey = new SimpleSurvey
                    {
                        Name            = surveyRequest.Survey.Name,
                        Sender          = surveyRequest.Sender,
                        Expiration      = DateTime.UtcNow.AddMinutes(surveyRequest.Survey.Duration),
                        QuestionTitle   = surveyRequest.Survey.QuestionTitle,
                        QuestionChoices = surveyRequest.Survey.QuestionChoices,
                        ResultsReported = false,
                        Participants    = new List <Participant>()
                    };

                    // Create participants
                    foreach (string recipient in surveyRequest.Recipients)
                    {
                        // Check if recipient is already in database, create if not
                        var participant = db.Participants.FirstOrDefault(p => p.Email == recipient.ToLower());

                        if (participant == null)
                        {
                            participant = new Participant
                            {
                                Email     = recipient.ToLower(),
                                TokenSalt = TokenGenerator.GenerateNewTokenSalt()
                            };

                            db.Participants.Add(participant);
                        }

                        newSurvey.Participants.Add(participant);
                    }

                    // Add survey to the database
                    db.Surveys.Add(newSurvey);
                    db.SaveChanges();

                    newSurveyLocation = Url.Link("DefaultApi", new { id = newSurvey.SimpleSurveyId });

                    response.Status       = "Succeeded";
                    response.SurveyId     = newSurvey.SimpleSurveyId;
                    response.Expiration   = newSurvey.Expiration;
                    response.Participants = new List <SurveyParticipant>();

                    foreach (Participant participant in newSurvey.Participants)
                    {
                        response.Participants.Add(new SurveyParticipant
                        {
                            Email        = participant.Email,
                            LimitedToken = TokenGenerator.GenerateLimitedToken(newSurvey.SimpleSurveyId, participant.Email, participant.TokenSalt)
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            return(Created(newSurveyLocation, response));
        }