示例#1
0
        public void TestCreateUpdateDeleteStudents()
        {
            //DBConnection.ConnectionString = "Server=(local)\\sqlexpress;Database=Students;Trusted_Connection=True;";
            //DBConnection.Type = DBType.SQLServer;
            DBConnection.Type             = DBType.MySQL;
            DBConnection.ConnectionString = "Server=localhost;Port=3306;Uid=root;Pwd=system32;Database=Students;CheckParameters=false;";

            var newStudent = new Student("Superman", "Male", StudentType.High);
            var result     = StudentMapper.Insert(newStudent);

            Assert.AreEqual(true, result);

            var savedStudent = StudentMapper.GetById(newStudent.Id);

            Assert.AreEqual("Superman", savedStudent.Name);

            savedStudent.Name = "Clark Kent";
            StudentMapper.Update(savedStudent);
            var updatedStudent = StudentMapper.GetById(savedStudent.Id);

            Assert.AreEqual("Clark Kent", updatedStudent.Name);

            StudentMapper.Hide(updatedStudent.Id);
            var hiddenStudent = StudentMapper.GetById(updatedStudent.Id);

            Assert.AreEqual(false, hiddenStudent.Enabled);

            StudentMapper.Delete(hiddenStudent.Id);
            var deleteStudent = StudentMapper.GetById(hiddenStudent.Id);

            Assert.AreEqual(null, deleteStudent);
        }
示例#2
0
        public OperationResponse AddNew(Student student)
        {
            var response = new OperationResponse();

            try
            {
                response.HasError = !StudentMapper.Insert(student);
            }
            catch (Exception ex) {
                response.HasError = true;
                response.Message  = ex.ToString();
            }

            return(response);
        }
示例#3
0
 private void buttonRegister_Click(object sender, EventArgs e)
 {
     if (!labelRegisterName.Visible) //First button click shows the rest of the fields that need to be filled in
     {
         labelRegisterName.Visible      = true;
         labelRegisterSurname.Visible   = true;
         textBoxRegisterName.Visible    = true;
         textBoxRegisterSurname.Visible = true;
     }
     else //After the rest of the fields are visible, the register button tries to register the user in the DB
     {
         if (!String.IsNullOrEmpty(textBoxUsername.Text) && !String.IsNullOrEmpty(textBoxPassword.Text) && !String.IsNullOrEmpty(textBoxRegisterName.Text) && !String.IsNullOrEmpty(textBoxRegisterSurname.Text))
         {
             Student newStudent = new Student(textBoxUsername.Text, textBoxRegisterName.Text, textBoxRegisterSurname.Text);
             if (StudentMapper.Get(newStudent.Username) == null) //If the student doesn't already exist
             {
                 if (StudentMapper.Insert(newStudent, textBoxPassword.Text))
                 {
                     if (MessageBox.Show("Ο χρήστης εγγράφηκε επιτυχώς!", "Επιτυχής Εγγραφή!", MessageBoxButtons.OK) == DialogResult.OK)
                     {
                         //auto login after register
                         buttonLogin.PerformClick();
                     }
                 }
                 else
                 {
                     MessageBox.Show("Υπήρξε ένα σφάλμα κατά την δημιουργία του χρήστη!", "Σφάλμα", MessageBoxButtons.OK);
                 }
             }
             else
             {
                 MessageBox.Show("Υπάρχει ήδη ένας χρήστης με αυτό το όνομα χρήστη!", "Σφάλμα", MessageBoxButtons.OK);
             }
         }
         else
         {
             MessageBox.Show("Παρακαλούμε συμπληρώστε όλα τα παραπάνω πεδιά πριν συνεχίσετε!", "Σφάλμα", MessageBoxButtons.OK);
         }
     }
 }
示例#4
0
        /// <summary>
        /// Reads valid data to create a new student and save it to the database
        /// </summary>
        private static void CreateStudent()
        {
            StudentType type      = StudentType.Kinder;
            bool        validType = false;

            Console.WriteLine("Specify the student type (K)inder, (E)lementary, (H)igh, (U)niversity; introducing the letter enclosed in parenthesis and hitting enter: ");
            string answer = Console.ReadLine();

            if (!string.IsNullOrEmpty(answer))
            {
                answer = answer[0].ToString().ToUpper();
                switch (answer)
                {
                case "K":
                    validType = true;
                    break;

                case "E":
                    validType = true;
                    type      = StudentType.Elementary;
                    break;

                case "H":
                    validType = true;
                    type      = StudentType.High;
                    break;

                case "U":
                    validType = true;
                    type      = StudentType.University;
                    break;
                }
            }

            string name = string.Empty;

            Console.WriteLine("Please introduce the student's name, it must no be empty: ");
            name = Console.ReadLine();

            Console.WriteLine("Specify the student gender (M)ale, (F)emale; introducing the letter enclosed in parenthesis and hitting enter: ");

            string gender = string.Empty;

            gender = Console.ReadLine();
            if (!string.IsNullOrEmpty(gender))
            {
                gender = gender.Trim();
                if (gender.Length > 0)
                {
                    gender = gender[0].ToString().ToUpper();
                }
            }

            if (validType && !string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(gender) && (gender == "M" || gender == "F"))
            {
                Student student = new Student();
                student.Type      = type;
                student.Name      = name;
                student.Gender    = gender == "M" ? "Male" : "Female";
                student.Enabled   = true;
                student.UpdatedOn = DateTime.Now;
                if (!StudentMapper.Exists(student))
                {
                    if (StudentMapper.Insert(student))
                    {
                        ShowContinueMessage("The student was added successfully.");
                    }
                    else
                    {
                        ShowContinueMessage("We found and error adding the student please review logs and try again.");
                    }
                }
                else
                {
                    ShowContinueMessage("There is already a student with the same data added, we don't allow duplicates. You can search it for edit if you want.");
                }
            }
            else
            {
                ShowContinueMessage("There is an error with your data. Please verify that the Type is K for Kinder, E for Elementary, H for High, U for University, The name is not empty and Gender is M for Male or F for Female.");
            }
        }