예제 #1
0
 public void T4_Find_inDataBase()
 {
     Course firstCourse = new Course("Biology", 101);
       firstCourse.Save();
       Course resultCourse = Course.Find(firstCourse.GetId());
       Assert.Equal(firstCourse,resultCourse);
 }
예제 #2
0
        public void T2_ObjectandObject_returnEqual()
        {
            Course firstCourse = new Course("Biology", 101);
              Course secondCourse = new Course("Biology", 101);

              Assert.Equal(firstCourse, secondCourse);
        }
예제 #3
0
        public void T3_SaveInDatabase_True()
        {
            Course firstCourse = new Course("Biology", 101);
            firstCourse.Save();
            List<Course> newList = Course.GetAll();
            List<Course> testCourse = new List<Course>{firstCourse};

            Assert.Equal(testCourse, newList);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        public static Course Find(int id)
        {
            SqlConnection conn = DB.Connection();
              conn.Open();

              SqlCommand cmd = new SqlCommand("SELECT * FROM courses WHERE id = @NewId;",conn);

              SqlParameter courseIdParameter = new SqlParameter();
              courseIdParameter.ParameterName = "@NewId";
              courseIdParameter.Value=id;
              cmd.Parameters.Add(courseIdParameter);
              SqlDataReader rdr = cmd.ExecuteReader();

              int foundId=0;
              string foundName=null;
              int foundNumber=0;

              while(rdr.Read())
              {
            foundId=rdr.GetInt32(0);
            foundName=rdr.GetString(1);
            foundNumber=rdr.GetInt32(2);

              }
              Course foundCourse = new Course(foundName,foundNumber,foundId);

              if(rdr != null)
              {
            rdr.Close();
              }

              if(conn != null)
              {
            conn.Close();
              }
              return foundCourse;
        }
예제 #7
0
        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];
              };
        }
예제 #8
0
        public static List<Course> GetAll()
        {
            List<Course> allCourses = new List<Course>{};

              SqlConnection conn = DB.Connection();
              conn.Open();

              SqlCommand cmd = new SqlCommand("SELECT * FROM courses;",conn);

              SqlDataReader rdr = cmd.ExecuteReader();

              int courseId=0;
              string courseName=null;
              int courseNumber=0;

              while(rdr.Read())
              {
            courseId = rdr.GetInt32(0);
            courseName=rdr.GetString(1);
            courseNumber=rdr.GetInt32(2);

            Course newCourse = new Course(courseName,courseNumber,courseId);
            allCourses.Add(newCourse);
              }

              if( rdr!=null)
              {
            rdr.Close();
              }

              if(conn != null)
              {
            conn.Close();
              }

              return allCourses;
        }
예제 #9
0
        public List<Course> GetCourses()
        {
            SqlConnection conn = DB.Connection();
            conn.Open();

            SqlCommand cmd = new SqlCommand("SELECT courses.* FROM students JOIN students_courses ON (students.id=students_courses.student_id) JOIN courses ON (students_courses.id=courses.id) WHERE students.id=@StudentId;",conn);

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

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

            List<Course> foundCourses= new List<Course>{};

            while(rdr.Read())
            {
              int courseId=rdr.GetInt32(0);
              string courseName=rdr.GetString(1);
              int courseNumber=rdr.GetInt32(2);
              Course foundCourse = new Course(courseName,courseNumber,courseId);
              foundCourses.Add(foundCourse);
            }

            if(rdr != null)
            {
              rdr.Close();
            }

            if(conn !=null)
            {
              conn.Close();
            }
            return foundCourses;
        }
예제 #10
0
        public void AddCourses(Course newCourse)
        {
            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= this.GetId();

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

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

            cmd.ExecuteNonQuery();

            if(conn != null)
            {
              conn.Close();
            }
        }