public void Test_Find_FindsCourseInDatabase() { Course testCourse = new Course("Intro to programming",975,"Epicodus","5 weeks"); testCourse.Save(); Course foundCourse = Course.Find(testCourse.GetId()); Assert.Equal(testCourse, foundCourse); }
public void Test_Save_SavesToDatabase() { Course testCourse = new Course("Intro to programming",975,"Epicodus","5 weeks"); testCourse.Save(); List<Course> result = Course.GetAll(); List<Course> testList = new List<Course>{testCourse}; Assert.Equal(testList, result); }
public void Test_Save_AssignsIdToObject() { Course testCourse=new Course("Intro to programming",975,"Epicodus","5 weeks"); testCourse.Save(); Course savedCourse = Course.GetAll()[0]; int result=savedCourse.GetId(); int testId=testCourse.GetId(); Assert.Equal(testId,result); }
public HomeModule() { Get["/"] = _ => { return View["index.cshtml"]; }; Get["/all"] = _ => { List<Course> allCourses=Course.GetAll(); return View["view_all.cshtml", allCourses]; }; Post["/results"] = _ => { Course newCourse = new Course( Request.Form["new-course"], Request.Form["new-cost"], Request.Form["new-company"], Request.Form["new-duration"]); newCourse.Save(); List<Course> allCourses=Course.GetAll(); return View["results.cshtml", allCourses]; }; Get["/find"] = _ => { return View["find.cshtml"]; }; Get["/delete"] = _ => { return View["delete.cshtml"]; }; Get["/findForm"] = _ => { int idName = int.Parse(Request.Query["old-id"]); Course newCourse=Course.Find(idName); return View["find-result.cshtml",newCourse]; }; Post["/delete"] = _ => { Course.DeleteAll(); return View["confirm.cshtml"]; }; }
public static Course Find(int id) { SqlConnection conn = DB.Connection(); 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); SqlDataReader rdr = cmd.ExecuteReader(); int foundCourseId=0; string foundCourseName= null; int foundCourseCost= 0; string foundCourseCompany= null; string foundCourseDuration= null; while(rdr.Read()) { foundCourseId=rdr.GetInt32(0); foundCourseName=rdr.GetString(1); foundCourseCost=rdr.GetInt32(2); foundCourseCompany=rdr.GetString(3); foundCourseDuration=rdr.GetString(4); } Course foundCourse = new Course(foundCourseName, foundCourseCost,foundCourseCompany, foundCourseDuration,foundCourseId); if(rdr != null) { rdr.Close(); } if(conn !=null) { conn.Close(); } return foundCourse; }
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(); while(rdr.Read()) { int courseId = rdr.GetInt32(0); string courseName = rdr.GetString(1); int courseCost = rdr.GetInt32(2); string courseComapny=rdr.GetString(3); string courseDuration=rdr.GetString(4); Course newCourse = new Course(courseName, courseCost,courseComapny,courseDuration,courseId); allCourses.Add(newCourse); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return allCourses; }
public void Test_Equal_ReturnsTrueIfDescriptionsAreTheSame() { Course firstCourse = new Course("Intro to programming",975,"Epicodus","5 weeks"); Course secondCourse = new Course("Intro to programming",975,"Epicodus","5 weeks"); Assert.Equal(firstCourse,secondCourse); }