public void CreateMap(MapQuestionListViewModel model)
        {
            Map map = model.Map;

            if (map == null)
            {
                throw new MapException("Map is empty");
            }
            IEnumerable <Question> selectedQuestions = model.Questions.Where(q => q.isChecked).ToQuestion();
            IEnumerable <Question> mapQuestions      = u_repo.GetQuestions(t => t.GenusId == model.Map.GenusId);


            map.Questions = mapQuestions.Intersect(selectedQuestions, new QuestionComparer()).ToList();
            map.Genus     = u_repo.GetGenus(map.GenusId);
            map.Retired   = false;
            u_repo.SaveMap(map);
            Years year = new Years()
            {
                Map   = map,
                MapId = map.Id,
                Year  = map.PlantingYear
            };

            u_repo.SaveYear(year);
            map.Years.Add(year);
            u_repo.SaveMap(map);
        }
        public ActionResult Create()
        {
            int                      genusId    = SessionManager.GetGenusId().Value;
            List <Question>          questions  = m_repo.GetQuestions(q => q.Retired == false && q.GenusId == genusId).ToList();
            var                      genus      = m_repo.GetGenus(genusId);
            MapQuestionListViewModel model      = MapQuestionViewModelTranslator.ToMapQuestionListViewModel(questions, genus, genus.DefaultPlantsInRep);
            var                      crossTypes = m_repo.GetCrossTypes().Where(t => t.GenusId == model.Map.GenusId && t.Retired == false);

            ViewBag.CrossTypes = new SelectList(crossTypes, "Id", "Name");
            return(View(model));
        }
        public ActionResult Create(MapQuestionListViewModel model)
        {
            int genusId = SessionManager.GetGenusId().Value;

            if (ModelState.IsValid)
            {
                map_repo.CreateMap(model);
                return(RedirectToAction("Details", new { id = model.Map.Id }));
            }
            if (model.Map != null & model.Map.LocationId.HasValue)
            {
                model.Map.Location = m_repo.GetLocation(model.Map.LocationId.Value);
            }
            var crossTypes = m_repo.GetCrossTypes().Where(t => t.GenusId == model.Map.GenusId && t.Retired == false);

            ViewBag.CrossTypes = new SelectList(crossTypes, "Id", "Name", model.Map.CrossTypeId);

            return(View(model));
        }
        public ActionResult Edit(MapQuestionListViewModel mapQuestionViewModel)
        {
            ActionResult result;

            if (ModelState.IsValid)
            {
                Map map = mapQuestionViewModel.Map;

                IEnumerable <Question> selectedQuestions = mapQuestionViewModel.Questions.Where(q => q.isChecked).ToQuestion().ToList();
                IEnumerable <Question> mapQuestions      = m_repo.GetQuestions(t => t.GenusId == map.GenusId);
                map.Genus = m_repo.GetGenus(map.GenusId);
                m_repo.SaveMap(map);

                map = m_repo.GetMap(map.Id);

                IEnumerable <Question> updatedQuestions = mapQuestions.Intersect(selectedQuestions, new QuestionComparer());
                map.Questions.Clear(); // needed to update questions
                map.Questions = updatedQuestions.ToList();
                m_repo.SaveMap(map);


                result = RedirectToAction("Details", new { id = map.Id });
            }
            else
            {
                if (mapQuestionViewModel.Map != null & mapQuestionViewModel.Map.LocationId.HasValue)
                {
                    mapQuestionViewModel.Map.Location = m_repo.GetLocation(mapQuestionViewModel.Map.LocationId.Value);
                }

                result = View(mapQuestionViewModel);
            }
            var crossTypes = m_repo.GetCrossTypes().Where(t => t.GenusId == mapQuestionViewModel.Map.GenusId && t.Retired == false);

            ViewBag.CrossTypes = new SelectList(crossTypes, "Id", "Name", mapQuestionViewModel.Map.CrossTypeId);


            return(result);
        }
        public ActionResult Edit(int?id)
        {
            if (!id.HasValue)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Map map = m_repo.GetMap(id.Value);

            if (map == null)
            {
                return(HttpNotFound());
            }
            List <Question>          availQuestions = m_repo.GetQuestions(q => q.Retired == false && q.GenusId == map.GenusId).ToList();
            MapQuestionListViewModel mapQuestion    = map.ToMapQuestionListViewModel(availQuestions);

            ViewBag.Location = new SelectList(m_repo.GetLocations(), "Id", "Name");
            var crossTypes = m_repo.GetCrossTypes().Where(t => t.GenusId == map.GenusId && t.Retired == false);

            ViewBag.CrossTypes = new SelectList(crossTypes, "Id", "Name");

            return(View(mapQuestion));
        }