Exemplo n.º 1
0
        // PUT api/ManageSurvey/5
        public IHttpActionResult PutSurvey(int id, Survey survey)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != survey.SurveyId)
            {
                return BadRequest();
            }

            db.Entry(survey).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SurveyExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Exemplo n.º 2
0
        public IHttpActionResult PostSurvey(Survey survey)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

            return CreatedAtRoute("DefaultApi", new { id = survey.SurveyId }, survey);
        }
Exemplo n.º 3
0
        public virtual ActionResult Import(ImportSurveyViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return View(viewModel);
            }

            var survey = new Survey();
            survey.Title = viewModel.Name;
            survey.CreatedDate = DateTime.Now;
            survey.UpdatedDate = DateTime.Now;

            if (viewModel.SurveyType == SurveyType.PIT)
            {
                survey.IsPIT = true;
            }
            if (viewModel.SurveyType == SurveyType.Encampment)
            {
                survey.IsEncampment = true;
            }

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

            var uploadPath = "C:\\CTASurveys\\";

            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }

            var fileName = Path.GetFileName(viewModel.ExcelDocument.FileName);
            viewModel.ExcelDocument.SaveAs(string.Format("{0}{1}", uploadPath, fileName));

            fileName = string.Format("{0}{1}", uploadPath, fileName);
            var connectionString =
                string.Format(
                    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";",
                    fileName);

            var adapter = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}$]", viewModel.SheetName), connectionString);
            var ds = new DataSet();

            adapter.Fill(ds, "sccSurvey");
            DataTable data = ds.Tables["sccSurvey"];

            var questionParents = new List<QuestionWrapper>();

            var questions = new List<Question>();

            foreach (DataRow row in data.Rows)
            {
                if (!string.IsNullOrEmpty(row["QuestionId"].ToString()))
                {
                    var question = new Question();
                    question.QuestionText = row["QuestionText"].ToString().Trim();
                    if (row["Options"].ToString() != "NULL")
                    {
                        question.Options = row["Options"].ToString().Trim();
                    }
                    question.QuestionType =
                        (Question.QType) Enum.Parse(typeof (Question.QType), row["QuestionType"].ToString());
                    question.Active = row["Active"].ToString() == "1";
                    question.TextBoxDataType = row["TextBoxDataType"].ToString();

                    if (data.Columns.Contains("RequiredQuestion"))
                    {
                        question.Required = row["RequiredQuestion"].ToString() == "Y";
                    }

                    var questionWrapper = new QuestionWrapper();
                    questionWrapper.Id = int.Parse(row["QuestionId"].ToString());
                    questionWrapper.ParentQuestion = question;
                    questionParents.Add(questionWrapper);

                    if (row["ParentQuestionId"].ToString() != "NULL")
                    {
                        var parentId = int.Parse(row["ParentQuestionId"].ToString());
                        question.ParentRequiredAnswer = row["ParentRequiredAnswer"].ToString();

                        var parent = questionParents.First(q => q.Id == parentId);
                        parent.Children.Add(question);
                    }
                    db.Questions.Add(question);
                    questions.Add(question);
                }
            }

            db.SaveChanges();

            foreach (var q in questionParents)
            {
                foreach (var c in q.Children)
                {
                    c.ParentQuestionId = q.ParentQuestion.QuestionId;
                }
            }

            var i = 0;
            foreach (var q in questions)
            {
                var surveyQuestion = new SurveyQuestion();
                surveyQuestion.Survey = survey;
                surveyQuestion.Question = q;
                surveyQuestion.OrderId = i++;
                db.SurveyQuestions.Add(surveyQuestion);
            }

            db.SaveChanges();

            return null;
        }