コード例 #1
0
        /// <summary>
        /// loads the browse brochures page with all brochures available to book
        /// </summary>
        /// <returns>Browse page</returns>
        public async Task <IActionResult> Browse(string filter)
        {
            //populate the model and inject into page
            ViewBrochuresViewModel model = new ViewBrochuresViewModel()
            {
                //get all brochures from database
                Brochures = await _context.Brochures.Include(b => b.Accomodation)
                            .Include(b => b.Accomodation.Country)
                            .ToListAsync(),
                ErrorMessage = ""
            };

            //populate catering data for filtering
            ViewData["Catering"] = new SelectList(new List <string>
            {
                "",
                Catering.ALL_INCLUSIVE.ToString(),
                Catering.HALF_BOARD.ToString(),
                Catering.SELF_CATERING.ToString()
            });

            //populate accomodation data for filtering
            ViewData["Accomodation"] = new SelectList(new List <string>
            {
                "",
                "Hotel",
                "Private"
            });

            //populate categories data for filtering from the database
            var allCategories = await _context.Categorys.ToListAsync();

            var categories = new List <string> {
                ""
            };

            foreach (var category in allCategories)
            {
                categories.Add(category.CategoryName);
            }
            ViewData["Category"] = new SelectList(categories);

            //populate countries data for filtering from the database
            var allCountries = await _context.Countrys.ToListAsync();

            var countries = new List <string> {
                ""
            };

            foreach (var country in allCountries)
            {
                countries.Add(country.CountryName);
            }
            ViewData["Country"] = new SelectList(countries);

            //load page
            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> Browse(ViewBrochuresViewModel model)
        {
            //if there is search criteria, do the search
            if (model.Accomodation != null || model.Category != null || model.Catering != null || model.Country != null)
            {
                //gets all brochures with the matching category
                var brochuresWithCategory = _context.Brochures
                                            .Where(b => b.Category.CategoryName.Equals(model.Category))
                                            .Include(b => b.Accomodation.Country)
                                            .ToAsyncEnumerable()
                                            .ToEnumerable();

                //gets all brochures with the matching country
                var brochuresWithCatering = _context.Brochures
                                            .Where(b => b.Catering.ToString().Equals(model.Catering))
                                            .Include(b => b.Accomodation.Country)
                                            .ToAsyncEnumerable()
                                            .ToEnumerable();

                //gets all brochures with the matching country
                var brochuresWithCountry = _context.Brochures
                                           .Where(b => b.Accomodation.Country.CountryName.Equals(model.Country))
                                           .Include(b => b.Accomodation.Country)
                                           .ToAsyncEnumerable()
                                           .ToEnumerable();

                //instantiate empty list
                IEnumerable <Brochure> brochuresWithAccomodation;

                //if no value for accomodation is selected, create a new list
                if (model.Accomodation == null)
                {
                    brochuresWithAccomodation = new List <Brochure>();
                }

                //if hotel is selected, get all hotels from the database
                else if (model.Accomodation.Equals("Hotel"))
                {
                    brochuresWithAccomodation = _context.Brochures
                                                .Where(b => b.Accomodation.GetType() == typeof(Hotel))
                                                .Include(b => b.Accomodation.Country)
                                                .ToAsyncEnumerable()
                                                .ToEnumerable();
                }

                //if private property is selected, get all private accomodations from the database
                else if (model.Accomodation.Equals("Private"))
                {
                    brochuresWithAccomodation = _context.Brochures
                                                .Where(b => b.Accomodation.GetType() == typeof(Private))
                                                .Include(b => b.Accomodation.Country)
                                                .ToAsyncEnumerable()
                                                .ToEnumerable();
                }

                //if reached this far, something has failed
                else
                {
                    brochuresWithAccomodation = new List <Brochure>();
                }

                //instantiate a list to contain all other lists
                List <IEnumerable <Brochure> > allLists = new List <IEnumerable <Brochure> >();

                //if category brochures have data, add list to allLists
                if (brochuresWithCategory.Count() > 0)
                {
                    allLists.Add(brochuresWithCategory);
                }

                //if catering brochures have data, add list to allLists
                if (brochuresWithCatering.Count() > 0)
                {
                    allLists.Add(brochuresWithCatering);
                }

                //if country brochures have data, add list to allLists
                if (brochuresWithCountry.Count() > 0)
                {
                    allLists.Add(brochuresWithCountry);
                }

                //if accomodation brochures have data, add list to allLists
                if (brochuresWithAccomodation.Count() > 0)
                {
                    allLists.Add(brochuresWithAccomodation);
                }

                //if allLists contains a single list
                if (allLists.Count() > 0)
                {
                    //iterate through each list, find all brochures that match and store in seperate list
                    var brochures = allLists
                                    .Skip(1)
                                    .Aggregate(
                        new HashSet <Brochure>(allLists.First()),
                        (h, e) => { h.IntersectWith(e); return(h); }
                        );

                    //add filtered brochures to model
                    model.Brochures = brochures.ToList();

                    model.ErrorMessage = "";
                }
                //no matches have been found, display all brochures
                else
                {
                    model.Brochures = await _context.Brochures.Include(b => b.Accomodation)
                                      .Include(b => b.Accomodation.Country)
                                      .ToListAsync();

                    model.ErrorMessage = "No results match search criteria";
                }
            }
            else
            {
                model.Brochures = await _context.Brochures.Include(b => b.Accomodation)
                                  .Include(b => b.Accomodation.Country)
                                  .ToListAsync();

                model.ErrorMessage = "";
            }

            //populate catering data for filtering
            ViewData["Catering"] = new SelectList(new List <string>
            {
                "",
                Catering.ALL_INCLUSIVE.ToString(),
                Catering.HALF_BOARD.ToString(),
                Catering.SELF_CATERING.ToString()
            });

            //populate accomodation data for filtering
            ViewData["Accomodation"] = new SelectList(new List <string>
            {
                "",
                "Hotel",
                "Private"
            });

            //populate categories data for filtering from the database
            var allCategories = await _context.Categorys.ToListAsync();

            var categories = new List <string> {
                ""
            };

            foreach (var category in allCategories)
            {
                categories.Add(category.CategoryName);
            }
            ViewData["Category"] = new SelectList(categories);

            //populate countries data for filtering from the database
            var allCountries = await _context.Countrys.ToListAsync();

            var countries = new List <string> {
                ""
            };

            foreach (var country in allCountries)
            {
                countries.Add(country.CountryName);
            }
            ViewData["Country"] = new SelectList(countries);

            //load page
            return(View(model));
        }