public ActionResult AddTasks([Bind(Include="ID, OrganisationID, TaskCount")]Survey survey, string Tasks )
        {
            if (ModelState.IsValid)
            {
                string pattern = ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)";

                string[] taskList = Tasks.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string task in taskList)
                {

                    // input.Substring(1, input.Length - 2) removes the first and last " from the string
                    string[] tasksplit = Regex.Split(task, pattern);

                    var dbTask = new SurveyTask
                    {
                        Name = tasksplit[0].Trim(new char[]{' ', '"'}),
                        SurveyID = survey.ID,
                        Description = "",
                        Active = true
                    };

                    if ( tasksplit.Length > 1)
                    {
                        dbTask.Description = tasksplit[1].Trim();
                    }

                    db.Tasks.Add(dbTask);
                }

                db.Surveys.Attach(survey);
                db.Entry(survey).Property(x => x.TaskCount).IsModified = true;
                db.SaveChanges();

                return RedirectToAction("Questions", new { id = survey.ID });
            }

            return View(survey);
        }
Beispiel #2
0
        public IHttpActionResult Add(int id, SurveyTask task)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            if (id > 0)
            {
                var survey = db.Surveys.Find(id);
                if (survey == null)
                    return BadRequest("No Survey");

                if (!AdminSecurity.IsValidOrganisation(survey.OrganisationID))
                    return BadRequest("Not Allowed to Add");

                task.survey = survey;
                db.Tasks.Add(task);
                db.SaveChanges();

                return CreatedAtRoute("DefaultAPI", new { id = task.ID }, task);
            }

            return BadRequest("SurveyID was blank");
        }
Beispiel #3
0
        public IHttpActionResult Update(int id, SurveyTask task)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var survey = db.Surveys.Find(id);
            if (survey == null)
                return BadRequest("No survey");

            if (!AdminSecurity.IsValidOrganisation(survey.OrganisationID))
                return BadRequest("Not allowed");

            db.Tasks.Attach(task);
            task.survey = survey;
            db.Entry(task).Property(p => p.Name).IsModified = true;
            db.Entry(task).Property(p => p.Description).IsModified = true;
            db.SaveChanges();

            return Ok(task);
        }