Exemple #1
0
        public ActionResult Edit(int id, CourseViewModel model)
        {
            var topicRepository = new TopicRepository(context);

            try
            {
                var repository = new CourseRepository(context);
                if (ModelState.IsValid)
                {
                    var course = MapperHelper.Map <Course>(model);
                    repository.UpdateCourseWithTopics(course, model.SelectTopics);
                    context.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                var topic = topicRepository.Query(null, "Name");
                model.AvailableTopics = MapperHelper.Map <ICollection <TopicCourseViewModel> >(topic);
                return(View(model));
            }
            catch
            {
                var topic = topicRepository.Query(null, "Name");
                model.AvailableTopics = MapperHelper.Map <ICollection <TopicCourseViewModel> >(topic);
                return(View(model));
            }
        }
Exemple #2
0
        public ActionResult Edit(int id, EditTopicViewModel model)
        {
            try
            {
                var repository = new TopicRepository(context);
                if (model.Name != model.NombreAnterior)
                {
                    var existeNombre = repository.Query(x => x.Name == model.Name && x.Id != model.Id).Count > 0;
                    if (existeNombre)
                    {
                        ModelState.AddModelError("Name", "El nombre ya está ocupado por otro tópico.");
                        return(View(model));
                    }
                }
                else
                {
                    ModelState.Remove("Nombre");
                }

                if (ModelState.IsValid)
                {
                    var topic = MapperHelper.Map <Topic>(model);
                    repository.Update(topic);
                    context.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
Exemple #3
0
 public ActionResult Create(CourseViewModel model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             var repository     = new CourseRepository(context);
             var validacionName = new Course {
                 Name = model.Name
             };
             var nameExiste = repository.QueryByExample(validacionName).Count > 0;
             if (!nameExiste)
             {
                 var course = MapperHelper.Map <Course>(model);
                 repository.InsertCourseWithTopics(course, model.SelectTopics);
                 context.SaveChanges();
             }
             else
             {
                 ModelState.AddModelError("Name", "El Nombre del curso ya existe");
                 var topicrepository = new TopicRepository(context);
                 var topic           = topicrepository.Query(null, "Name");
                 model.AvailableTopics = MapperHelper.Map <ICollection <TopicCourseViewModel> >(topic);
                 return(View(model));
             }
         }
         return(RedirectToAction("Index"));
     }
     catch (Exception ex)
     {
         return(View());
     }
 }
        public ActionResult Create(CourseViewModel model)
        {
            try
            {
                var repository = new CourseRepository(context);
                if (ModelState.IsValid)
                {
                    var course = MapperHelper.Map <Course>(model);
                    //Pasar modelo ya lleno
                    //course.Topics = new List<Topic>();

                    /* var topicRepo = new TopicRepository(context);
                     * foreach (var topicId in model.SelectedTopics) {
                     *   course.Topics.Add(topicRepo.Find(topicId));
                     *
                     * }
                     * repository.Insert(course);
                     */
                    repository.InsertCourseWithTopics(course, model.SelectedTopics);
                    context.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                else
                {
                    var topicRepository = new TopicRepository(context);
                    var topics          = topicRepository.Query(null, "Name DESC");
                    model.AvailableTopics = MapperHelper.Map <ICollection <TopicViewModel> >(topics);
                    return(View(model));
                }
            }
            catch
            {
                return(View());
            }
        }
Exemple #5
0
        private ICollection <TopicViewModel> LoadTopics()
        {
            var topicRepository = new TopicRepository(context);
            var topics          = topicRepository.Query(null, "Name DESC");

            return(MapperHelper.Map <ICollection <TopicViewModel> >(topics));
        }
        public ActionResult Edit(int id, CourseViewModel model)
        {
            try
            {
                var repository      = new CourseRepository(context);
                var topicRepository = new TopicRepository(context);
                var topics          = topicRepository.Query(null, "Name");
                model.AvailableTopics = MapperHelper.Map <ICollection <TopicViewModel> >(topics);

                if (ModelState.IsValid)
                {
                    var entity = MapperHelper.mapper.Map <Course>(model);
                    repository.UpdateCourse(entity, model.SelectedTopics);
                    context.SaveChanges();
                    return(RedirectToAction("Index"));
                }


                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = ex.Message;
                return(View(model));
            }
        }
Exemple #7
0
 public ActionResult Create(CourseViewModel model)
 {
     try
     {
         var repository = new CourseRepository(context);
         // TODO: Add insert logic here
         if (ModelState.IsValid)
         {
             var courses = MapperHelpers.Map <Course>(model);
             repository.InsertCourseWithTopic(courses, model.SelectedTopics);
             context.SaveChanges();
             return(RedirectToAction("Index"));
         }
         else
         {
             var topicRepository = new TopicRepository(context);
             var topics          = topicRepository.Query(null, "Name DESC");
             model.AvailableTopics = MapperHelpers.Map <ICollection <TopicViewModel> >(topics);
             return(View(model));
         }
     }
     catch
     {
         return(View());
     }
 }
Exemple #8
0
        // GET: Topic
        public ActionResult Index()
        {
            var repository = new TopicRepository(context);
            var topic      = repository.Query(null, "Name");
            var models     = MapperHelper.Map <IEnumerable <TopicViewModel> >(topic);

            return(View(models));
        }
Exemple #9
0
        // GET: Course/Create
        public ActionResult Create()
        {
            var model      = new CourseViewModel();
            var repository = new TopicRepository(context);
            var topic      = repository.Query(null, "Name");

            model.AvailableTopics = MapperHelper.Map <ICollection <TopicCourseViewModel> >(topic);
            return(View(model));
        }
        // GET: Course/Edit/5
        public ActionResult Edit(int id)
        {
            var repository      = new CourseRepository(context);
            var topicRepository = new TopicRepository(context);
            var includes        = new Expression <Func <Course, object> >[] { x => x.Topics };
            var course          = repository.QueryIncluding(x => x.Id == id, includes).SingleOrDefault();
            var model           = MapperHelper.Map <CourseViewModel>(course);
            var topics          = topicRepository.Query(null, "Name");

            model.AvailableTopics = MapperHelper.Map <ICollection <TopicViewModel> >(topics);
            model.SelectedTopics  = course.Topics.Select(x => x.Id.Value).ToArray();
            return(View(model));
        }
        // GET: Course/Edit/5
        public ActionResult Edit(int id)
        {
            var model = new CourseViewModel();

            try
            {
                var repository      = new CourseRepository(context);
                var includes        = new Expression <Func <Course, object> >[] { x => x.Topics };
                var entity          = repository.QueryIncluding(x => x.Id == id, includes).SingleOrDefault();
                var topicRepository = new TopicRepository(context);
                var topics          = topicRepository.Query(null, "Name");
                model = MapperHelper.mapper.Map <CourseViewModel>(entity);
                model.AvailableTopics = MapperHelper.Map <ICollection <TopicViewModel> >(topics);
                model.SelectedTopics  = entity.Topics.Select(x => x.Id.Value).ToArray();
                return(View(model));
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = ex.Message;
                return(View(model));
            }
        }