Ejemplo n.º 1
0
        private void Queue_Click(object sender, RoutedEventArgs e)
        {
            Movie current = (Movie)MovieList.SelectedItem;

            using (var context = new MovieRentalEntities())
            {
                Queue queue = new Queue()
                {
                    AccountNumber = customer.AccountNumber,
                    MovieID       = current.MovieID,
                    DateAdded     = System.DateTime.Today
                };

                try
                {
                    context.Queues.Add(queue);
                    context.SaveChanges();
                    MessageBox.Show(current.Title + " has been added to your queue");
                }
                catch (DbUpdateException)
                {
                    MessageBox.Show(current.Title + " is already in your queue");
                }
            }
        }
Ejemplo n.º 2
0
        private List <Movie> GetWeekPopular()
        {
            List <Movie> movies = new List <Movie>();

            // Sunday of this week
            DayOfWeek firstDay = 0;

            DateTime firstOfWeek = DateTime.Today.Date;

            // Find the date of this week's Sunday
            while (firstOfWeek.DayOfWeek != firstDay)
            {
                firstOfWeek = firstOfWeek.AddDays(-1);
            }

            DateTime date = firstOfWeek.Date;

            using (var context = new MovieRentalEntities())
            {
                var weekMovies      = "SELECT MovieID, COUNT(OrderID) as NumberOfOrders FROM dbo.Orders WHERE Orders.RentalDate >= @date GROUP BY MovieID ORDER BY (NumberOfOrders) DESC";
                var popularThisWeek = context.Database.SqlQuery <StorePopular>(weekMovies, new SqlParameter("@date", date.ToShortDateString()));

                var top10 = popularThisWeek.Take(10); // 10 most popular movies of the week

                foreach (StorePopular popular in top10)
                {
                    Movie movie = context.Movies.Where(m => m.MovieID == popular.MovieID).Single();
                    movies.Add(movie);
                }
            }

            return(movies);
        }
Ejemplo n.º 3
0
        private List <Movie> SearchByGenre(string genre)
        {
            List <Movie> movies = new List <Movie>();

            using (var context = new MovieRentalEntities())
            {
                movies = context.Movies.Where(movie => movie.Genre.Equals(genre)).ToList();
                return(movies);
            }
        }
Ejemplo n.º 4
0
        private List <Movie> SearchByTitle(string title)
        {
            List <Movie> movies = new List <Movie>();

            using (var context = new MovieRentalEntities())
            {
                movies = context.Movies.Where(movie => movie.Title.Contains(SearchBox.Text)).ToList();
                return(movies);
            }
        }
Ejemplo n.º 5
0
        public List <Movie> GeneratePersonalRecommendations()
        {
            using (var context = new MovieRentalEntities())
            {
                List <Movie> recs = new List <Movie>();

                var query    = "SELECT Genre, COUNT(OrderID) as NumberOfOrders FROM Orders, Movie, Customer WHERE Orders.MovieID = Movie.MovieID AND Customer.AccountNumber = @account GROUP BY Genre ORDER BY (NumberOfOrders) DESC";
                var favGenre = context.Database.SqlQuery <StoreGenreCount>(query, new SqlParameter("@account", customer.AccountNumber)).First();

                // Customer's favorite genre
                string genre = favGenre.Genre;

                var moviesByGenre = context.Movies.Where(m => m.Genre == genre).ToList();

                recs.AddRange(moviesByGenre);

                var actorQuery = "SELECT ActorID, Count(OrderID) as NumberOfOrders FROM Orders, Credits, Customer WHERE Orders.MovieID = Credits.MovieID AND Customer.AccountNumber = 2 GROUP BY ActorID ORDER BY (NumberOfOrders) DESC";
                var favorites  = context.Database.SqlQuery <StoreFavoriteActors>(actorQuery, new SqlParameter("@account", customer.AccountNumber)).ToList();

                var top = favorites.First();

                List <int> favoriteActors = new List <int>();

                // First favorite actor
                favoriteActors.Add(top.ActorID);

                // Check for any other actors that have the same number of orders as the first one in the list
                int count = top.NumberOfOrders;

                foreach (StoreFavoriteActors result in favorites)
                {
                    // Add the actor if order count is equal
                    if (result.NumberOfOrders == count)
                    {
                        favoriteActors.Add(result.ActorID);
                    }
                }

                foreach (int actorID in favoriteActors)
                {
                    var credits = context.Credits.Where(c => c.ActorID == actorID).ToList();

                    foreach (Credit credit in credits)
                    {
                        var movie = context.Movies.Where(m => m.MovieID == credit.MovieID).Single();
                        recs.Add(movie);
                    }
                }

                return(recs.Distinct().ToList());
            }
        }
Ejemplo n.º 6
0
        private void ResetButton_Click(object sender, RoutedEventArgs e)
        {
            RecommendationText.Visibility = Visibility.Hidden;
            using (var context = new MovieRentalEntities())
            {
                var movies = from m in context.Movies
                             where m.NumberOfCopies > 0
                             select m;

                MovieList.ItemsSource   = movies.ToList();
                MovieList.SelectedIndex = 0;
            }
        }
Ejemplo n.º 7
0
        public List <Movie> GenerateRecommendationsByActor(int id)
        {
            using (var context = new MovieRentalEntities())
            {
                var credits = context.Credits.Where(c => c.ActorID == id).ToList();

                List <Movie> recs = new List <Movie>();

                foreach (Credit credit in credits)
                {
                    var movie = context.Movies.Where(m => m.MovieID == credit.MovieID).Single();

                    recs.Add(movie);
                }

                return(recs);
            }
        }
Ejemplo n.º 8
0
        public BrowseMovies(Customer customer)
        {
            InitializeComponent();
            this.customer = customer;

            SearchBy.Items.Add("Actor");
            SearchBy.Items.Add("Genre");
            SearchBy.Items.Add("Title");
            SearchBy.Items.Add("Most Popular");

            using (var context = new MovieRentalEntities())
            {
                var movies = from m in context.Movies
                             where m.NumberOfCopies > 0
                             select m;

                MovieList.DisplayMemberPath = "Title";
                MovieList.SelectedValuePath = "MovieID";

                MovieList.ItemsSource   = movies.ToList();
                MovieList.SelectedIndex = 0;
            }

            Genres.ItemsSource       = GenreDict.genreDict;
            Genres.SelectedValuePath = "Value";
            Genres.DisplayMemberPath = "Value";
            Genres.SelectedIndex     = 0;

            List <string> spans = new List <string>();

            spans.Add("This week"); spans.Add("This month"); spans.Add("This year");
            Timespan.ItemsSource = spans;

            // Initially search by titles and hide the genre combobox
            SearchBy.SelectedIndex = 2;
            Genres.Visibility      = Visibility.Hidden;
            Timespan.Visibility    = Visibility.Hidden;

            ActorList.SelectedIndex = -1;

            RecommendationText.Visibility = Visibility.Hidden;
        }
Ejemplo n.º 9
0
        private List <Movie> GetYearPopular()
        {
            List <Movie> movies = new List <Movie>();

            DateTime firstOfYear = new DateTime(DateTime.Now.Year, 1, 1);

            using (var context = new MovieRentalEntities())
            {
                var yearMovies      = "SELECT MovieID, COUNT(OrderID) as NumberOfOrders FROM dbo.Orders WHERE Orders.RentalDate >= @date GROUP BY MovieID ORDER BY (NumberOfOrders) DESC";
                var popularThisYear = context.Database.SqlQuery <StorePopular>(yearMovies, new SqlParameter("@date", firstOfYear.ToShortDateString()));

                var top10 = popularThisYear.Take(10); // 10 most popular movies of the year

                foreach (StorePopular popular in top10)
                {
                    Movie movie = context.Movies.Where(m => m.MovieID == popular.MovieID).Single();
                    movies.Add(movie);
                }
            }

            return(movies);
        }
Ejemplo n.º 10
0
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            SearchMovie current = (SearchMovie)MovieListBox.SelectedItem;

            Credits credits = client.GetMovieCreditsAsync(current.Id).Result;

            int copies; decimal fee;

            foreach (Cast cast in credits.Cast)
            {
                // Top 5 actors
                if (cast.Order < 5)
                {
                    Person person = client.GetPersonAsync(cast.Id).Result;

                    int    id = person.Id;
                    string firstName, lastName;
                    string gender = person.Gender.ToString();
                    string sex;

                    if (gender == "Male" || gender == "Female")
                    {
                        sex = gender[0].ToString();
                    }
                    else
                    {
                        continue;
                    }


                    var today = DateTime.Today;

                    int age = today.Year - person.Birthday.GetValueOrDefault().Year;

                    string fullName = person.Name;
                    var    names    = fullName.Split(' ');

                    // Just take first and last name if there are more than two names
                    if (names.Length > 2)
                    {
                        firstName = names[0]; lastName = names[names.Length - 1];
                    }
                    else if (names.Length == 2)
                    {
                        firstName = names[0]; lastName = names[1];
                    }
                    else
                    {
                        continue; // Skip adding this actor, does not conform to the database
                    }

                    using (var context = new MovieRentalEntities())
                    {
                        // If the actor exists, don't do anything
                        if (context.Actors.Any(a => a.ActorID == id))
                        {
                        }
                        else
                        {
                            Actor actor = new Actor()
                            {
                                ActorID   = id,
                                FirstName = firstName,
                                LastName  = lastName,
                                Sex       = sex,
                                Age       = age,
                                Rating    = 1
                            };

                            context.Actors.Add(actor);
                            context.SaveChanges();
                        }
                    }

                    using (var context = new MovieRentalEntities())
                    {
                        // Add the actor's credits for this movie into the database
                        Credit credit = new Credit()
                        {
                            MovieID = current.Id,
                            ActorID = id
                        };

                        try
                        {
                            context.Credits.Add(credit);
                            context.SaveChanges();
                        }
                        catch
                        {
                            continue; // Skip this credit
                        }
                    }
                }
            }

            var image = client.GetMovieImagesAsync(current.Id);

            using (var context = new MovieRentalEntities())
            {
                if (context.Movies.Any(m => m.MovieID == current.Id))
                {
                    MessageBox.Show("Movie is already in the database");
                    return;
                }

                try
                {
                    copies = Convert.ToInt32(NumberOfCopies.Text);
                }
                catch
                {
                    MessageBox.Show("Error in number of copies");
                    return;
                }

                try
                {
                    fee = Convert.ToDecimal(DistFee.Text);
                }
                catch
                {
                    MessageBox.Show("Error in distribution fee");
                    return;
                }


                Movie movie = new Movie()
                {
                    MovieID         = current.Id,
                    Title           = current.Title,
                    Genre           = GenreDict.genreDict[current.GenreIds[0]], // First available genre for the movie
                    DistributionFee = fee,
                    NumberOfCopies  = copies,
                    Rating          = (int)Math.Round(current.VoteAverage / 2)
                };

                context.Movies.Add(movie);
                context.SaveChanges();

                MessageBox.Show("Movie added successfully!");
            }
        }
Ejemplo n.º 11
0
        private void Movies_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ActorList.Items.Clear();
            try
            {
                Movie current = (Movie)MovieList.SelectedItem;
                MovieTitle.Text = current.Title;

                APIMovie movie = client.GetMovieAsync(current.MovieID).Result;

                // Use the API to get the movie poster
                Uri               apiUri     = new Uri("http://image.tmdb.org/t/p/w342//");
                string            posterPath = movie.PosterPath;
                System.UriBuilder uriBuilder = new System.UriBuilder(apiUri);
                uriBuilder.Path += posterPath;


                // TODO: Check if this is null
                Poster.Source = new BitmapImage(uriBuilder.Uri);

                MovieTitle.Text    = current.Title;
                MovieOverview.Text = movie.Overview;
                GenreText.Text     = current.Genre;

                int rating = current.Rating;

                if (rating >= 4)
                {
                    RatingCircle.Stroke     = Brushes.Green;
                    RatingNumber.Text       = rating.ToString();
                    RatingNumber.Foreground = Brushes.Green;
                }
                else if (rating >= 2 && rating < 4)
                {
                    RatingCircle.Stroke     = Brushes.Gold;
                    RatingNumber.Text       = rating.ToString();
                    RatingNumber.Foreground = Brushes.Gold;
                }
                else
                {
                    RatingCircle.Stroke     = Brushes.Red;
                    RatingNumber.Text       = rating.ToString();
                    RatingNumber.Foreground = Brushes.Red;
                }

                using (var context = new MovieRentalEntities())
                {
                    var query = context.Credits.Where(c => c.MovieID == current.MovieID).ToList();

                    foreach (Credit credit in query)
                    {
                        var actor = context.Actors.Where(a => a.ActorID == credit.ActorID).Single();

                        ActorList.Items.Add(actor);
                    }

                    ActorList.DisplayMemberPath = "FullName";
                }
            }
            catch (NullReferenceException error)
            {
                Console.WriteLine(error.Message);
            }
        }
Ejemplo n.º 12
0
        // Search movies by actor name
        private List <Movie> SearchByActor(string name)
        {
            List <Movie> movies = new List <Movie>();
            List <Actor> actors = new List <Actor>();

            // User entered both first and last name
            if (name.Contains(" "))
            {
                string   first; string last;
                string[] names = name.Split(' ');

                first = names[0];
                last  = names[1];

                using (var context = new MovieRentalEntities())
                {
                    var firstSearch = context.Actors.Where(a => a.FirstName.Equals(first)).ToList();
                    var lastSearch  = context.Actors.Where(a => a.LastName.Equals(last)).ToList();

                    actors.AddRange(firstSearch);
                    actors.AddRange(lastSearch);

                    var unique = new HashSet <Actor>(actors);

                    foreach (Actor actor in unique)
                    {
                        string fullName = actor.FirstName + " " + actor.LastName;

                        // Rebuild the full name and check if it is equal to the searched name
                        if (fullName == name)
                        {
                            var actorCredits = context.Credits.Where(credit => credit.ActorID == actor.ActorID).ToList();

                            // Add every movie the actor has been in to the result set
                            foreach (Credit credit in actorCredits)
                            {
                                var movie = context.Movies.Where(m => m.MovieID == credit.MovieID).FirstOrDefault();
                                movies.Add(movie);
                            }
                        }
                    }
                }
            }
            else
            {
                // User entered either a first name or a last name
                using (var context = new MovieRentalEntities())
                {
                    // E.g - User entered "Chris", find all Chris/Christopher/Christian/etc actors
                    var firstSearch = context.Actors.Where(a => a.FirstName.Contains(name)).ToList();

                    // E.g - User entered "Pratt", find all actors with last name Pratt or something containing Pratt
                    var lastSearch = context.Actors.Where(a => a.LastName.Contains(name)).ToList();

                    // Add all matches to the list
                    actors.AddRange(firstSearch);
                    actors.AddRange(lastSearch);

                    var unique = new HashSet <Actor>(actors);

                    foreach (Actor actor in unique)
                    {
                        var actorCredits = context.Credits.Where(credit => credit.ActorID == actor.ActorID).ToList();

                        // Add every movie the actor has been in to the result set
                        foreach (Credit credit in actorCredits)
                        {
                            var movie = context.Movies.Where(m => m.MovieID == credit.MovieID).FirstOrDefault();
                            movies.Add(movie);
                        }
                    }
                }
            }

            return(movies);
        }
Ejemplo n.º 13
0
        private void Rent_Click(object sender, RoutedEventArgs e)
        {
            Movie current = (Movie)MovieList.SelectedItem;

            using (var context = new MovieRentalEntities())
            {
                // The first day of the current month
                DateTime firstOfMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);

                var countMonth    = "SELECT COUNT(*) FROM dbo.Orders WHERE RentalDate > @date AND AccountNumber = @account";
                var countRequests = "SELECT COUNT(*) FROM dbo.Orders WHERE RentalDate IS NULL AND AccountNumber = @account";
                var countCurrent  = "SELECT COUNT(*) FROM dbo.Orders WHERE AccountNumber = @account AND RentalDate > @date AND ActualReturn IS NULL";

                var monthlyOrders = context.Database.SqlQuery <int>(countMonth, new SqlParameter("@date", firstOfMonth),
                                                                    new SqlParameter("@account", customer.AccountNumber)).Single();

                var requests = context.Database.SqlQuery <int>(countRequests, new SqlParameter("@account", customer.AccountNumber)).Single();

                var currentOrders = context.Database.SqlQuery <int>(countCurrent, new SqlParameter("@account", customer.AccountNumber),
                                                                    new SqlParameter("@date", firstOfMonth)).Single();

                Console.WriteLine(currentOrders);

                int account = customer.AccountType;

                if ((monthlyOrders == 1 || requests == 1) && account == 0)
                {
                    MessageBox.Show("You have already rented your movie for the month");
                    return;
                }

                if (account == 1)
                {
                    if (currentOrders == 1 || requests == 1)
                    {
                        MessageBox.Show("You can only rent one movie at a time. Please return a movie or wait for your previous orders to be approved.");
                        return;
                    }
                }
                if (account == 2)
                {
                    if (currentOrders == 2 || requests == 2)
                    {
                        MessageBox.Show("You can only rent two movies at a time. Please return a movie or wait for your previous orders to be approved.");
                        return;
                    }
                }
                if (account == 3)
                {
                    if (currentOrders == 3 || requests == 3)
                    {
                        MessageBox.Show("You can only rent three movies at a time. Please return a movie or wait for your previous orders to be approved.");
                        return;
                    }
                }


                // Order to be approved by an employee
                Order order = new Order()
                {
                    MovieID       = current.MovieID,
                    AccountNumber = customer.AccountNumber,
                };

                try
                {
                    context.Orders.Add(order);
                    context.SaveChanges();

                    MessageBox.Show("Your request to rent " + current.Title + " has been sent");
                }
                catch (DbUpdateException)
                {
                }
            }
        }