Beispiel #1
0
        private void CourseTeacherGenerator()
        {
            CourseTeacher ct1 = new CourseTeacher();
            ct1.CourseId = 1;
            ct1.TeacherId = 2;

            CourseTeacher ct2 = new CourseTeacher();
            ct2.CourseId = 2;
            ct2.TeacherId = 2;

            CourseTeacher ct3 = new CourseTeacher();
            ct3.CourseId = 3;
            ct3.TeacherId = 2;

            CourseTeacher ct4 = new CourseTeacher();
            ct4.CourseId = 4;
            ct4.TeacherId = 4;

            CourseTeacher ct5 = new CourseTeacher();
            ct5.CourseId = 5;
            ct5.TeacherId = 4;

            CourseTeacher ct6 = new CourseTeacher();
            ct6.CourseId = 6;
            ct6.TeacherId = 4;

            context.CourseTeachers.Add(ct1);
            context.CourseTeachers.Add(ct2);
            context.CourseTeachers.Add(ct3);
            context.CourseTeachers.Add(ct4);
            context.CourseTeachers.Add(ct5);
            context.CourseTeachers.Add(ct6);
        }
        public ActionResult Edit(CourseViewModel model, HttpPostedFileBase file)
        {
            if (!ModelState.IsValid || model.TeacherId == 0) {
                TempData["CourseModel"] = model;
                if (model.TeacherId == 0) {
                    ModelState.AddModelError("NoTeacher", "You must specify a teacher.");
                }
                TempData["Errors"] = ModelState.Values.SelectMany(v => v.Errors).ToList();
                if (model.Id > 0) {
                    return RedirectToAction("Edit", "Course", new { id = model.Id });
                } else {
                    return RedirectToAction("Edit", "Course");
                }
            }
            ICourseService courseService = CourseService.GetInstance<CourseRepository>();
            ICourseTeacherService courseTeacherService = CourseTeacherService.GetInstance<CourseTeacherRepository>();
            Course course = model.Id == 0 ? new Course() : courseService.Get(model.Id);
            course.Title = model.Title;
            course.Description = model.Description;
            course.AboutCourse = model.AboutCourse;
            course.Syllabus = model.Syllabus;
            course.Lectures = model.Lectures;
            course.IsPublic = model.IsPublic;
            if (course.SchoolId == 0) {
                course.SchoolId = CurrentProfile.Id;
            }

            string filename;
            if (file != null && file.ContentLength > 0) {
                filename = Guid.NewGuid().ToString() + "." + Path.GetExtension(file.FileName);
                string path = Path.Combine(Server.MapPath(Constants.UPLOADS_PATH), filename);
                FileHelper.CreateFile(path, file.InputStream, true);

            } else {
                filename = "class_default.jpg";
            }

            course.PictureUrl = filename;

            if (model.Id == 0) {
                course = courseService.Insert(course);
            } else {
                courseService.Update(course);
            }

            CourseTeacher courseTeacher = new CourseTeacher();
            courseTeacher.CourseId = course.Id;
            courseTeacher.TeacherId = model.TeacherId;
            courseTeacherService.Insert(courseTeacher);

            return RedirectToAction("Edit", "Course", new { id = course.Id });
        }