public static Course Find(int id) { SqlConnection conn = DB.Connection(); SqlDataReader rdr = null; conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM courses WHERE id = @CourseId;", conn); SqlParameter courseIdParameter = new SqlParameter(); courseIdParameter.ParameterName = "@CourseId"; courseIdParameter.Value = id.ToString(); cmd.Parameters.Add(courseIdParameter); rdr = cmd.ExecuteReader(); int foundCourseId = 0; string foundCourseDescription = null; string foundCourseNumber = null; while(rdr.Read()) { foundCourseId = rdr.GetInt32(0); foundCourseDescription = rdr.GetString(1); foundCourseNumber = rdr.GetString(2); } Course foundCourse = new Course(foundCourseDescription, foundCourseNumber, foundCourseId); if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return foundCourse; }
public void Test_Equal_ReturnsTrueForSameName() { //Arrange, Act Course firstCourse = new Course("History" , "HIST101"); Course secondCourse = new Course("History" , "HIST101"); //Assert Assert.Equal(firstCourse, secondCourse); }
public void Test_Find_FindsCourseInDatabase() { //Arrange Course testCourse = new Course("History" , "HIST101"); testCourse.Save(); //Act Course foundCourse = Course.Find(testCourse.GetId()); //Assert Assert.Equal(testCourse, foundCourse); }
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()]; }; }
public static List<Course> GetAll() { List<Course> allCourses = new List<Course>{}; SqlConnection conn = DB.Connection(); SqlDataReader rdr = null; conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM courses;", conn); rdr = cmd.ExecuteReader(); while(rdr.Read()) { int courseId = rdr.GetInt32(0); string courseName = rdr.GetString(1); string courseNumber = rdr.GetString(2); Course newCourse = new Course(courseName, courseNumber, courseId); allCourses.Add(newCourse); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return allCourses; }
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"]; // }; }
// [Fact] // public void AddStudent_AddsStudentToCourse_studentList() // { // Course testCourse = new Course("History", "HIST100"); // testCourse.Save(); // // Student testStudent = new Student("Annie", "3/2/2014"); // testStudent.Save(); // // Student testStudent2 = new Student("Bryant", "4/2/2013"); // 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); // } // // [Fact] // public void Update_UpdateCourseNameAndNumber_NameAndNumberUpdatedInDatabase() // { // Course testCourse = new Course("History", "HIST100"); // testCourse.Save(); // // testCourse.Update("Japanese", "JAPN100"); // Course result = Course.GetAll()[0]; // // Assert.Equal(result, testCourse); // } // [Fact] // public void Test_Delete_DeletesCourseAssociationsFromDatabase() // { // Student testStudent = new Student("Mow the lawn"); // testStudent.Save(); // // string testName = "Home stuff"; // Course testCourse = new Course(testName); // testCourse.Save(); // // testCourse.AddStudent(testStudent); // testCourse.Delete(); // // List<Course> resultStudentCategories = testStudent.GetCategories(); // List<Course> testStudentCategories = new List<Course> {}; // // Assert.Equal(testStudentCategories, resultStudentCategories); // } public void Dispose() { // Student.DeleteAll(); Course.DeleteAll(); }
public void GetAll_DatabaseEmptyAtFirst_0() { int result = Course.GetAll().Count; Assert.Equal(0, result); }
public void Dispose() { Course.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 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]); }; }
public void AddCourse(Course newCourse) { 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 = newCourse.GetId(); cmd.Parameters.Add(courseIdParameter); SqlParameter studentIdParameter = new SqlParameter(); studentIdParameter.ParameterName = "@StudentId"; studentIdParameter.Value = this.GetId(); cmd.Parameters.Add(studentIdParameter); cmd.ExecuteNonQuery(); if (conn != null) { conn.Close(); } }
public void Test_Save_SavesCourseToDatabase() { //Arrange Course testCourse = new Course("History" , "HIST101"); testCourse.Save(); //Act List<Course> result = Course.GetAll(); List<Course> testList = new List<Course>{testCourse}; //Assert Assert.Equal(testList, result); }
public void Test_Save_AssignsIdToCourseObject() { //Arrange Course testCourse = new Course("History" , "HIST101"); testCourse.Save(); //Act Course savedCourse = Course.GetAll()[0]; int result = savedCourse.GetId(); int testId = testCourse.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); }
public List<Course> GetCourses() { SqlConnection conn = DB.Connection(); SqlDataReader rdr = null; conn.Open(); SqlCommand cmd = new SqlCommand("SELECT courses.* FROM students JOIN courses_students ON(students.id = courses_students.student_id) JOIN courses ON(courses_students.course_id = courses.id) WHERE students.id = @StudentId;", conn); SqlParameter studentIdParameter = new SqlParameter(); studentIdParameter.ParameterName = "@StudentId"; studentIdParameter.Value = this.GetId(); cmd.Parameters.Add(studentIdParameter); rdr = cmd.ExecuteReader(); List<Course> courses = new List<Course> {}; while (rdr.Read()) { int thisCourseId = rdr.GetInt32(0); string courseName = rdr.GetString(1); string courseNumber = rdr.GetString(2); Course foundCourse = new Course(courseName, courseNumber, thisCourseId); courses.Add(foundCourse); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return courses; }
public HomeModule() { Get["/"] = _ => { return(View["index.cshtml"]); }; Get["/students"] = _ => { List <Student> allStudents = Student.GetAll(); return(View["students.cshtml", allStudents]); }; Post["/students"] = _ => { var newStudent = new Student(Request.Form["student"]); newStudent.Save(); var allStudents = Student.GetAll(); return(View ["students.cshtml", allStudents]); }; Get["/student/{studentId}"] = parameters => { Dictionary <string, object> model = new Dictionary <string, object>(); Student newStudent = Student.Find(parameters.studentId); List <Course> studentCourses = newStudent.GetCourses(); model.Add("student", newStudent); model.Add("all_courses", Course.GetAll()); model.Add("courses", studentCourses); return(View["student.cshtml", model]); }; Get["/courses"] = _ => { List <Course> allCourses = Course.GetAll(); return(View["courses.cshtml", allCourses]); }; Post["/courses"] = _ => { var newCourse = new Course(Request.Form["course"]); newCourse.Save(); var allCourses = Course.GetAll(); return(View ["courses.cshtml", allCourses]); }; Get["/course/{courseId}"] = parameters => { Dictionary <string, object> model = new Dictionary <string, object>(); Course newCourse = Course.Find(parameters.courseId); List <Student> courseStudents = newCourse.GetStudents(); model.Add("course", newCourse); model.Add("all_students", Student.GetAll()); model.Add("students", courseStudents); return(View["course.cshtml", model]); }; Get["/student/course_added"] = _ => { return(View["course_added.cshtml"]); }; Post["/student/course_added"] = _ => { Course course = Course.Find(Request.Form["course-list"]); Student student = Student.Find(Request.Form["student"]); student.AddCourse(course); return(View["course_added.cshtml"]); }; Post["/course/student_added"] = _ => { Course course = Course.Find(Request.Form["course-list"]); Student student = Student.Find(Request.Form["student"]); student.AddCourse(course); return(View["student_added.cshtml"]); }; }