Ejemplo n.º 1
0
        public DifficultyViewModel GetDifficultyById(int difficultyId)
        {
            var difficultyViewModel = new DifficultyViewModel();

            using (var dbContext = new FilRougeDBContext())
            {
                var difficultyEntities = dbContext.Difficulties.Find(difficultyId);
                difficultyViewModel = difficultyEntities.MapToDifficultyViewModel();
            }

            return(difficultyViewModel);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Convertir une DifficultyViewModel en Difficulty de Question
        /// </summary>
        /// <param name="difficultyViewModel"></param>
        /// <returns>new Difficulty</returns>
        public static Difficulty MapToDifficulty(this DifficultyViewModel difficultyViewModel)
        {
            var difficulty = new Difficulty();

            if (difficultyViewModel == null)
            {
                return(difficulty);
            }
            else
            {
                difficulty = new Difficulty {
                    DifficultyId   = difficultyViewModel.DifficultyId,
                    DifficultyName = difficultyViewModel.DifficultyName
                };
                return(difficulty);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Convertir une Difficulté de Question en View.
        /// </summary>
        /// <param name="difficulty"></param>
        /// <returns>New DifficultyViewModel</returns>
        public static DifficultyViewModel MapToDifficultyViewModel(this Difficulty difficulty)
        {
            var difficultyViewModel = new DifficultyViewModel();

            if (difficulty == null)
            {
                return(difficultyViewModel);
            }
            else
            {
                difficultyViewModel = new DifficultyViewModel
                {
                    DifficultyId   = difficulty.DifficultyId,
                    DifficultyName = difficulty.DifficultyName
                };
                return(difficultyViewModel);
            }
        }
        // GET: Advertisements

        public ActionResult Index(AdvertisementsListViewModel viewModel, string sortOrder, int?page)
        {
            if (viewModel.AdvertisementSearchModel == null)
            {
                viewModel.AdvertisementSearchModel = new AdvertisementSearchModel();
                if (Session["SearchViewModel"] != null)
                {
                    viewModel.AdvertisementSearchModel =
                        (AdvertisementSearchModel)Session["SearchViewModel"];
                }
            }
            else
            {
                Session["SearchViewModel"] = viewModel.AdvertisementSearchModel;
            }

            //Categories
            var selectedCategories = new List <Category>();
            var postedCategoryIds  = new string[0];

            if (viewModel.AdvertisementSearchModel.PostedCategories == null)
            {
                viewModel.AdvertisementSearchModel.PostedCategories =
                    new PostedCategories();
            }

            if (viewModel.AdvertisementSearchModel.PostedCategories.CategoriesIds != null)
            {
                postedCategoryIds =
                    viewModel.AdvertisementSearchModel.PostedCategories.CategoriesIds;
            }

            if (postedCategoryIds.Any())
            {
                selectedCategories = db.Categories.Where(x =>
                                                         postedCategoryIds.Any(s => x.Id.ToString().Equals(s))).ToList();
            }

            viewModel.AvaibleCategories = new List <CategoryViewModel>();
            var categories = db.Categories;

            foreach (Category c in categories)
            {
                var cvm = new CategoryViewModel();
                cvm.Id   = c.Id;
                cvm.Name = c.Name;
                viewModel.AvaibleCategories.Add(cvm);
            }

            viewModel.SelectedCategories = new List <CategoryViewModel>();
            foreach (Category c in selectedCategories)
            {
                var cvm = new CategoryViewModel();
                cvm.Id   = c.Id;
                cvm.Name = c.Name;
                viewModel.SelectedCategories.Add(cvm);
            }

            //Difficulties

            var selectedDifficulties = new List <Difficulty>();
            var postedDifficultyIds  = new string[0];

            if (viewModel.AdvertisementSearchModel.PostedDifficulties == null)
            {
                viewModel.AdvertisementSearchModel.PostedDifficulties = new PostedDifficulties();
            }

            if (viewModel.AdvertisementSearchModel.PostedDifficulties.DifficultiesIds != null)
            {
                postedDifficultyIds = viewModel.AdvertisementSearchModel.PostedDifficulties.DifficultiesIds;
            }

            if (postedDifficultyIds.Any())
            {
                selectedDifficulties = db.Difficulties.Where(x => postedDifficultyIds.Any(s => x.Id.ToString().Equals(s))).ToList();
            }

            viewModel.AvaibleDifficulties = new List <DifficultyViewModel>();
            var difficulties = db.Difficulties;

            foreach (Difficulty d in difficulties)
            {
                var dvm = new DifficultyViewModel();
                dvm.Id   = d.Id;
                dvm.Name = d.Name;
                viewModel.AvaibleDifficulties.Add(dvm);
            }

            viewModel.SelectedDifficulties = new List <DifficultyViewModel>();
            foreach (Difficulty d in selectedDifficulties)
            {
                var dvm = new DifficultyViewModel();
                dvm.Id   = d.Id;
                dvm.Name = d.Name;
                viewModel.SelectedDifficulties.Add(dvm);
            }

            //Locations
            viewModel.Locations = new List <Location>();
            viewModel.Locations = db.Locations.ToList();

            //Advertisements
            var searchLogic    = new AdvertisementsSearchLogic();
            var advertisements = searchLogic.GetAdvertisements(viewModel.AdvertisementSearchModel);

            //Sorting
            ViewBag.CurrentSort      = sortOrder;
            ViewBag.TitleSortParm    = String.IsNullOrEmpty(sortOrder) ? "title_desc" : "";
            ViewBag.LocationSortParm =
                sortOrder == "location_asc" ? "location_desc" : "location_asc";
            ViewBag.AddDateSortParm =
                sortOrder == "addDate_asc" ? "addDate_desc" : "addDate_asc";
            ViewBag.DifficultySortParm =
                sortOrder == "difficulty_asc" ? "difficulty_desc" : "difficulty_asc";

            switch (sortOrder)
            {
            case "title_desc":
                advertisements = advertisements.OrderByDescending(a => a.Title);
                break;

            case "location_asc":
                advertisements = advertisements.OrderBy(a => a.Location.Name);
                break;

            case "location_desc":
                advertisements = advertisements.OrderByDescending(a => a.Location.Name);
                break;

            case "addDate_asc":
                advertisements = advertisements.OrderBy(a => a.AddDate);
                break;

            case "addDate_desc":
                advertisements = advertisements.OrderByDescending(a => a.AddDate);
                break;

            case "difficulty_asc":
                advertisements = advertisements.OrderBy(a => a.Difficulty.Name);
                break;

            case "difficulty_desc":
                advertisements = advertisements.OrderByDescending(a => a.Difficulty.Name);
                break;

            default:
                advertisements = advertisements.OrderBy(a => a.Title);
                break;
            }

            int pageSize   = 2;
            int pageNumber = (page ?? 1);

            viewModel.Advertisements = advertisements.ToPagedList(pageNumber, pageSize);

            return(View(viewModel));
        }