예제 #1
0
        public ActionResult Create(ProjectTask projectTask, FormCollection form)
        {
            if (!coursesRepository.IsTeacherInAnyCourse(User.Identity.GetUserId(), User.IsInRole("Administrator")))
            {
                throw new HttpException((int)HttpStatusCode.Forbidden, "Forbidden!");
            }

            if (ModelState.IsValid)
            {
                foreach (var file in form.GetValue("files").AttemptedValue.Split(','))
                {
                    if (String.IsNullOrEmpty(file.Trim()))
                    {
                        continue;
                    }
                    projectTasksRepository.AddFilesRequired(new FileRequired {
                        Name = file.Trim(), ProjectTaskId = projectTask.Id
                    });
                }

                for (var i = 0; i < form.Count; i++)
                {
                    var key = form.Keys[i];

                    if (key.StartsWith("test") && key.EndsWith("output"))
                    {
                        var counter = key.Split('_')[1];
                        var input   = form.GetValue("test_" + counter + "_input").AttemptedValue.Replace("&quot;", "\"");
                        var output  = form.GetValue("test_" + counter + "_output").AttemptedValue.Replace("&quot;", "\"");
                        projectTasksRepository.AddTaskTests(new TaskTest {
                            Input = input, Output = output, ProjectTaskId = projectTask.Id
                        });
                    }
                }

                projectTasksRepository.AddProjectTask(projectTask);
                projectTasksRepository.SaveChanges();
                return(RedirectToAction("Index"));
            }


            ViewBag.ProjectId = new SelectList(projectsRepository.GetProjectsByUserId(User.Identity.GetUserId(), User.IsInRole("Administrator")), "Id", "Name", projectTask.ProjectId);
            return(View(projectTask));
        }