Esempio n. 1
0
        public static Student Find(int id)
        {
            SqlConnection conn = DB.Connection();
            SqlDataReader rdr = null;
            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);
            rdr = cmd.ExecuteReader();

            int foundStudentId = 0;
            string foundStudentName = null;
            DateTime foundStudentEnrollmentDate = new DateTime(0);

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

            if (rdr != null)
            {
              rdr.Close();
            }
            if (conn != null)
            {
              conn.Close();
            }
            return foundStudent;
        }
        public void Test_Equal_ReturnsTrueIfDescriptionsAreTheSame()
        {
            //Arrange, Act
              Student firstStudent = new Student("Mow the lawn", new DateTime(2015, 1, 18));
              Student secondStudent = new Student("Mow the lawn", new DateTime(2015, 1, 18));

              //Assert
              Assert.Equal(firstStudent, secondStudent);
        }
        public void Test_EqualOverrideTrueForSameDescription()
        {
            //Arrange, Act
              Student firstStudent = new Student("Mow the lawn", new DateTime(2015, 1, 18));
              Student secondStudent = new Student("Mow the lawn", new DateTime(2015, 1, 18));

              //Assert
              Assert.Equal(firstStudent, secondStudent);
        }
        public void Test_FindFindsStudentInDatabase()
        {
            //Arrange
              Student testStudent = new Student("Mow the lawn", new DateTime(2015, 1, 18));
              testStudent.Save();

              //Act
              Student foundStudent = Student.Find(testStudent.GetId());

              //Assert
              Assert.Equal(testStudent, foundStudent);
        }
        public void Test_AddCourse_AddsCourseToStudent()
        {
            //Arrange
              Student testStudent = new Student("Mow the lawn", new DateTime(2015, 1, 18));
              testStudent.Save();

              Course testCourse = new Course("History" , "HIST101");
              testCourse.Save();

              //Act
              testStudent.AddCourse(testCourse);

              List<Course> result = testStudent.GetCourses();
              List<Course> testList = new List<Course>{testCourse};

              //Assert
              Assert.Equal(testList, result);
        }
        public void Test_AddStudent_AddsStudentToCourse()
        {
            Course testCourse = new Course("History" , "HIST101");
              testCourse.Save();

              Student testStudent = new Student("Mow the Lawn", new DateTime(2014, 4, 21));
              testStudent.Save();

              Student testStudent2 = new Student("Water the garden", new DateTime(2014, 4, 21));
              testStudent2.Save();

              testCourse.AddStudent(testStudent);
              testCourse.AddStudent(testStudent2);

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

              Assert.Equal(testList, result);
        }
 public HomeModule()
 {
     Get["/"] =_=> View["index.cshtml", Course.GetAll()];
       Get["/course/new"] =_=> View["course_form.cshtml"];
       Post["course/added"] =_=> {
     Course newCourse = new Course(Request.Form["newName"], Request.Form["newNumber"]);
     newCourse.Save();
     return View["index.cshtml", Course.GetAll()];
       };
       Get["/course/{id}"] =parameters=> {
     Course Model = Course.Find(parameters.id);
     return View["course_detail.cshtml", Model];
       };
       Get["/student/edit"] =_=> View["student_edit.cshtml", Student.GetAll()];
       Get["/student/delete/{id}"] =parameters=> {
     Student.Delete(parameters.id);
     return View["student_edit.cshtml", Student.GetAll()];
       };
       Post["/student/new"] =_=> {
     Student newStudent = new Student(Request.Form["studentName"], Request.Form["studentEnrollment"]);
     newStudent.Save();
     return View["student_edit.cshtml", Student.GetAll()];
       };
       Post["/student/edit/name/{id}"] =parameters=> {
     Student ExistingStudent = Student.Find(parameters.id);
     ExistingStudent.SetName(Request.Form["newName"]);
     ExistingStudent.Update();
     return View["student_edit.cshtml", Student.GetAll()];
       };
       Post["/student/edit/enrollment/{id}"] =parameters=> {
     Student ExistingStudent = Student.Find(parameters.id);
     ExistingStudent.SetEnrollment(Request.Form["newEnrollment"]);
     ExistingStudent.Update();
     return View["student_edit.cshtml", Student.GetAll()];
       };
 }
Esempio n. 8
0
        public List<Student> GetStudents()
        {
            SqlConnection conn = DB.Connection();
              SqlDataReader rdr = null;
              conn.Open();

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

              rdr = cmd.ExecuteReader();

              List<Student> students = new List<Student> {};
              while(rdr.Read())
              {
            int thisStudentId = rdr.GetInt32(0);
            string studentDescription = rdr.GetString(1);
            DateTime studentDueDate = rdr.GetDateTime(2);
            Student foundStudent = new Student(studentDescription, studentDueDate, thisStudentId);
            students.Add(foundStudent);
              }

              if (rdr != null)
              {
            rdr.Close();
              }
              if (conn != null)
              {
            conn.Close();
              }
              return students;
        }
Esempio n. 9
0
        public void AddStudent(Student newStudent)
        {
            SqlConnection conn = DB.Connection();
              conn.Open();

              SqlCommand cmd = new SqlCommand("INSERT INTO courses_students (course_id, student_id) VALUES (@CourseId, @StudentId)", conn);
              SqlParameter courseIdParameter = new SqlParameter();
              courseIdParameter.ParameterName = "@CourseId";
              courseIdParameter.Value = this.GetId();
              cmd.Parameters.Add(courseIdParameter);

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

              cmd.ExecuteNonQuery();

              if (conn != null)
              {
            conn.Close();
              }
        }
Esempio n. 10
0
        public HomeModule()
        {

            Get["/"] =_=>
            {
                return View["index.cshtml"];
            };

            Get["/students"]= _ => {
                List<Student> allStudents = Student.GetAll();
                return View["students.cshtml", allStudents];
            };

            Post["/students"] = _ =>
            {
                Student newStudent = new Student(Request.Form["student-name"], Request.Form["student-enrollment"]);
                newStudent.Save();
                List<Student> allStudents = Student.GetAll();
                return View["students.cshtml", allStudents];
            };

            Get["/courses"]= _ => {
                List<Course> allCourses = Course.GetAll();
                return View["courses.cshtml", allCourses];
            };

            Post["/courses"] = _ =>
            {
                Course newCourse = new Course(Request.Form["course-name"], Request.Form["course-number"]);
                newCourse.Save();
                List<Course> allCourses = Course.GetAll();
                return View["courses.cshtml", allCourses];
            };

            Get["/departments"]= _ => {
                List<Department> allDepartments = Department.GetAll();
                return View["departments.cshtml", allDepartments];
            };

            Post["/departments"] = _ =>
            {
                Department newDepartment = new Department(Request.Form["department-major"]);
                newDepartment.Save();
                List<Department> allDepartments = Department.GetAll();
                return View["departments.cshtml", allDepartments];
            };

            Get["/departments/{id}"] = parameter =>{
                Department foundDepartment = Department.Find(parameter.id);
                List<string> courses = new List<string>{};
                List<string> students = new List<string>{};
                foreach(var course in foundDepartment.GetCourses())
                {
                    courses.Add(course.GetName());
                };
                foreach(var student in foundDepartment.GetStudents())
                {
                    students.Add(student.GetName());
                };
                Dictionary<string, List<string>> departmentDeets = new Dictionary<string, List<string>>{{"courses", courses}, {"students", students}};
                return View["department.cshtml", departmentDeets];
            };

            Get["courses/{id}"] = parameters => {
                Dictionary<string, object> model = new Dictionary<string, object>();
                Course SelectedCourse = Course.Find(parameters.id);
                List<Student> CourseStudents = SelectedCourse.GetStudents();
                List<Student> AllStudents = Student.GetAll();
                model.Add("course", SelectedCourse);
                model.Add("courseStudents", CourseStudents);
                model.Add("allStudents", AllStudents);
                return View["course.cshtml", model];
            };

            Get["students/{id}"] = parameters => {
                Dictionary<string, object> model = new Dictionary<string, object>();
                Student SelectedStudent = Student.Find(parameters.id);
                List<Student> StudentCourses = SelectedStudent.GetCompletedCourses();
                List<Student> AllStudents = Student.GetAll();
                model.Add("student", SelectedStudent);
                model.Add("studentCourses", StudentCourses);
                model.Add("allStudents", AllStudents);
                return View["student.cshtml", model];
            };



            // Post["course/add_student"] = _ => {
            //        Student student = Student.Find(Request.Form["student-id"]);
            //        Course course = Course.Find(Request.Form["course-id"]);
            //        course.AddStudent(student);
            //        return View["course.cshtml"];
            //    };

            // Post["student/add_course"] = _ => {
            //    Student student = Student.Find(Request.Form["student-id"]);
            //    Course course = Course.Find(Request.Form["course-id"]);
            //    student.AddCourse(course);
            //    return View["student.cshtml"];
            // };
        }
Esempio n. 11
0
 public void Dispose()
 {
     Course.DeleteAll();
     Student.DeleteAll();
 }
        public void Test_GetStudents_RetrievesAllCourseStudents()
        {
            Course testCourse = new Course("History" , "HIST101");
              testCourse.Save();

              Student testStudent1 = new Student("Mow the lawn", new DateTime(2014, 4, 21));
              testStudent1.Save();

              Student testStudent2 = new Student("Buy plane ticket", new DateTime(2014, 4, 21));
              testStudent2.Save();

              testCourse.AddStudent(testStudent1);
              List<Student> savedStudents = testCourse.GetStudents();
              List<Student> testList = new List<Student> {testStudent1};

              Assert.Equal(testList, savedStudents);
        }
        public void Test_Save_SavesToDatabase()
        {
            //Arrange
              Student testStudent = new Student("Mow the lawn", new DateTime(2015, 1, 18));

              //Act
              testStudent.Save();
              List<Student> result = Student.GetAll();
              List<Student> testList = new List<Student>{testStudent};

              //Assert
              Assert.Equal(testList, result);
        }
        public void Test_Save_AssignsIdToObject()
        {
            //Arrange
              Student testStudent = new Student("Mow the lawn", new DateTime(2015, 1, 18));

              //Act
              testStudent.Save();
              Student savedStudent = Student.GetAll()[0];

              int result = savedStudent.GetId();
              int testId = testStudent.GetId();

              //Assert
              Assert.Equal(testId, result);
        }
        public void Test_GetCourses_ReturnsAllStudentCourses()
        {
            //Arrange
              Student testStudent = new Student("Mow the lawn", new DateTime(2015, 1, 18));
              testStudent.Save();

              Course testCourse1 = new Course("History" , "HIST101");
              testCourse1.Save();

              Course testCourse2 = new Course("Work stuff", "Career101");
              testCourse2.Save();

              //Act
              testStudent.AddCourse(testCourse1);
              List<Course> result = testStudent.GetCourses();
              List<Course> testList = new List<Course> {testCourse1};

              //Assert
              Assert.Equal(testList, result);
        }
Esempio n. 16
0
        public static List<Student> GetAll()
        {
            List<Student> AllStudents = new List<Student>{};

            SqlConnection conn = DB.Connection();
            SqlDataReader rdr = null;
            conn.Open();

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

            while(rdr.Read())
            {
              int studentId = rdr.GetInt32(0);
              string studentName = rdr.GetString(1);
              DateTime studentEnrollmentDate = rdr.GetDateTime(2);
              Student newStudent = new Student(studentName, studentEnrollmentDate, studentId);
              AllStudents.Add(newStudent);
            }
            if (rdr != null)
            {
              rdr.Close();
            }
            if (conn != null)
            {
              conn.Close();
            }
            return AllStudents;
        }
Esempio n. 17
0
        public HomeModule()
        {
            Get["/"] = _ => {
                return(View["index.cshtml"]);
            };
            Get["/course/new"] = _ => {
                return(View["new-course-form.cshtml"]);
            };
            Post["/course/new"] = _ => {
                Course newCourse = new Course(Request.Form["courseName"], Request.Form["courseNumber"]);
                newCourse.Save();
                List <Course> allCourses = Course.GetAll();
                return(View["courses.cshtml", allCourses]);
            };
            Get["/courses"] = _ => {
                List <Course> allCourses = Course.GetAll();
                return(View["courses.cshtml", allCourses]);
            };
            Get["/student/new"] = _ => {
                return(View["new-student-form.cshtml"]);
            };
            Post["/student/new"] = _ => {
                Student newStudent = new Student(Request.Form["studentName"], Request.Form["enrollmentDate"]);
                newStudent.Save();
                List <Student> allStudents = Student.GetAll();
                return(View["students.cshtml", allStudents]);
            };
            Get["/course/{id}"] = parameters => {
                Course currentCourse = Course.Find(parameters.id);
                Dictionary <string, object> model = new Dictionary <string, object>();
                List <Student> allStudents        = Student.GetAll();
                List <Student> courseStudents     = currentCourse.GetAllStudents();
                model.Add("course", currentCourse);
                model.Add("allStudents", allStudents);
                model.Add("courseStudents", courseStudents);
                return(View["course.cshtml", model]);
            };
            Patch["/course/update/{id}"] = parameters => {
                Course currentCourse = Course.Find(parameters.id);
                currentCourse.UpdateName(Request.Form["newName"]);
                Dictionary <string, object> model = new Dictionary <string, object>();
                List <Student> allStudents        = Student.GetAll();
                List <Student> courseStudents     = currentCourse.GetAllStudents();
                model.Add("course", currentCourse);
                model.Add("allStudents", allStudents);
                model.Add("courseStudents", courseStudents);
                return(View["course.cshtml", model]);
            };
            Delete["/course/delete/{id}"] = parameters => {
                Course currentCourse = Course.Find(parameters.id);
                currentCourse.Delete();
                List <Course> allCourses = Course.GetAll();
                return(View["courses.cshtml", allCourses]);
            };
            Post["/course/add_student"] = _ => {
                Course currentCourse = Course.Find(Request.Form["courseId"]);
                currentCourse.AddStudent(Request.Form["studentId"]);

                Dictionary <string, object> model = new Dictionary <string, object>();
                List <Student> allStudents        = Student.GetAll();
                List <Student> courseStudents     = currentCourse.GetAllStudents();
                model.Add("course", currentCourse);
                model.Add("allStudents", allStudents);
                model.Add("courseStudents", courseStudents);
                return(View["course.cshtml", model]);
            };
            Get["/student/{id}"] = parameters => {
                Student currentStudent            = Student.Find(parameters.id);
                Dictionary <string, object> model = new Dictionary <string, object>();
                List <Course> allCourses          = Course.GetAll();
                List <Course> studentCourses      = currentStudent.GetAllCourses();
                model.Add("student", currentStudent);
                model.Add("allCourses", allCourses);
                model.Add("studentCourses", studentCourses);
                return(View["student.cshtml", model]);
            };
            Post["/student/add_course"] = _ => {
                Student currentStudent = Student.Find(Request.Form["studentId"]);
                currentStudent.AddCourse(Request.Form["courseId"]);
                Dictionary <string, object> model = new Dictionary <string, object>();
                List <Course> allCourses          = Course.GetAll();
                List <Course> studentCourses      = currentStudent.GetAllCourses();
                model.Add("student", currentStudent);
                model.Add("allCourses", allCourses);
                model.Add("studentCourses", studentCourses);
                return(View["student.cshtml", model]);
            };
            Delete["/student/delete/{id}"] = parameters => {
                Student currentStudent = Student.Find(parameters.id);
                currentStudent.Delete();
                List <Student> allStudents = Student.GetAll();
                return(View["students.cshtml", allStudents]);
            };
            Get["/students"] = _ => {
                List <Student> allStudents = Student.GetAll();
                return(View["students.cshtml", allStudents]);
            };
            Patch["/student/update/{id}"] = parameters => {
                Student currentStudent = Student.Find(parameters.id);
                currentStudent.UpdateName(Request.Form["newName"]);
                Dictionary <string, object> model = new Dictionary <string, object>();
                List <Course> allCourses          = Course.GetAll();
                List <Course> studentCourses      = currentStudent.GetAllCourses();
                model.Add("student", currentStudent);
                model.Add("allCourses", allCourses);
                model.Add("studentCourses", studentCourses);
                return(View["student.cshtml", model]);
            };
        }