Exemplo n.º 1
0
        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                bool newRecord = (game == null);
                if (newRecord)
                {
                    Random rand  = new Random();
                    int    newId = rand.Next();
                    while (SQL.GetOutput("SELECT * FROM Boardgame WHERE id = " + newId).Count != 0)
                    {
                        newId = rand.Next();
                    }
                    game = new Game(newId, "");
                }
                else
                {
                    game.id = int.Parse(IdTextBlock.Text);
                }

                game.name         = TextBoxName.Text;
                game.price        = decimal.Parse(TextBoxPrice.Text);
                game.minPlayers   = int.Parse(TextBoxMinPlayers.Text);
                game.maxPlayers   = int.Parse(TextBoxMaxPlayers.Text);
                game.playTimeMins = int.Parse(TextBoxPlayTime.Text);
                game.releaseYear  = TextBoxYear.Text;
                game.avail        = (bool)CheckBoxAvail.IsChecked;

                if (newRecord)
                {
                    game.InsertSql();
                }
                else
                {
                    game.UpdateSql();
                }

                List <string>    updatedDesRaw     = new List <string>(TextBoxDes.Text.Split(new char[] { ';' }));
                List <string>    updatedPubRaw     = new List <string>(TextBoxPub.Text.Split(new char[] { ';' }));
                List <string>    updatedGenreRaw   = new List <string>(TextBoxGenres.Text.Split(new char[] { ';' }));
                List <Designer>  updatedDesigners  = new List <Designer>();
                List <Publisher> updatedPublishers = new List <Publisher>();
                List <Genre>     updatedGenres     = new List <Genre>();

                // Find from updated designers those that are already on record
                for (int i = 0; i < updatedDesRaw.Count; i++)
                {
                    string designerName = updatedDesRaw[i].Trim();
                    foreach (Designer designer in Global.designers)
                    {
                        if (designerName.Equals(designer.name))
                        {
                            updatedDesigners.Add(designer);
                            updatedDesRaw.RemoveAt(i);
                            break;
                        }
                    }
                }
                // Only left in the list are designers that have not been seen before
                foreach (string s in updatedDesRaw)
                {
                    Designer newDesigner = new Designer(s);
                    updatedDesigners.Add(newDesigner);
                    Global.designers.Add(newDesigner);
                }

                for (int i = 0; i < updatedPubRaw.Count; i++)
                {
                    string publisherName = updatedPubRaw[i].Trim();
                    foreach (Publisher publisher in Global.publishers)
                    {
                        if (publisherName.Equals(publisher.name))
                        {
                            updatedPublishers.Add(publisher);
                            updatedPubRaw.RemoveAt(i);
                            break;
                        }
                    }
                }
                foreach (string s in updatedPubRaw)
                {
                    Publisher newPublisher = new Publisher(s);
                    updatedPublishers.Add(newPublisher);
                    Global.publishers.Add(newPublisher);
                }

                for (int i = 0; i < updatedGenreRaw.Count; i++)
                {
                    string genreName = updatedGenreRaw[i];
                    foreach (Genre genre in Global.genres)
                    {
                        if (genreName.Equals(genre.name))
                        {
                            updatedGenres.Add(genre);
                            updatedGenreRaw.RemoveAt(i);
                        }
                    }
                }
                foreach (string s in updatedGenreRaw)
                {
                    Genre newGenre = new Genre(s);
                    updatedGenres.Add(newGenre);
                    Global.genres.Add(newGenre);
                }

                var addedDes      = updatedDesigners.ToArray().Except(designers.ToArray());
                var removedDes    = designers.ToArray().Except(updatedDesigners.ToArray());
                var addedPub      = updatedPublishers.ToArray().Except(publishers.ToArray());
                var removedPub    = publishers.ToArray().Except(updatedPublishers.ToArray());
                var addedGenres   = updatedGenres.ToArray().Except(genres.ToArray());
                var removedGenres = genres.ToArray().Except(updatedGenres.ToArray());

                foreach (var s in addedDes)
                {
                    SQL.ExecuteQuery("INSERT INTO GameDesignedBy VALUES (" + game.id + ", '" + s + "')");
                }
                foreach (var s in removedDes)
                {
                    SQL.ExecuteQuery("DELETE FROM GameDesignedBy WHERE boardgameID = " + game.id +
                                     " AND designerName = '" + s + "'");
                }
                foreach (var s in addedPub)
                {
                    SQL.ExecuteQuery("INSERT INTO GamePublishedBy VALUES (" + game.id + ", '" + s + "')");
                }
                foreach (var s in removedPub)
                {
                    SQL.ExecuteQuery("DELETE FROM GamePublishedBy WHERE boardgameID = " + game.id +
                                     " AND publisherName = '" + s + "'");
                }
                foreach (var s in addedGenres)
                {
                    SQL.ExecuteQuery("INSERT INTO GameGenreValue VALUES (" + game.id + ", '" + s + "')");
                }
                foreach (var s in removedGenres)
                {
                    SQL.ExecuteQuery("DELETE FROM GameGenreValue WHERE boardgameID = " + game.id +
                                     " AND genreName = '" + s + "'");
                }
                Close();
            }
            catch (FormatException exception)
            {
                MessageBox.Show("Error processing inputs. Please check that no letters are present in numeric field", "Error", MessageBoxButton.OK);
                Console.WriteLine(exception);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.StackTrace);
            }
        }
        private void UpdateLists(string condition)
        {
            Global.customers.Clear();
            Global.games.Clear();
            Global.reviews.Clear();
            Global.managers.Clear();
            Global.publishers.Clear();
            Global.designers.Clear();
            Global.genres.Clear();
            List <object[]> customersRaw  = SQL.GetOutput("SELECT * FROM Customer");
            List <object[]> gamesRaw      = SQL.GetOutput("SELECT * FROM Boardgame");
            List <object[]> reviewsRaw    = SQL.GetOutput("SELECT * FROM Review");
            List <object[]> managersRaw   = SQL.GetOutput("SELECT * FROM Manager");
            List <object[]> publishersRaw = SQL.GetOutput("SELECT * FROM Publisher");
            List <object[]> designersRaw  = SQL.GetOutput("SELECT * FROM Designer");
            List <object[]> genresRaw     = SQL.GetOutput("SELECT * FROM Genre");

            foreach (object[] values in customersRaw)
            {
                Customer.AddFromSql(values);
            }
            foreach (object[] values in gamesRaw)
            {
                Game.AddFromSql(values);
            }
            foreach (object[] values in reviewsRaw)
            {
                Review.AddFromSql(values);
            }
            foreach (object[] values in managersRaw)
            {
                Manager.AddFromSql(values);
            }
            foreach (object[] values in publishersRaw)
            {
                Publisher.AddFromSql(values);
            }
            foreach (object[] values in designersRaw)
            {
                Designer.AddFromSql(values);
            }
            foreach (object[] values in genresRaw)
            {
                Genre.AddFromSql(values);
            }
            searchedGames.Clear();
            foreach (Game game in Global.games)
            {
                searchedGames.Add(game);
            }
            if (condition != "")
            {
                for (int i = 0; i < searchedGames.Count; i++)
                {
                    if (!searchedGames[i].ToString().ToLower().Contains(condition))
                    {
                        searchedGames.RemoveAt(i--);
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void UpdateLists(string condition)
        {
            Global.customers.Clear();
            Global.games.Clear();
            Global.reviews.Clear();
            Global.managers.Clear();
            Global.publishers.Clear();
            Global.designers.Clear();
            Global.genres.Clear();
            List <object[]> customersRaw  = SQL.GetOutput("SELECT * FROM Customer");
            List <object[]> gamesRaw      = SQL.GetOutput("SELECT * FROM Boardgame");
            List <object[]> reviewsRaw    = SQL.GetOutput("SELECT * FROM Review");
            List <object[]> managersRaw   = SQL.GetOutput("SELECT * FROM Manager");
            List <object[]> publishersRaw = SQL.GetOutput("SELECT * FROM Publisher");
            List <object[]> designersRaw  = SQL.GetOutput("SELECT * FROM Designer");
            List <object[]> genresRaw     = SQL.GetOutput("SELECT * FROM Genre");

            foreach (object[] values in customersRaw)
            {
                Customer.AddFromSql(values);
            }
            foreach (object[] values in gamesRaw)
            {
                Game.AddFromSql(values);
            }
            foreach (object[] values in reviewsRaw)
            {
                Review.AddFromSql(values);
            }
            foreach (object[] values in managersRaw)
            {
                Manager.AddFromSql(values);
            }
            foreach (object[] values in publishersRaw)
            {
                Publisher.AddFromSql(values);
            }
            foreach (object[] values in designersRaw)
            {
                Designer.AddFromSql(values);
            }
            foreach (object[] values in genresRaw)
            {
                Genre.AddFromSql(values);
            }
            searchedCustomers.Clear();
            searchedGames.Clear();
            searchedReviews.Clear();
            searchedManagers.Clear();
            foreach (Customer customer in Global.customers)
            {
                searchedCustomers.Add(customer);
            }
            foreach (Game game in Global.games)
            {
                searchedGames.Add(game);
            }
            foreach (Review review in Global.reviews)
            {
                searchedReviews.Add(review);
            }
            foreach (Manager manager in Global.managers)
            {
                searchedManagers.Add(manager);
            }
            if (condition != "")
            {
                if (ButtonMainAction.Content == CUST_BUTTON_TEXT)
                {
                    for (int i = 0; i < searchedCustomers.Count; i++)
                    {
                        if (!searchedCustomers[i].ToString().ToLower().Contains(condition))
                        {
                            searchedCustomers.RemoveAt(i--);
                        }
                    }
                }
                else if (ButtonMainAction.Content == GAME_BUTTON_TEXT)
                {
                    for (int i = 0; i < searchedGames.Count; i++)
                    {
                        if (!searchedGames[i].ToString().ToLower().Contains(condition))
                        {
                            searchedGames.RemoveAt(i--);
                        }
                    }
                }
                else if (ButtonMainAction.Content == REVIEW_BUTTON_TEXT)
                {
                    for (int i = 0; i < searchedReviews.Count; i++)
                    {
                        if (!searchedReviews[i].ToString().ToLower().Contains(condition))
                        {
                            searchedReviews.RemoveAt(i--);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < searchedManagers.Count; i++)
                    {
                        if (!searchedManagers[i].ToString().ToLower().Contains(condition))
                        {
                            searchedManagers.RemoveAt(i--);
                        }
                    }
                }
            }
        }