コード例 #1
0
        //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);
                    }
                }
            }
        }