public ActionResult BeginCreate(EducationPlanViewModel viewModel)
        {
            //Checks that all field in the form the user posted was provided by the user.
            if (ModelState.IsValid)
            {
                //Nothing can be persisted yet, because the education plan is not yet fully supplied by the user.
                Session["EducationPlan"] = viewModel;
                IEnumerable<HeadlineModel> models;
                using (var repository = new EducationPlanRepository())
                {
                    models = repository.GetHeadlinesForGroupId(viewModel.GroupId);
                    //if no hedlines are found http status code 404 is returned.
                    if (models == null)
                    {
                        return HttpNotFound();
                    }
                    //Translates the models into apropriate viewmodels
                    List<HeadlineViewModel> headlineViewModels = Mapper.Map<IEnumerable<HeadlineModel>, List<HeadlineViewModel>>(models);
                    //The actual text under each headline is saved in the junction table between the headline table and the education plan table.
                    IEnumerable<EducationPlanToHeadlineViewModel> educationToHeadlineViewModels =
                        headlineViewModels.Select(hvm => new EducationPlanToHeadlineViewModel { HeadLineId = hvm.Id, Headline = hvm });
                    return View("Create", educationToHeadlineViewModels.ToList());
                }

            }
            //if some fields were left empty the user is prompted to enter text into those fields as well.
            return View(viewModel);
        }
        public ActionResult BeginCreate()
        {
            ViewBag.ListOfGroupIds = new List<SelectListItem>();
            using (var repository = new EducationPlanRepository())
            {
                ViewBag.ListOfGroupIds =
                    repository.GetAllDistinctGroupIds().Select(gi => new SelectListItem { Value = gi.ToString(), Text = gi.ToString() }).ToList();
            }

            return View();
        }
 public ActionResult Create(IList<EducationPlanToHeadlineViewModel> viewModels)
 {
     //Checks that all fields has been supplied by the user.
     if (ModelState.IsValid)
     {
         //Gets the education plan from the session state.
         var educationPlanViewModel = (EducationPlanViewModel)Session["EducationPlan"];
         EducationPlanModel educationPlanModel;
         if (educationPlanViewModel != null)
         {
             educationPlanModel = Mapper.Map<EducationPlanModel>(educationPlanViewModel);
             /*Saves the info abut the education plan to the database the EducationPlanModel now
             gets an id.*/
             using (var repository = new EducationPlanRepository())
             {
                 repository.CreateInfoAboutEducationPlan(educationPlanModel);
                 repository.Save();
             }
             //That id is set for each of the junction table entries.
             foreach (EducationPlanToHeadlineViewModel viewModel in viewModels)
             {
                 viewModel.EducationPlanId = educationPlanModel.Id;
             }
             List<EducationPlanToHeadlineModel> models =
                 viewModels.Select(Mapper.Map<EducationPlanToHeadlineModel>).ToList();
             /*Sets the headline-proprtie to null so that it isn't saved once more in the database because
             it is already there.*/
             models.ForEach(m => m.Headline = null);
             //Saves the education plan to the database.
             using (var repository = new EducationPlanRepository())
             {
                 repository.CreateEducationPlan(models);
                 repository.Save();
             }
             return RedirectToAction("Index");
         }
     }
     //Not all fields have been supplied to the user and the errors are presented to him or her.
     ModelState.AddModelError("", "Something went wrong");
     return View(viewModels);
 }
 public ActionResult PostComment(EducationPlanCommentViewModel viewModel)
 {
     EducationPlanCommentModel model = Mapper.Map<EducationPlanCommentModel>(viewModel);
     using (var repository = new EducationPlanRepository())
     {
         repository.CreateCommentForEducationPlan(model);
         repository.Save();
         return RedirectToAction("Details", new { id = model.EducationPlanId });
     }
 }
 /// <summary>
 /// Fetches all education plans from the database and returns a rendered view containing a table
 /// of them.
 /// </summary>
 /// <returns>The rendered view.</returns>
 public ActionResult Index()
 {
     IEnumerable<EducationPlanModel> list;
     using (var repository = new EducationPlanRepository())
     {
         list = repository.GetEducationPlans();
         IEnumerable<EducationPlanViewModel> viewModelList = list.Select(Mapper.Map<EducationPlanViewModel>);
         return View(viewModelList.ToList());
     }
 }
        public ActionResult Edit(EducationPlanViewModel viewModel)
        {
            using (var educationRepository = new EducationPlanRepository())
            using (var headlineRepository = new EducationPlanToHeadlineRepository())
            {
                educationRepository.Update(Mapper.Map<EducationPlanModel>(viewModel));
                foreach (var educationPlanHeadline in viewModel.EducationPlanToHeadline)
                {
                    headlineRepository.Update(Mapper.Map<EducationPlanToHeadlineModel>(educationPlanHeadline));
                }

                headlineRepository.Save();
                educationRepository.Save();
            }

            return RedirectToAction("Index");
        }
 public ActionResult Edit(int? id)
 {
     if (id.HasValue)
     {
         using (var repository = new EducationPlanRepository())
         {
             EducationPlanModel model = repository.Find(id.Value);
             EducationPlanViewModel viewModel = Mapper.Map<EducationPlanViewModel>(model);
             return View(viewModel);
         }
     }
     else
     {
         throw new ArgumentException("Id must be provided");
     }
 }
        /// <summary>
        /// Fetches an education plan with a user given id and returns a rendered view containing a form which 
        /// lets the user read and comment it.
        /// </summary>
        /// <param name="id">The id supplied by the user.</param>
        /// <returns>A rendered view containing info about the education plan and a form 
        /// for commenting the education plan.</returns>
        /// <exception cref="ArgumentException">Thrown when no id is supplied by the user.</exception>
        public ActionResult Details(int? id)
        {
            //If no id is supplied, an exception is thrown.
            if (!id.HasValue)
            {
                throw new ArgumentException("Id must be provided");
            }
            EducationPlanModel model;
            using (var repository = new EducationPlanRepository())
            {
                model = repository.Find(id.Value);
                if (model != null)
                {
                    model.EducationPlanComments = (IList<EducationPlanCommentModel>)repository.GetCommentsForEducationPlan(id.Value);
                    EducationPlanViewModel viewModel = Mapper.Map<EducationPlanViewModel>(model);
                    return View(viewModel);
                }
            }

            return HttpNotFound();
        }