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");
                }
            }
        }
Exemple #2
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!");
            }
        }
        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)
                {
                }
            }
        }