public override void Revert_Changes(Book book, User user) { if (this.mode == MODE.GIVE) { book.Count++; book.Change_Count(); string query_select = string.Format("Select Book_User.COUNT From Book_User Where Book_User.BOOK_ID = {0} and Book_User.USER_ID = {1}", book.Book_id, this.User_id); DataTable dt = DataBaseEvents.ExecuteQuery(query_select, data_source); if (dt.Rows.Count <= 0) { return; } int current_count = int.Parse(dt.Rows[0][0].ToString()); if (current_count > 1) { current_count--; string query_update = string.Format("Update Book_User Set COUNT = {0} Where Book_User.BOOK_ID = {1} and Book_User.USER_ID = {2}", current_count, book.Book_id, this.user_id); DataBaseEvents.ExecuteNonQuery(query_update, data_source); } else { string query_delete = string.Format("Delete From Book_User Where Book_User.BOOK_ID = {0} and Book_User.USER_ID = {1}", book.Book_id, this.user_id); DataBaseEvents.ExecuteNonQuery(query_delete, data_source); } // Popularity Author aut = main_page.Main_author_list.Find_Author_By_ID(book.Author_id); Category ctg = main_page.Main_category_list.Find_Category_By_ID(book.Category_id); book.Popularity_score -= 5; aut.Popularity_score -= 5; ctg.Popularity_score -= 5; book.Change_Popularity_Score(); aut.Change_Popularity_Score(); ctg.Change_Popularity_Score(); } else { book.Count--; book.Change_Count(); string query_select = string.Format("Select Book_User.COUNT From Book_User Where Book_User.BOOK_ID = {0} and Book_User.USER_ID = {1}", book.Book_id, this.User_id); DataTable dt = DataBaseEvents.ExecuteQuery(query_select, data_source); if (dt.Rows.Count <= 0) { string query_delete = string.Format("Insert Into Book_User(BOOK_ID, USER_ID, COUNT) Values({0},{1},{2})", book.Book_id, this.user_id, 1); DataBaseEvents.ExecuteNonQuery(query_delete, data_source); } else { int current_count = int.Parse(dt.Rows[0][0].ToString()); if (current_count > 0) { current_count++; string query_update = string.Format("Update Book_User Set COUNT = {0} Where Book_User.BOOK_ID = {1} and Book_User.USER_ID = {2}", current_count, book.Book_id, this.user_id); DataBaseEvents.ExecuteNonQuery(query_update, data_source); } } } }
private void btn_give_book_Click(object sender, EventArgs e) { Microwave main_page = (Microwave)Application.OpenForms["Microwave"]; Book current = book_list.Find_Book_By_Chosen(); if (current == null) { current = search_list.Find_Book_By_Chosen(); } if (current == null) { lbl_message.Text = "*Please choose a book to lend"; lbl_message.ForeColor = Color.Red; return; } if (user.Fee > 0) { lbl_message.Text = "*Should pay all fees before"; lbl_message.ForeColor = Color.Red; return; } if (user.Book_count < 5) { if (current.Count <= 0) { lbl_message.Text = "*That book doesn't exist right now"; lbl_message.ForeColor = Color.Red; return; } current.Give_Book_To_User(user); current.Change_Count(); Author curr_aut = main_page.Main_author_list.Find_Author_By_ID(current.Author_id); Category curr_ctg = main_page.Main_category_list.Find_Category_By_ID(current.Category_id); current.Popularity_score += 5; curr_aut.Popularity_score += 5; curr_ctg.Popularity_score += 5; current.Change_Popularity_Score(); curr_aut.Change_Popularity_Score(); curr_ctg.Change_Popularity_Score(); DataTable dt = Book.Show_All_Books(user); int rows_count = dt.Rows.Count; if (rows_count <= 0) { user.Book_count = 0; } else { int user_book_count = 0; for (int i = 0; i < rows_count; i++) { user_book_count += int.Parse(dt.Rows[i][9].ToString()); } user.Book_count = user_book_count; } dt_form.Tb_6.Text = user.Book_count.ToString(); dt_form.Dgw_users.DataSource = dt; this.lbl_message.Text = "*Book has been lended successfully"; this.lbl_message.ForeColor = Color.LightGreen; } else { lbl_message.Text = "*A user can only have at most 5 books at the same time "; lbl_message.ForeColor = Color.Red; return; } }