Beispiel #1
0
        public IHttpActionResult CreateSurvey(CreateSurveyRequest surveyContext)
        {
            var response = _surveyManager.CreateSurvey(surveyContext);

            if (response != Errors.OK)
            {
                return(Content(HttpStatusCode.BadRequest, new ErrorResponse(response)));
            }
            return(Content(HttpStatusCode.OK, ""));
        }
        public ActionResult AddSurvey(CreateSurveyRequest createRequest)
        {
            var newSurvey = _surveyRepository.AddSurvey(createRequest.Date);

            foreach (var question in createRequest.Questions)
            {
                Console.WriteLine(question);
            }
            //_questionRepository.AddQuestion(createRequest.Questions);
            return(Created($"/api/student/{newSurvey.Id}", newSurvey));
        }
Beispiel #3
0
        public void CreateSurveyOK()
        {
            // Arrange
            var mockDb     = new Mock <IDataBaseManager>();
            var mockLogger = new Mock <ILogger>();
            CreateSurveyRequest surveyContext = new CreateSurveyRequest();

            mockDb.Setup(db => db.InsertSurvey(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <QuestionRequest[]>())).Returns(true);
            var manager = new SurveyManager(mockDb.Object, mockLogger.Object);


            // Act

            var result = manager.CreateSurvey(surveyContext);

            // Assert

            Assert.AreEqual(Errors.OK, result);
        }
Beispiel #4
0
        public void CreateSurveyDbError()
        {
            // Arrange
            var mockDb     = new Mock <IDataBaseManager>();
            var mockLogger = new Mock <ILogger>();
            CreateSurveyRequest surveyContext = new CreateSurveyRequest();

            mockDb.Setup(db => db.InsertSurvey(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <QuestionRequest[]>())).Throws(new Exception());
            var manager = new SurveyManager(mockDb.Object, mockLogger.Object);


            // Act

            var result = manager.CreateSurvey(surveyContext);

            // Assert

            Assert.AreEqual(Errors.DATA_BASE_ERROR, result);
        }
Beispiel #5
0
 /// <summary>
 /// Creates survey
 /// </summary>
 /// <param name="surveyContext">Param with survey, questions, and options records</param>
 /// <returns>
 ///     Errors.OK survey created
 ///     Errors.SURVEY_ALREADY_EXIST if survey already exist
 ///     Errors.DATA_BASE_ERROR if an database exception was thrown
 /// </returns>
 public Errors CreateSurvey(CreateSurveyRequest surveyContext)
 {
     try
     {
         if (_dbManager.InsertSurvey(surveyContext.Name, surveyContext.Description, surveyContext.Questions))
         {
             return(Errors.OK);
         }
         else
         {
             return(Errors.SURVEY_ALREADY_EXIST);
         }
     }
     catch (Exception ex)
     {
         _logger.Error(ex.Message);
         return(Errors.DATA_BASE_ERROR);
     }
 }
        private async Task <CreateSurveyResponse> CreateSurveyInService(Survey survey, string sender, string[] recipients)
        {
            CreateSurveyRequest request = new CreateSurveyRequest
            {
                Survey     = survey,
                Sender     = sender,
                Recipients = recipients
            };

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:1266/");
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.PostAsJsonAsync("api/surveys", request);

                response.EnsureSuccessStatusCode();

                return(await response.Content.ReadAsAsync <CreateSurveyResponse>());
            }
        }
        public ActionResult CreateSurvey([FromBody] CreateSurveyRequest req)
        {
           Project proj = _context.Project.Where(p => p.Uid == req.ProjectUid).FirstOrDefault<Project>();
           Concept cpt = _context.Concept.Where(c => c.Uid == req.ConceptUid).FirstOrDefault<Concept>();


            Survey survey = new Survey();
            survey.Uid = req.UniqueId;
            survey.ProjectId = proj.Id;
            survey.Prototypes = req.Prototypes;
            if (cpt != null)
            survey.ConceptId = cpt.Id;
            survey.SurveyName = req.SurveyName;
            survey.Notes = req.Notes;
            survey.Qualifications = req.Qualifications;
            survey.DateCreated = DateTime.Now;
            survey.Questions = req.Questions;
            survey.Status = req.Status;

           _context.Survey.Add(survey);
           _context.SaveChanges();

            return Ok(new { message = "Success!" });
        }
        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));
        }