Example #1
0
        /// <summary>
        /// Event for when the filter button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            bool   isYearFromOK    = true;
            bool   isYearToOK      = true;
            bool   isYearFromEmpty = false;
            bool   isYearToEmpty   = false;
            string errorMessage    = "";

            // Filter for the "from year"
            TextBox objYearFrom = txtYearFrom;
            string  yearFrom    = objYearFrom.Text.Trim();

            if (yearFrom == "")
            {
                isYearFromOK    = false;
                isYearFromEmpty = true;
            }
            else if (!(Regex.IsMatch(yearFrom, @"^\d+$")))
            {
                isYearFromOK  = false;
                errorMessage += "The \"from year\" needs to be a number!";
            }
            else if (yearFrom.Length != 4)
            {
                isYearFromOK  = false;
                errorMessage += "The \"from year\" needs to be YYYY!";
            }


            // Filter for the "to year"
            TextBox objYearTo = txtYearTo;
            string  yearTo    = objYearTo.Text.Trim();

            if (yearTo == "")
            {
                isYearToOK    = false;
                isYearToEmpty = true;
            }
            else if (!(Regex.IsMatch(yearTo, @"^\d+$")))
            {
                isYearToOK = false;
                if (errorMessage != "")
                {
                    errorMessage += "\n";
                }
                errorMessage += "The \"to year\" needs to be a number!";
            }
            else if (yearTo.Length != 4)
            {
                isYearToOK = false;
                if (errorMessage != "")
                {
                    errorMessage += "\n";
                }
                errorMessage += "The \"to year\" needs to be YYYY!";
            }

            if (isYearFromOK && isYearToOK)
            {
                if (Int32.Parse(yearFrom) > Int32.Parse(yearTo))
                {
                    isYearFromOK  = isYearToOK = false;
                    errorMessage += "The filter \"from year\" needs to be smaller than the \"to year\"!";
                }
            }

            // Filter for the genres
            ListBox    objGenreList = lstbGenre;
            List <int> genres       = new List <int>();

            foreach (ListBoxItem genre in objGenreList.Items)
            {
                if (genre.IsSelected)
                {
                    genres.Add(Int32.Parse(genre.Tag.ToString()));
                }
            }

            // Main area of the filter
            if ((isYearFromOK && isYearToOK) || (isYearFromEmpty && isYearToEmpty && genres.Count > 0))
            {
                MovieRepository movieRepo = new MovieRepository();
                List <fmmMovie> movies;
                if (isYearFromOK && isYearToOK)
                {
                    movies = movieRepo.Filter(genres, new int[] { Int32.Parse(yearFrom), Int32.Parse(yearTo) });
                }
                else
                {
                    movies = movieRepo.Filter(genres);
                }


                if (movies.Count > 0)
                {
                    btnBackSearch.Visibility = Visibility.Visible;

                    ShowHideMenu("sbHideLeftMenu", btnLeftMenuHide, btnLeftMenuShow, pnlLeftMenu);

                    List <UIElement> delItems = new List <UIElement>();

                    IEnumerable <Image> covers = gridMovies.Children.OfType <Image>();
                    foreach (Image child in covers)
                    {
                        // Get the objects that were added to the display by the search or filter function
                        // These elements will be deleted
                        if (child.Tag.ToString() == "search")
                        {
                            delItems.Add(child);
                        }
                        else
                        {
                            child.Visibility = Visibility.Collapsed;
                        }
                    }

                    foreach (UIElement delitem in delItems)
                    {
                        gridMovies.Children.Remove(delitem);
                    }

                    searchClicked = false;

                    foreach (fmmMovie movie in movies)
                    {
                        string urlImg = "";
                        if (movie.poster != null && internetConected)
                        {
                            urlImg = "https://image.tmdb.org/t/p/w500" + movie.poster;
                        }

                        addMovieGrid(urlImg, movie.id, "search");
                    }

                    searchClicked = true;
                }
                else
                {
                    MessageBox.Show("No movies were found!", "Find My Movie", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }

            if (errorMessage != "")
            {
                MessageBox.Show(errorMessage, "Filter warning!", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
Example #2
0
        public void ViewMovieList(User user)
        {
            List <Movie> movies       = new List <Movie>();
            bool         isFinished   = false;
            string       errorMessage = string.Empty;

            while (!isFinished)
            {
                Screen.ClearScreen();
                if (!string.IsNullOrWhiteSpace(errorMessage))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(errorMessage);
                    Console.ResetColor();
                    errorMessage = string.Empty;
                }

                if (movies.Count != 0)
                {
                    movies.PrintMovies();
                }
                Screen.OrderingMenu();
                var selection = InputParser.ToInteger(0, 9);
                switch (selection)
                {
                case 1:
                    movies = _movieRepository.GetAll();
                    break;

                case 2:
                    movies = _movieRepository.OrderByGenre(true);
                    break;

                case 3:
                    Genre genre = InputParser.ToGenre();
                    movies = _movieRepository.Filter(x => x.Genre == genre);
                    break;

                case 4:
                    movies = _movieRepository.OrderByReleaseDate(true);
                    break;

                case 5:
                    int year = InputParser.ToInteger(
                        _movieRepository.GetAll().Min(_movie => _movie.ReleaseDate.Year),
                        DateTime.Now.Year - 1
                        );
                    movies = _movieRepository.Filter(x => x.ReleaseDate.Year == year);
                    break;

                case 6:
                    movies = _movieRepository.OrderByAvailability(true);
                    break;

                case 7:
                    movies = _movieRepository.Filter(x => x.IsAvailable);
                    break;

                case 8:
                    string titlePart       = Console.ReadLine();
                    string trimedTitlePart = titlePart.Trim().ToLower();
                    movies = _movieRepository.Filter(x => x.Title.ToLower().Contains(trimedTitlePart));
                    break;

                case 9:
                    //TODO: Rent a movie
                    try
                    {
                        RentMovie(user);
                    }
                    catch (Exception ex)
                    {
                        // TODO: Find a way to show error message.
                        errorMessage = ex.Message;
                    }
                    break;

                case 0:
                    isFinished = !isFinished;
                    break;
                }
            }
        }