private void SaveCourseButton_Click(object sender, EventArgs e) { if (ValidData()) { if (course == null) { OnlineCourseModel c = new OnlineCourseModel(); c.Title = CourseTitleValue.Text; c.CourseLink = CheckForFullLinkPath(CourseLinkValue.Text); GlobalConfig.Connection.CreateOnlineCourseModel(c, selectedCategories); } else if (course != null) { course.Title = CourseTitleValue.Text; course.CourseLink = CourseLinkValue.Text; GlobalConfig.Connection.UpdateOnlineCourseModel(course, selectedCategories); } callingForm.CourseComplete(); this.Close(); } }
public void CreateOnlineCourseModel(OnlineCourseModel model, List <CategoryModel> currentCategories) { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db))) { var p = new DynamicParameters(); p.Add("@CourseName", model.Title); p.Add("@CourseLink", model.CourseLink); p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); connection.Execute("dbo.spOnlineCourses_Insert", p, commandType: CommandType.StoredProcedure); model.ID = p.Get <int>("@id"); foreach (CategoryModel cm in currentCategories) { p = new DynamicParameters(); p.Add("@OnlineCourseid", model.ID); p.Add("@Categoryid", cm.ID); connection.Execute("dbo.spOnlineCourseCategories_Insert", p, commandType: CommandType.StoredProcedure); } } }
private void CoursesListBox_SelectedIndexChanged(object sender, EventArgs e) { selectedCourse = (OnlineCourseModel)CoursesListBox.SelectedItem; if (selectedCourse != null) { RefreshSelectedCourseLink(); } }
public OnlineCourseForm(IOnlineCourseRequester caller, OnlineCourseModel model) { course = model; callingForm = caller; InitializeComponent(); this.Text = $"Edit { course.Title }"; CourseTitleValue.Text = course.Title; CourseLinkValue.Text = course.CourseLink; availableCategories.Remove(availableCategories.Where(x => x.ID == 1).First()); WireUpExistingCategories(); WireUpLists(); }
public void UpdateOnlineCourseModel(OnlineCourseModel model, List <CategoryModel> currentCategories) { //TODO - }
public void UpdateOnlineCourseModel(OnlineCourseModel model, List <CategoryModel> currentCategories) { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db))) { var p = new DynamicParameters(); p.Add("@CourseName", model.Title); p.Add("@CourseLink", model.CourseLink); p.Add("@id", model.ID); connection.Execute("dbo.spOnlineCourses_Update", p, commandType: CommandType.StoredProcedure); //call sp to get list of stored Categories for the course List <CategoryModel> storedCategories = new List <CategoryModel>(); p = new DynamicParameters(); p.Add("@OnlineCourseid", model.ID); storedCategories = connection.Query <CategoryModel>("dbo.spCategories_GetByCourse", p, commandType: CommandType.StoredProcedure).ToList(); //TODO - refactor //currentCategories list and storedCategories are both null then do nothing - nothing to update if (currentCategories != null && storedCategories != null) { //do checks foreach (CategoryModel c in currentCategories) { //if it already exists in the database remove it from further actions if (storedCategories.Exists(x => x.ID == c.ID)) { storedCategories.RemoveAt(storedCategories.FindIndex(x => x.ID == c.ID)); } else //it doesn't already exist in the database so add it { p = new DynamicParameters(); p.Add("@OnlineCourseid", model.ID); p.Add("@Categoryid", c.ID); connection.Execute("dbo.spOnlineCourseCategories_Insert", p, commandType: CommandType.StoredProcedure); } } //if there are still categories that were on the database, but aren't in your list, remove them from database if (storedCategories.Count > 0) { foreach (CategoryModel storedC in storedCategories) { p = new DynamicParameters(); p.Add("@OnlineCourseid", model.ID); p.Add("@Categoryid", storedC.ID); connection.Execute("dbo.spOnlineCourseCategories_Remove", p, commandType: CommandType.StoredProcedure); } } } else if (currentCategories == null && storedCategories != null) { //remove all from db in stored categories since nothing is assigned to the model categories foreach (CategoryModel c in storedCategories) { p = new DynamicParameters(); p.Add("@OnlineCourseid", model.ID); p.Add("@Categoryid", c.ID); connection.Execute("dbo.spOnlineCourseCategories_Remove", p, commandType: CommandType.StoredProcedure); } } else if (currentCategories != null && storedCategories == null) { //add all from model.categories to db since nothing was previously saved in db foreach (CategoryModel c in currentCategories) { p = new DynamicParameters(); p.Add("@OnlineCourseid", model.ID); p.Add("@Categoryid", c.ID); connection.Execute("dbo.spOnlineCourseCategories_Insert", p, commandType: CommandType.StoredProcedure); } } } }