Ejemplo n.º 1
0
 public PartialViewResult CourseList(string userName)
 {
     Author userAuthor = new Author();
     using (ChalkitDbContext db = new ChalkitDbContext())
     {
         userAuthor = db.Authors.Find(userName);
         db.Entry(userAuthor).Collection(x => x.Courses).Load();
     }
     ViewBag.AuthorName = userAuthor.AuthorUserName;
     return PartialView("_AuthorCoursesList", userAuthor.Courses);
 }
Ejemplo n.º 2
0
 public ActionResult CreateOrModifyCourse(string userName, Course course)
 {
     Author userAuthor = new Author();
     using (ChalkitDbContext db = new ChalkitDbContext())
     {
         try
         {
             userAuthor = db.Authors.Find(userName);
             Course existingCourse = db.Courses.Find(course.CourseID);
             if (existingCourse == null)
             {
                 course.Exercises = new List<Exercise>();
                 db.Courses.Add(course);
                 db.Entry(userAuthor).Collection(x => x.Courses).Load();
                 userAuthor.Courses.Add(course);
             }
             else
             {
                 db.Entry(existingCourse).Collection(x => x.Exercises).Load();
                 existingCourse.CourseName = course.CourseName;
                 existingCourse.CourseDescription = course.CourseDescription;
             }
             db.SaveChanges();
         }
         catch (DbEntityValidationException dbEx)
         {
             foreach (var validationErrors in dbEx.EntityValidationErrors)
             {
                 foreach (var validationError in validationErrors.ValidationErrors)
                 {
                     Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                 }
             }
         }
     }
     return RedirectToAction("Index", "Author");
 }
Ejemplo n.º 3
0
 public PartialViewResult EditCourse(int courseID)
 {
     Author userAuthor = new Author();
     using (ChalkitDbContext db = new ChalkitDbContext())
     {
        userAuthor = db.Authors.Find(HttpContext.User.Identity.Name);
        db.Entry(userAuthor).Collection(x => x.Courses).Load();
        ViewBag.AuthorName = userAuthor.AuthorUserName;
         foreach(Course tempCourse in userAuthor.Courses)
         {
             if(tempCourse.CourseID == courseID)
             {
                 db.Entry(tempCourse).Collection(y => y.Exercises).Load();
                 return PartialView("_AuthorCourseUpdateCreate",tempCourse);
             }
         }
     }
     return PartialView("_AuthorCourseUpdateCreate", new Course());
 }
Ejemplo n.º 4
0
 //
 // GET: /Author/
 public ActionResult Index()
 {
     Author userAuthor = new Author();
     using (ChalkitDbContext db = new ChalkitDbContext())
     {
        userAuthor = db.Authors.Find(HttpContext.User.Identity.Name);
        db.Entry(userAuthor).Collection(x => x.Courses).Load();
     }
     ViewBag.AuthorName = userAuthor.AuthorUserName;
     return View(userAuthor);
 }
Ejemplo n.º 5
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    if (db.Authors.Find(model.UserName) != null || db.Students.Find(model.UserName) != null)
                    {
                        MembershipCreateUserException e = new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName);
                        throw e;
                    }
                    if (model.AccountType == AccountType.Author)
                    {
                        try
                        {
                            Author author = new Author();
                            author.AuthorUserName = model.UserName;
                            author.Password = model.Password;
                            author.FirstName = model.FirstName;
                            author.LastName = model.LastName;
                            author.PersonalEmail = model.PersonalEmail;
                            author.Courses = new List<Course>();
                            db.Authors.Add(author);
                            db.SaveChanges();
                        }
                        catch (DbEntityValidationException dbEx)
                        {
                            foreach (var validationErrors in dbEx.EntityValidationErrors)
                            {
                                foreach (var validationError in validationErrors.ValidationErrors)
                                {
                                    Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                                }
                            }
                        }
                        LoginUser(model.UserName, false);
                        return RedirectToAction("Index", "Author"); // redirect to Author home page
                    }
                    else
                    {
                        Author student = new Author();
                        student.AuthorUserName = model.UserName;
                        student.Password = model.Password;
                        student.FirstName = model.FirstName;
                        student.LastName = model.LastName;
                        student.PersonalEmail = model.PersonalEmail;
                        db.Authors.Add(student);
                        db.SaveChanges();
                        LoginUser(model.UserName, false);
                        return RedirectToAction("Index", "Home"); // redirect to student home page
                    }

                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }