Ejemplo n.º 1
0
        private void DeleteSetBtn_Click(object sender, EventArgs e)
        {
            ChooseSet.Text = "";
            ChooseSet.Items.Remove(currentSet.SetName);

            Text1.Clear();
            Text2.Clear();
            Title1.Clear();
            Title2.Clear();
            PointerInput.Clear();
            PointerVal.Text = "";

            Querey querey = new Querey();

            using (querey.connection = querey.NewConn)
            {
                string[][] tables = { new string[] { "Sets", "Id" }, new string[] { "Flashcards", "SetId" }, new string[] { "Permissions", "SetId" }, new string[] { "SetProgress", "SetId" } };

                foreach (string[] table in tables)
                {
                    using (querey.command = querey.NewComm)
                    {
                        querey.command.CommandText = "DELETE FROM " + table[0] + " WHERE " + table[1] + "= @setid;";
                        querey.AddParameter("@setid", currentCard.setId);
                        querey.command.ExecuteNonQuery();
                    }
                }
            }

            currentSet = null;

            ChangeEdit(false);
            SaveChanges();
            new MessageForm("deleted").Show();
        }
Ejemplo n.º 2
0
        private void Insert_Click(object sender, EventArgs e) // inserts set into database and then updates cloud
        {
            Querey getSetId = new Querey();
            bool   used;

            using (getSetId.connection = getSetId.NewConn)
            {
                using (getSetId.command = getSetId.NewComm)
                {
                    SetIdCommand(getSetId);
                    using (getSetId.reader = getSetId.command.ExecuteReader())
                    {
                        used = getSetId.reader.Read();
                    }
                }
            }

            if (SetName.Text == "")
            {
                MessageForm errorForm = new MessageForm("Please enter a set name.");
                errorForm.Show();
            }
            else if (!used)
            {
                if (Title1.Text == "" || Title2.Text == "")
                {
                    MessageForm errorForm = new MessageForm("Please enter titles for your sets.");
                    errorForm.Show();
                }
                else
                {
                    Querey insert = new Querey();
                    using (insert.connection = insert.NewConn)
                    {
                        using (insert.command = insert.NewComm)
                        {
                            insert.command.CommandText = "INSERT INTO Sets (OwnerId, SetName, Title1, Title2) VALUES (@owner, @name, @title1, @title2);";
                            insert.AddParameter("@owner", _userId);
                            insert.AddParameter("@name", SetName.Text);
                            insert.AddParameter("@title1", Title1.Text);
                            insert.AddParameter("@title2", Title2.Text);
                            if (insert.connection.State != ConnectionState.Open)
                            {
                                insert.connection.Open();
                            }
                            insert.command.ExecuteNonQuery();
                            insert.Close();
                        }
                    }

                    getSetId = new Querey();
                    int setId;
                    using (getSetId.connection = getSetId.NewConn)
                    {
                        using (getSetId.command = getSetId.NewComm)
                        {
                            SetIdCommand(getSetId);
                            using (getSetId.reader = getSetId.command.ExecuteReader())
                            {
                                getSetId.reader.Read();
                                setId = getSetId.reader.GetInt32(0);
                            }
                        }
                    }

                    insert = new Querey();
                    using (insert.connection = insert.NewConn)
                    {
                        int count = 0;
                        while (count < 64 && _data[count, 0] != null)
                        {
                            using (insert.command = insert.NewComm)
                            {
                                insert.command.CommandText = "INSERT INTO Flashcards (SetId, Text1, Text2) VALUES (@setId, @text1, @text2);";
                                insert.AddParameter("@setId", setId);
                                insert.AddParameter("@text1", _data[count, 0]);
                                insert.AddParameter("@text2", _data[count, 1]);
                                insert.command.ExecuteNonQuery();
                                count++;
                            }
                        }

                        string[] tables = { "Permissions", "SetProgress" }; // done this way to allow easier error checking and make queries more readable
                        for (int i = 0; i < tables.Length; i++)
                        {
                            using (insert.command = insert.NewComm)
                            {
                                if (_student != null)
                                {
                                    insert.command.CommandText = "INSERT INTO " + tables[i] + " (UserId, SetId) SELECT Users.Id, Sets.Id FROM Users, Sets, Friends WHERE Student1 = @id AND Sets.Id = @setId AND Sets.OwnerId = Student1 AND Users.Id = Student2 OR Student2 = @id AND Sets.Id = @setId AND Sets.OwnerId = Student2 AND Users.Id = Student1;";
                                }
                                else
                                {
                                    insert.command.CommandText = "INSERT INTO " + tables[i] + " (UserId, SetId) SELECT Users.Id, Sets.Id FROM Users, Sets, TeacherStudent WHERE TeacherId = @id AND Sets.Id = @setId AND Sets.OwnerId = TeacherId AND Users.Id = StudentId;";
                                }
                                insert.AddParameter("@id", _userId);
                                insert.AddParameter("@setId", setId);
                                insert.command.ExecuteNonQuery();
                                new MessageForm("inserted").Show();
                            }
                        }

                        using (insert.command = insert.NewComm)
                        {
                            insert.command.CommandText = "INSERT INTO SetProgress (UserId, SetId) SELECT OwnerId, Id FROM Sets WHERE id = @setId;";
                            insert.AddParameter("setId", setId);
                            insert.command.ExecuteNonQuery();
                        }

                        using (insert.command = insert.NewComm)
                        {
                            double initialStrength = 2.10;
                            insert.command.CommandText = "UPDATE SetProgress SET LastReview = @now, Strength = @strength WHERE SetId = @set;";
                            insert.AddParameter("@now", DateTime.Now.ToString());
                            insert.AddParameter("@strength", initialStrength);
                            insert.AddParameter("@set", setId);

                            insert.command.ExecuteNonQuery();
                        }
                    }

                    GDrive drive = new GDrive();
                    drive.UploadDatabase();

                    ConfirmMsg.Enabled = false;
                    OpenFile.Reset();
                    SetName.Clear();
                    Title1.Clear();
                    Title2.Clear();
                    OpenFile.Reset();
                    FileName.Text = "";
                }
            }
            else
            {
                MessageForm errorForm = new MessageForm("This set name has already been used. Please choose a different one.");
                errorForm.Show();
            }
        }