//Function that is performed when button is clicked
        //Adjusts depending on action to be performed and node being performed on
        private async void Add_Click(object sender, RoutedEventArgs e)
        {
            FirebaseResponse resp = await client.GetTaskAsync("Counter/" + Node); //Get counter data

            Counter get  = resp.ResultAs <Counter>();                             //Set response to counter object
            object  data = null;
            object  obj  = null;

            if (Action == "Add")
            {
                try
                {
                    //Creates new Sport object with data inserted from user
                    data = new Sport
                    {
                        SportID    = Convert.ToInt32(get.cnt) + 1,
                        SportName  = txt2.Text,
                        SportRules = txt3.Text,
                    };

                    //Create new counter object with cnt being the same as the ID of the sport object created above
                    obj = new Counter
                    {
                        cnt = Convert.ToInt32(get.cnt) + 1
                    };
                }
                catch (Exception ex) //Catches exception when input data is incorrect
                {
                    MessageBox.Show(ex.Message + ", Check what you inserted in the Textboxes");
                    return;
                }

                string      index    = (get.cnt + 1).ToString();
                SetResponse response = await client.SetTaskAsync(Node + "/" + index, data); //Adds sport object to databse

                SetResponse response1 = await client.SetTaskAsync("Counter/" + Node, obj);  //Updates count in database

                //Resets all textboxes back to blank
                this.txt1.Text = "";
                this.txt2.Text = "";
                this.txt3.Text = "";


                MessageBox.Show("Data Inserted");
            }
            else if (Action == "Update")
            {
                try
                {
                    //Creates new sport object with data inserted in the textboxes
                    data = new Sport
                    {
                        SportID    = int.Parse(txt1.Text),
                        SportName  = txt2.Text,
                        SportRules = txt3.Text,
                    };
                }
                catch (Exception ex) //Catches exception when input data is incorrect
                {
                    MessageBox.Show(ex.Message + ", Check what you inserted in the Textboxes");
                    return;
                }
                FirebaseResponse response = await client.UpdateTaskAsync(Node + "/" + txt1.Text, data); //Updates information on database

                MessageBox.Show("Data Updated");
            }
            else if (Action == "Retrieve")
            {
                string print = null;
                if (txt1.IsEnabled)
                {
                    FirebaseResponse response = await client.GetTaskAsync("Counter/" + Node); //Gets counter information

                    Counter cnt = response.ResultAs <Counter>();                              //Sets counter object to response

                    try
                    {
                        //Checks if ID entered is valid

                        int count = cnt.cnt;

                        int ID = int.Parse(txt1.Text);

                        if (ID > count || ID < 1)
                        {
                            MessageBox.Show(txt1.Text + " is not a valid ID");
                            return;
                        }
                    }
                    catch (Exception ex)//Catches exception when input data is incorrect
                    {
                        MessageBox.Show(ex.Message + ", Check what you inserted in the Textboxes");
                        return;
                    }

                    response = await client.GetTaskAsync(Node + "/" + txt1.Text); //Retrieves data that user prompted

                    Sport result = response.ResultAs <Sport>();                   //Set to Sport result

                    print = result.printAll();                                    //Calls print function to get information in a string

                    MessageBox.Show(print);                                       //Shows information to user
                }
                else
                {
                    List <string> elements = new List <string>(); //Creates a list that holds strings with all selected information

                    //Iterates through all instances of a certain node
                    for (int i = 1; i <= get.cnt; i++)
                    {
                        FirebaseResponse result = await client.GetTaskAsync(Node + "/" + i); //Gets node information

                        try
                        {
                            //Set response to a Sport object
                            Sport sport = result.ResultAs <Sport>();
                            //Bool to check if node being analyzed in this iteration can be added to the list
                            bool addElement = true;

                            //If cooresponding label is enabled
                            if (NameLabel)
                            {
                                if (txt2.Text != "")                  //Check whether user inputted into cooresponding textbox
                                {
                                    if (sport.SportName != txt2.Text) //Checks if user input does not cooresponds to data on databse
                                    {
                                        addElement = false;           //Sets addElement to false
                                    }
                                }
                            }
                            //Look at above comments
                            if (RuleLabel)
                            {
                                if (txt3.Text != "")
                                {
                                    if (sport.SportRules != txt3.Text)
                                    {
                                        addElement = false;
                                    }
                                }
                            }

                            if (addElement) //Calls print function and adds element to list
                            {
                                elements.Add(sport.printRestirction(RuleLabel, NameLabel));
                            }
                        }
                        catch (Exception ex) //Catches exception when input data is incorrect
                        {
                            MessageBox.Show(ex.Message + ", Check what you inserted in the Textboxes");
                            return;
                        }
                    }

                    if (elements.Count == 0) //If list is empty then inform user
                    {
                        MessageBox.Show("There are no elements with the inputs inserted!");
                    }

                    foreach (string ele in elements)  //Print all selected information according to user
                    {
                        MessageBox.Show(ele);
                    }
                }
            }
        }
Exemplo n.º 2
0
        //Function that calls when delete button is clicked
        //Adjust depending on node that was selected
        private async void Delete_Click(object sender, RoutedEventArgs e)
        {
            int startID = 0;


            FirebaseResponse response = await client.GetTaskAsync(Node + "/" + txt1.Text); //Get instance from databse

            FirebaseResponse resp = await client.GetTaskAsync("Counter/" + Node);          //Get amount of instances for a certain node

            Counter cnt = resp.ResultAs <Counter>();                                       //Set counter object to the response

            try
            {
                //Check if ID entered is valid

                int count = cnt.cnt;

                int ID = int.Parse(txt1.Text);
                if (ID > count || ID < 1)
                {
                    MessageBox.Show(txt1.Text + " is not a valid ID");
                    return;
                }
            }
            catch (Exception ex) //Catch exception when input data is incorrect
            {
                MessageBox.Show(ex.Message + ", Check what you inserted in the Textbox");
                return;
            }

            //Depending on node that is selected
            //Create new instance and set starting ID to the instance ID
            if (Node == "Coaches")
            {
                Coaches data = response.ResultAs <Coaches>();
                startID = data.CoachId;
            }
            else if (Node == "Extras")
            {
                Extras data = response.ResultAs <Extras>();
                startID = data.ExtraID;
            }
            else if (Node == "League")
            {
                League data = response.ResultAs <League>();
                startID = data.LeagueID;
            }
            else if (Node == "Sports")
            {
                Sport data = response.ResultAs <Sport>();
                startID = data.SportID;
            }
            else if (Node == "Sport Teams")
            {
                SportTeams data = response.ResultAs <SportTeams>();
                startID = data.SportID;
            }
            else if (Node == "Players")
            {
                Players data = response.ResultAs <Players>();
                startID = data.PlayerID;
            }



            //In order to keep unique IDs and count we had to overwrite data
            //This for loop goes througth all instances from the startId to the last instance in database
            //It overwrites the startId instance with the next instance in the database and so on
            //While overwriting it is changing the unique ID
            //If it is on the last instance then it deletes that instance
            for (int i = startID; i <= cnt.cnt; i++)
            {
                if (i == cnt.cnt)
                {
                    FirebaseResponse delete = await client.DeleteTaskAsync(Node + "/" + cnt.cnt);

                    cnt.cnt -= 1;
                    resp     = await client.UpdateTaskAsync("Counter/" + Node, cnt);

                    break;
                }

                FirebaseResponse overwrite = await client.GetTaskAsync(Node + "/" + (i + 1));

                if (Node == "Coaches")
                {
                    Coaches data = overwrite.ResultAs <Coaches>();
                    data.CoachId = i;
                    FirebaseResponse updateIndex = await client.UpdateTaskAsync(Node + "/" + i, data);
                }
                else if (Node == "Extras")
                {
                    Extras data = overwrite.ResultAs <Extras>();
                    data.ExtraID = i;
                    FirebaseResponse updateIndex = await client.UpdateTaskAsync(Node + "/" + i, data);
                }
                else if (Node == "League")
                {
                    League data = overwrite.ResultAs <League>();
                    data.LeagueID = i;
                    FirebaseResponse updateIndex = await client.UpdateTaskAsync(Node + "/" + i, data);
                }
                else if (Node == "Sports")
                {
                    Sport data = overwrite.ResultAs <Sport>();
                    data.SportID = i;
                    FirebaseResponse updateIndex = await client.UpdateTaskAsync(Node + "/" + i, data);
                }
                else if (Node == "Sport Teams")
                {
                    SportTeams data = overwrite.ResultAs <SportTeams>();
                    data.TeamID = i;
                    FirebaseResponse updateIndex = await client.UpdateTaskAsync(Node + "/" + i, data);
                }
                else if (Node == "Players")
                {
                    Players data = overwrite.ResultAs <Players>();
                    data.PlayerID = i;
                    FirebaseResponse updateIndex = await client.UpdateTaskAsync(Node + "/" + i, data);
                }
            }

            MessageBox.Show("Deleted Element of ID: " + txt1.Text);
        }
        //Function when label is enabled and allowed for user interaction
        //For Update: Label ID is used to grab data of certain key
        //For Retrieve: Select labels that you want to be returned to you
        private async void Select(object sender, MouseButtonEventArgs e)
        {
            if (Action == "Retrieve")
            {
                Label label = sender as Label; //Gets selected label

                //Switches between normal and bold font weight
                if (label.FontWeight == FontWeights.Normal)
                {
                    label.FontWeight = FontWeights.Bold;

                    switch (label.Name)
                    {
                    //Enables ID textbox
                    //Disables all other textboxes
                    //Sets all font weights to normal
                    case "ID":
                        txt1.IsEnabled       = true;
                        NameLabel            = false;
                        SportName.FontWeight = FontWeights.Normal;
                        SportName.IsEnabled  = NameLabel;
                        RuleLabel            = false;
                        Rule.FontWeight      = FontWeights.Normal;
                        Rule.IsEnabled       = RuleLabel;
                        break;

                    //Enables cooresponding textbox
                    case "SportName":
                        NameLabel      = true;
                        txt2.IsEnabled = true;
                        break;

                    case "Rule":
                        RuleLabel      = true;
                        txt3.IsEnabled = true;
                        break;
                    }
                }
                else
                {
                    label.FontWeight = FontWeights.Normal;

                    switch (label.Name)
                    {
                    //Disables ID textbox
                    //Enables all other textboxes
                    case "ID":
                        txt1.IsEnabled      = false;
                        NameLabel           = false;
                        SportName.IsEnabled = true;
                        RuleLabel           = false;
                        Rule.IsEnabled      = true;
                        break;

                    //Disables cooresponding textbox
                    case "SportName":
                        NameLabel      = false;
                        txt2.IsEnabled = false;
                        break;

                    case "Rule":
                        RuleLabel      = false;
                        txt3.IsEnabled = false;
                        break;
                    }
                }
            }
            else if (Action == "Update")
            {
                if (txt1.IsEnabled == false)
                {
                    //Enables ID textbox
                    //Disables and clears all text boxes
                    txt1.IsEnabled      = true;
                    this.txt2.IsEnabled = false;
                    this.txt3.IsEnabled = false;
                    this.txt2.Text      = "";
                    this.txt3.Text      = "";
                    return;
                }

                FirebaseResponse resp = await client.GetTaskAsync("Counter/" + Node); //Get counter information

                Counter cnt = resp.ResultAs <Counter>();                              //Set response to counter object

                try
                {
                    //Checks if ID entered is valid

                    int count = cnt.cnt;

                    int ID = int.Parse(txt1.Text);

                    if (ID > count || ID < 1)
                    {
                        MessageBox.Show(txt1.Text + " is not a valid ID");
                        return;
                    }
                }
                catch (Exception ex) //Catches exception when input data enetered is incorrect
                {
                    MessageBox.Show(ex.Message + ", Check what you inserted in the Textboxes");
                    return;
                }


                resp = await client.GetTaskAsync(Node + "/" + txt1.Text);

                //Disables ID textbox
                //Enables all other textboxes for editing
                this.txt1.IsEnabled = false;
                this.txt2.IsEnabled = true;
                this.txt3.IsEnabled = true;

                //Display information from response to the cooresponding textboxes
                Sport result = resp.ResultAs <Sport>();
                this.txt2.Text = result.SportName;
                this.txt3.Text = result.SportRules;
            }
        }