public ActionResult SaveCourse(Post post, FormCollection postedForm)
        {
            if (NotAllowedHere()) return RedirectAway();

            if (ModelState.IsValid)
            {
                var theOldPost = _postRepo.FindByID(post.ID);
                if (theOldPost == null)
                    theOldPost = post;
                if (theOldPost.Images == null)
                    theOldPost.Images = new List<Image>();
                var oldImages = theOldPost.Images.ToList();
                var listOfImagesPaths = postedForm["image"];
                string[] arrayOfImagesPaths = null;
                if (listOfImagesPaths != null)
                    arrayOfImagesPaths = listOfImagesPaths.Split(',');

                oldImages = theOldPost.Images.ToList();
                if (arrayOfImagesPaths != null && arrayOfImagesPaths.Count() > 0)
                {
                    foreach (var addedimg in _imgRepo.FindAll(c =>
                arrayOfImagesPaths.Any(cat => cat == c.ImagePath.ToString()) &&
                !c.Posts.Any(p => p.ID == theOldPost.ID)).ToList())
                        theOldPost.Images.Add(addedimg);
                }
                foreach (var removedimg in oldImages.Where(c => arrayOfImagesPaths
                == null || !arrayOfImagesPaths.Any(cat2 => cat2 == c.ImagePath.ToString())))
                    theOldPost.Images.Remove(removedimg);

                if (theOldPost.Category == null)
                    theOldPost.Category = new List<Category>();
                var oldCategories = theOldPost.Category.ToList();
                var listOfCategoryIDs = postedForm["name"];
                string[] arrayOfCategoryIDs = null;
                if (listOfCategoryIDs != null)
                    arrayOfCategoryIDs = listOfCategoryIDs.Split(',');

                oldCategories = theOldPost.Category.ToList();
                if (arrayOfCategoryIDs != null && arrayOfCategoryIDs.Count() > 0)
                {
                    foreach (var addedCat in _categoryRepo.FindAll(c =>
                arrayOfCategoryIDs.Any(cat => cat == c.ID.ToString()) &&
                !c.Posts.Any(p => p.ID == theOldPost.ID)).ToList())
                        theOldPost.Category.Add(addedCat);
                }
                foreach (var removedCat in oldCategories.Where(c => arrayOfCategoryIDs
                == null || !arrayOfCategoryIDs.Any(cat2 => cat2 == c.ID.ToString())))
                    theOldPost.Category.Remove(removedCat);

                if (theOldPost.Badges == null)
                    theOldPost.Badges = new List<Image>();
                var oldBadges = theOldPost.Badges.ToList();
                var listOfBadges = postedForm["badge"];
                string[] arrayOfBadges = null;
                if (listOfBadges != null)
                    arrayOfBadges = listOfBadges.Split(',');

                oldBadges = theOldPost.Badges.ToList();
                if (arrayOfBadges != null && arrayOfBadges.Count() > 0)
                {
                    foreach (var addedbadge in _imgRepo.FindAll(c =>
                 arrayOfBadges.Any(cat => cat == c.ImagePath.ToString()) &&
                 !c.Posts.Any(p => p.ID == theOldPost.ID)).ToList())
                        theOldPost.Badges.Add(addedbadge);
                }
                foreach (var removedimg in oldBadges.Where(c => arrayOfBadges
                == null || !arrayOfBadges.Any(cat2 => cat2 == c.ImagePath.ToString())))
                    theOldPost.Badges.Remove(removedimg);

                _postRepo.Save(post);
                // add a message to the viewbag
                TempData["message"] = string.Format("{0} has been saved", post.Title);
                // return the user to the list
                return RedirectToAction("Index");
            }
            else
            {
                AddCourseViewModel vmCategory = new AddCourseViewModel();
                vmCategory.Post = post;
                if (post.Category != null) {
                    vmCategory.Categories = post.Category.ToList();
                }
                ViewData["events"] = new SelectList(_categoryRepo.FindAll().ToList(), "ID", "Name");
                // there is something wrong with the data values
                return View("AddCourse", vmCategory);
            }
        }
        public ActionResult EditCourse(int id = 0)
        {
            if (NotAllowedHere()) return RedirectAway();

            if (id != 0)
            {
                AddCourseViewModel vmCategory = new AddCourseViewModel();
                vmCategory.Post = _postRepo.FindByID(id);
                vmCategory.Categories = vmCategory.Post.Category.ToList();
                ViewData["events"] = new SelectList(_categoryRepo.FindAll().ToList(), "ID", "Name");
                return View("AddCourse", vmCategory);
            }
            else
            {
                AddCourseViewModel vmCategory = new AddCourseViewModel();

                vmCategory.Post = new Post();
                vmCategory.Post.Created = DateTime.Now;
                vmCategory.Post.postType = (int)Post.PostType.Course;
                ViewData["events"] = new SelectList(_categoryRepo.FindAll().ToList(), "ID", "Name");
                return View("AddCourse", vmCategory);
            }
        }
        public ActionResult Index()
        {
            if (NotAllowedHere()) return RedirectAway();

            AddCourseViewModel vm = new AddCourseViewModel();
            vm.Posts = _postRepo.FindAll().Where(p => p.postType == (int)Post.PostType.Course).ToList();
            vm.Categories = _categoryRepo.FindAll().ToList();
            return View(vm);
        }
Ejemplo n.º 4
0
        public ActionResult SaveCourse(Post post, FormCollection postedForm)
        {
            if (ModelState.IsValid)
            {
                var listOfCategoryIDs = postedForm["name"];

                var arrayOfCategoryIDs = listOfCategoryIDs.Split(',');
                 if (arrayOfCategoryIDs.Count() > 0){
                     post.Category = new List<Category>();
                     foreach (var array in arrayOfCategoryIDs) {
                         int x = Convert.ToInt32(array);
                         var cat = _categoryRepo.FindByID(x);
                         cat.Posts.Add(post);
                         _categoryRepo.Save(cat);
                     }
                 }
                _postRepo.Save(post);
                // add a message to the viewbag
                TempData["message"] = string.Format("{0} has been saved", post.Title);
                // return the user to the list
                return RedirectToAction("Index");
            }
            else
            {
                AddCourseViewModel vmCategory = new AddCourseViewModel();
                vmCategory.Post = post;
                if (post.Category != null) {
                    vmCategory.Categories = post.Category.ToList();
                }
                ViewData["events"] = new SelectList(_categoryRepo.FindAll().ToList(), "ID", "Name");
                // there is something wrong with the data values
                return View("AddCourse", vmCategory);
            }
        }