public void T3_ObjectandObject_returnEqual_id()
 {
     DateTime fakeTime=new DateTime(2016,08,02);
      Student firstStudent= new Student("Steve",fakeTime,1);
      Student secondStudent= new Student("Steve",fakeTime,1);
      Assert.Equal(firstStudent,secondStudent);
 }
        public static Student Find(int id)
        {
            SqlConnection conn = DB.Connection();
            conn.Open();

            SqlCommand cmd = new SqlCommand("SELECT * FROM students WHERE id = @StudentID;", conn);
            SqlParameter studentIdParameter = new SqlParameter();
            studentIdParameter.ParameterName = "@StudentID";
            studentIdParameter.Value = id.ToString();
            cmd.Parameters.Add(studentIdParameter);
            SqlDataReader rdr = cmd.ExecuteReader();

            int foundStudentId = 0;
            string foundNameStudent = null;
            DateTime foundDateStudent = new DateTime(2000,01,01);

            while (rdr.Read())
            {
              foundStudentId = rdr.GetInt32(0);
              foundNameStudent = rdr.GetString(1);
              foundDateStudent = rdr.GetDateTime(2);
            }
            Student foundStudent = new Student(foundNameStudent, foundDateStudent, foundStudentId);

            if (rdr != null)
            {
              rdr.Close();
            }
            if (conn != null)
            {
              conn.Close();
            }
            return foundStudent;
        }
        public void T5_FindStudentInDatabase()
        {
            DateTime fakeTime=new DateTime(2016,08,02);
             Student newStudent = new Student("Steve", fakeTime);
             newStudent.Save();

             Student foundStudent = Student.Find(newStudent.GetId());
             Assert.Equal(newStudent, foundStudent);
        }
        public void T4_SaveToDatabase_True()
        {
            DateTime fakeTime=new DateTime(2016,08,02);
             Student newStudent = new Student("Steve", fakeTime);
             newStudent.Save();

             List<Student>allStudent = Student.GetAll();
             List<Student>testStudent = new List <Student>{newStudent};

             Assert.Equal(testStudent, allStudent);
        }
        public void T5_AddStudentsToCourse()
        {
            Course firstCourse = new Course("Biology", 101);
              Course secondCourse = new Course("Physics", 102);
              firstCourse.Save();
              secondCourse.Save();

              DateTime fakeTime=new DateTime(2016,08,02);
              Student testStudent = new Student("Steve", fakeTime);
              testStudent.Save();

              testStudent.AddCourses(firstCourse);
              testStudent.AddCourses(secondCourse);
              List<Course> temp = Course.GetAll();

              List<Course> resultCourse = testStudent.GetCourses();
              List<Course> testCourses=new List<Course>{firstCourse,secondCourse};
              Assert.Equal(testCourses,temp);
        }
        public void T6_AddStudentTo_Course_True()
        {
            Course testCourse = new Course("Biology", 101);
             testCourse.Save();

             DateTime fakeTime=new DateTime(2016,08,02);
             Student testStudent = new Student("Steve", fakeTime);
             testStudent.Save();

             Student testStudent2 = new Student("Mike", fakeTime);
             testStudent2.Save();

             testCourse.AddStudents(testStudent);
             testCourse.AddStudents(testStudent2);
             List<Student> allStudent= Student.GetAll();
             List<Student> result = testCourse.GetStudents();
             List<Student> testList = new List<Student>{testStudent,testStudent2};

             Assert.Equal(testList, result);
        }
        public HomeModule()
        {
            Get["/"] = _ =>{
            List<Student> allStudents = Student.GetAll();
            List<Course> allCourses = Course.GetAll();

            Dictionary<string,object> model = new Dictionary<string,object>{};
            model.Add("student",allStudents);
            model.Add("course",allCourses);
            return View["index.cshtml",model];
              };

              Post["/add/new/student"]=_=>{
            Student newStudent = new Student(Request.Form["new-name"], Request.Form["new-date"]);
            newStudent.Save();

            List<Student> allStudents = Student.GetAll();
            List<Course> allCourses = Course.GetAll();

            Dictionary<string,object> model = new Dictionary<string,object>{};
            model.Add("student",allStudents);
            model.Add("course",allCourses);
            return View["index.cshtml",model];
              };
              Post["/add/new/course"]=_=>{
            Course newCourse = new Course(Request.Form["course-name"], Request.Form["course-number"]);
            newCourse.Save();

            List<Student> allStudents = Student.GetAll();
            List<Course> allCourses = Course.GetAll();

            Dictionary<string,object> model = new Dictionary<string,object>{};
            model.Add("student",allStudents);
            model.Add("course",allCourses);
            return View["index.cshtml",model];
              };
        }
Example #8
0
        public List<Student> GetStudents()
        {
            SqlConnection conn = DB.Connection();
              conn.Open();

              SqlCommand cmd = new SqlCommand("SELECT students.* FROM courses JOIN students_courses ON (courses.id = students_courses.course_id) JOIN students ON (students_courses.student_id = students.id) WHERE courses.id = @CourseId;", conn);
              SqlParameter CourseIdParameter = new SqlParameter();
              CourseIdParameter.ParameterName = "@CourseId";
              CourseIdParameter.Value = this.GetId().ToString();

              cmd.Parameters.Add(CourseIdParameter);
              SqlDataReader rdr = cmd.ExecuteReader();

              List<Student> students = new List<Student>{};

              while (rdr.Read())
              {
            int studentId = rdr.GetInt32(0);
            string studentName = rdr.GetString(1);
            DateTime studentDate = rdr.GetDateTime(2);
            Student newStudent = new Student(studentName, studentDate, studentId);
            students.Add(newStudent);
              }
              if (rdr != null)
              {
            rdr.Close();
              }
              if (conn != null)
              {
            conn.Close();
              }
              return students;
        }
Example #9
0
        public void AddStudents(Student newStudent)
        {
            SqlConnection conn = DB.Connection();
              conn.Open();

              SqlCommand cmd = new SqlCommand("INSERT INTO students_courses (student_id, course_id) VALUES (@StudentId,@CourseId)",conn);

              SqlParameter studentIdParameter = new SqlParameter();
              studentIdParameter.ParameterName= "@StudentId";
              studentIdParameter.Value= newStudent.GetId();

              SqlParameter courseIdParameter = new SqlParameter();
              courseIdParameter.ParameterName= "@CourseId";
              courseIdParameter.Value= this.GetId();

              cmd.Parameters.Add(studentIdParameter);
              cmd.Parameters.Add(courseIdParameter);

              cmd.ExecuteNonQuery();

              if(conn != null)
              {
            conn.Close();
              }
        }
Example #10
0
        public static List<Student> GetAll()
        {
            SqlConnection conn = DB.Connection();
            conn.Open();

            SqlCommand cmd = new SqlCommand("SELECT * FROM students;", conn);
            SqlDataReader rdr = cmd.ExecuteReader();

            List<Student> AllStudents = new List<Student>{};

            while (rdr.Read())
            {
              int studentId = rdr.GetInt32(0);
              string studentName = rdr.GetString(1);
              DateTime studentDate = rdr.GetDateTime(2);
              Student newStudent = new Student(studentName,studentDate, studentId);
              AllStudents.Add(newStudent);
            }
            if (rdr != null)
            {
              rdr.Close();
            }
            if (conn != null)
            {
              conn.Close();
            }
            return AllStudents;
        }