/// <summary>
        /// Get a problem by the problem's ID
        /// </summary>
        public TeacherProblemUpdateViewModel GetProblemById(int problemId)
        {
            var problem = _db.Problems.SingleOrDefault(x => x.Id == problemId);

            if (problem != null)
            {
                var model = new TeacherProblemUpdateViewModel {
                    Id             = problem.Id,
                    Name           = problem.Name,
                    Description    = problem.Description,
                    AttachmentName = problem.Attachment,
                    Filetype       = problem.Filetype,
                    Language       = problem.Language,
                    TestCases      = new List <TeacherTestCaseViewModel>()
                };

                var testCases = _db.TestCases.Where(x => x.ProblemId == problemId);

                foreach (var testCase in testCases)
                {
                    model.TestCases.Add(new TeacherTestCaseViewModel {
                        Id     = testCase.Id,
                        Input  = testCase.Input,
                        Output = testCase.ExpectedOutput
                    });
                }

                return(model);
            }

            return(null);
        }
        /// <summary>
        /// Get all problems in a base course via a course instance Id.
        /// </summary>
        public List <TeacherProblemUpdateViewModel> GetProblemsInCourseById(int courseInstanceId)
        {
            var problemList = new List <TeacherProblemUpdateViewModel>();

            var courseId = _courseService.GetCourseIdByCourseCourseInstanceId(courseInstanceId);

            if (courseId != 0)
            {
                var problems = _db.Problems.Where(x => x.CourseId == courseId);

                foreach (var problem in problems)
                {
                    var p = new TeacherProblemUpdateViewModel {
                        Id             = problem.Id,
                        Description    = problem.Description,
                        Name           = problem.Name,
                        AttachmentName = problem.Attachment,
                        CourseId       = problem.CourseId,
                        Filetype       = problem.Filetype,
                        Language       = problem.Language,
                        TestCases      = new List <TeacherTestCaseViewModel>()
                    };

                    foreach (var testCase in problem.TestCases)
                    {
                        var t = new TeacherTestCaseViewModel {
                            Input  = testCase.Input,
                            Output = testCase.ExpectedOutput
                        };

                        p.TestCases.Add(t);
                    }

                    problemList.Add(p);
                }
            }

            return(problemList);
        }
示例#3
0
 /// <summary>
 /// Edit problem's information and test cases
 /// </summary>
 public ActionResult EditProblemInformation(TeacherProblemUpdateViewModel problem)
 {
     return(Json(_teacherService.UpdateProblem(problem)));
 }
        /// <summary>
        /// Update problem information via TeacherProblemUpdateViewModel
        /// </summary>
        public bool UpdateProblem(TeacherProblemUpdateViewModel problem)
        {
            var dbProblem = _db.Problems.SingleOrDefault(x => x.Id == problem.Id);

            if (dbProblem != null)
            {
                dbProblem.Description = problem.Description;
                dbProblem.Name        = problem.Name;

                // Filetype check
                if (_db.Filetypes.SingleOrDefault(x => x.Type == problem.Filetype) == null)
                {
                    var fileType = new Filetype {
                        Type = problem.Filetype
                    };

                    _db.Filetypes.Add(fileType);
                }

                dbProblem.Filetype = problem.Filetype;

                // Language check
                if (_db.ProgrammingLanguages.SingleOrDefault(x => x.Language == problem.Language) == null)
                {
                    var language = new ProgrammingLanguage {
                        Language = problem.Filetype
                    };

                    _db.ProgrammingLanguages.Add(language);
                }

                dbProblem.Language = problem.Language;

                var dbTestCases = _db.TestCases.Where(x => x.ProblemId == problem.Id);

                // Delete removed test cases
                foreach (var dbTestCase in dbTestCases)
                {
                    var found = problem.TestCases.Any(x => x.Id == dbTestCase.Id);

                    if (!found)
                    {
                        _db.TestCases.Remove(dbTestCase);
                    }
                }

                // Add new test cases
                foreach (var testCase in problem.TestCases.Where(x => x.Id == 0))
                {
                    _db.TestCases.Add(new TestCase {
                        ExpectedOutput = testCase.Output,
                        Input          = testCase.Input,
                        ProblemId      = problem.Id
                    });
                }

                try {
                    _db.SaveChanges();
                    return(true);
                }
                catch (Exception e) {
                    return(false);
                }
            }

            return(false);
        }