public void openDeckCreatorAndCloseClassSelector(ClassSelectorWindow window, string selectedClass)
 {
     Deck deck = new Deck();
     deck.DeckClass = selectedClass;
     deck.DeckName = "New Deck";
     deck.DeckUser = GlobalVariables.LoggedInUser;
     DeckCreatorWindow deckCreatorWindow = new DeckCreatorWindow(deck);
     deckCreatorWindow.Show();
     window.Close();
 }
 public DeckCreatorWindow(Deck deck)
 {
     openDeck = deck;
     controller = new DeckCreatorWindowController();
     InitializeComponent();
     initializeAllCardList();
     initializeDeckCardList();
     if (openDeck.DeckId == null)
     {
         controller.saveDeckFirstTime(openDeck);
         openDeck = controller.getNewestDeckByUser(GlobalVariables.LoggedInUser);
     }
     SelectedClass = openDeck.DeckClass;
     txtDeckname.Text = openDeck.DeckName;
 }
        public bool AddCardToDeck(Card card, Deck deck)
        {
            NpgsqlConnection conn = databaseConnection.ConnectToDatabase();
            conn.CreateCommand();
            NpgsqlCommand command = new NpgsqlCommand("insert into dbcardtodeck(deckid, cardid) values(:value1, :value2)", conn);
            command.Parameters.Add(new NpgsqlParameter("value1", DbType.String));
            command.Parameters.Add(new NpgsqlParameter("value2", DbType.String));
            command.Parameters[0].Value = card.CardId;
            command.Parameters[1].Value = deck.DeckId;
            command.Connection = conn;
            if (databaseConnection.ExecuteChangeQuery(command, conn))
            {
                return true;
            }

            return false;
        }
 public List<Card> getAllCardsOfDeck(Deck deck)
 {
     List<Card> cardList = new List<Card>();
     NpgsqlConnection conn = databaseConnection.ConnectToDatabase();
     conn.CreateCommand();
     NpgsqlCommand command = new NpgsqlCommand("select * from dbcardtodeck where deckid = :value1", conn);
     command.Parameters.Add(new NpgsqlParameter("value1", DbType.String));
     command.Parameters[0].Value = deck.DeckId;
     command.Connection = conn;
     DataTable result = databaseConnection.ExecuteSelectQuery(command, conn);
     if (result != null)
     {
         for (int i = 0; i < result.Rows.Count; i++)
         {
             cardList.Add(getCardById(result.Rows[i].ItemArray[1].ToString()));
         }
     }
     return cardList;
 }
        public bool AddCardsToDeck(List<Card> cardList, Deck deck)
        {
            string sqlQuery = "insert into dbcardtodeck(deckid, cardid) values ";
            foreach (Card c in cardList)
            {
                sqlQuery += "('" + deck.DeckId + "', '" + c.CardId + "'), ";
            }

            sqlQuery = sqlQuery.Substring(0, sqlQuery.Length - 2);
            NpgsqlConnection conn = databaseConnection.ConnectToDatabase();
            conn.CreateCommand();
            NpgsqlCommand command = new NpgsqlCommand(sqlQuery, conn);
            command.Connection = conn;
            if (databaseConnection.ExecuteChangeQuery(command, conn))
            {
                return true;
            }

            return false;
        }
 private void ListViewItemDeck_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
 {
     var item = sender as ListViewItem;
     if (item != null && item.IsSelected)
     {
         Deck selectedDeck = controller.getDecksOfLoggedInUser().ToArray()[lvwDeckList.SelectedIndex];
         Deck newDeck = new Deck();
         newDeck.DeckId = selectedDeck.DeckId;
         newDeck.DeckName = selectedDeck.DeckName;
         newDeck.DeckUser = selectedDeck.DeckUser;
         newDeck.DeckClass = selectedDeck.DeckClass;
         foreach (Card card in controller.getCardsOfDeck(newDeck))
         {
             newDeck.CardList.Add(controller.getCardById(card.CardId));
         }
         controller.openDeckCreatorWindow(newDeck);
     }
 }
 public void openDeckCreatorWindow(Deck deck)
 {
     DeckCreatorWindow deckCreatorWindow = new DeckCreatorWindow(deck);
     deckCreatorWindow.Show();
 }
 public List<Card> getCardsOfDeck(Deck deck)
 {
     return cardDatabaseController.getAllCardsOfDeck(deck);
 }
 public void deleteSelectedDeck(Deck deck)
 {
     deckDatabaseController.deleteDeck(deck);
 }
 public void addCardToDeck(Card card, Deck deck)
 {
     deckDatabaseController.AddCardToDeck(card, deck);
 }
        public bool UpdateDeckList(Deck updatedDeck)
        {
            if (HasDeckChanged(updatedDeck))
            {
                NpgsqlConnection conn = databaseConnection.ConnectToDatabase();
                conn.CreateCommand();
                NpgsqlCommand command = new NpgsqlCommand("update dbdeck set deckname = :value1 where deckid = :value2", conn);
                command.Parameters.Add(new NpgsqlParameter("value1", DbType.String));
                command.Parameters.Add(new NpgsqlParameter("value2", DbType.String));
                command.Parameters[0].Value = updatedDeck.DeckName;
                command.Parameters[1].Value = updatedDeck.DeckId;
                command.Connection = conn;
                if (databaseConnection.ExecuteChangeQuery(command, conn))
                {
                    conn = databaseConnection.ConnectToDatabase();
                    conn.CreateCommand();

                    // Building SQL Query
                    string sqlQuery = "update dbcardtodeck as ctd set cardid = c.cardid from (values ";
                    foreach (Card c in updatedDeck.CardList)
                    {
                        sqlQuery += "('" + c.CardToDeckId + "', '" + c.CardId + "'), ";
                    }

                    sqlQuery = sqlQuery.Substring(0, sqlQuery.Length - 2);
                    sqlQuery += ") as c(cardtodeckid, cardid) where c.cardtodeckid = ctd.cardtodeckid";

                    command = new NpgsqlCommand(sqlQuery, conn);
                    command.Connection = conn;
                    if (databaseConnection.ExecuteChangeQuery(command, conn))
                    {
                        return true;
                    }
                }
            }

            return false;
        }
 public void saveDeck(Deck deck)
 {
     deckDatabaseController.ResetCardsInDeck(deck);
     deckDatabaseController.UpdateDeckList(deck);
     deckDatabaseController.AddCardsToDeck(deck.CardList, deck);
 }
 public void removeCardFromDeck(Card card, Deck deck)
 {
 }
 public bool deleteDeck(Deck deck)
 {
     NpgsqlConnection conn = databaseConnection.ConnectToDatabase();
         conn.CreateCommand();
         NpgsqlCommand command = new NpgsqlCommand("delete from dbdeck where deckid = :value1", conn);
         command.Parameters.Add(new NpgsqlParameter("value1", DbType.String));
         command.Parameters[0].Value = deck.DeckId;
         command.Connection = conn;
         if (databaseConnection.ExecuteChangeQuery(command, conn))
         {
             return true;
         }
         return false;
 }
        private bool HasDeckChanged(Deck updatedDeck)
        {
            Deck deckOnDB = GetDeckById(updatedDeck.DeckId);
            if (deckOnDB.DeckName.Equals(updatedDeck.DeckName))
            {
                if (updatedDeck.CardList.Count == deckOnDB.CardList.Count)
                {
                    int index = 0;
                    foreach (Card c in deckOnDB.CardList)
                    {
                        if (!c.CardId.Equals(updatedDeck.CardList[index].CardId))
                        {
                            return true;
                        }

                        index++;
                    }
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return true;
            }

            return false;
        }
 public void saveDeckFirstTime(Deck newDeck)
 {
     deckDatabaseController.CreateNewDeck(newDeck.DeckName, newDeck.DeckUser, newDeck.DeckClass);
 }
        public Deck GetDeckById(string deckId)
        {
            Deck returnDeck = new Deck();
            NpgsqlConnection conn = databaseConnection.ConnectToDatabase();
            conn.CreateCommand();
            NpgsqlCommand command = new NpgsqlCommand("select * from dbdeck where deckid = :value1", conn);
            command.Parameters.Add(new NpgsqlParameter("value1", DbType.String));
            command.Parameters[0].Value = deckId;
            command.Connection = conn;
            DataTable result = databaseConnection.ExecuteSelectQuery(command, conn);
            if (result != null)
            {
                UserDatabaseController udc = new UserDatabaseController();
                returnDeck.DeckId = deckId;
                returnDeck.DeckName = result.Rows[0].ItemArray[1].ToString();
                returnDeck.DeckUser = udc.GetUser(result.Rows[0].ItemArray[2].ToString());
                conn = databaseConnection.ConnectToDatabase();
                conn.CreateCommand();
                command = new NpgsqlCommand("select deckid, cardid, cardtodeckid from dbcardtodeck where deckid = :value1", conn);
                command.Parameters.Add(new NpgsqlParameter("value1", DbType.String));
                command.Parameters[0].Value = deckId;
                command.Connection = conn;
                result = databaseConnection.ExecuteSelectQuery(command, conn);
                if (result != null)
                {
                    for (int i = 0; i < result.Rows.Count; i++)
                    {
                        returnDeck.CardList.Add(new Card(result.Rows[i].ItemArray[1].ToString(), result.Rows[i].ItemArray[2].ToString()));
                    }
                }
            }

            return returnDeck;
        }