func deleteMain(mainID string) { db, err := connectDB() general.CheckErr(err) defer db.Close() stmt, err := db.Prepare("delete from main where id=?") general.CheckErr(err) _, err = stmt.Exec(mainID) general.CheckErr(err) }
func deleteAllRepliesByThread(thr_id string) { db, err := connectDB() general.CheckErr(err) defer db.Close() stmt, err := db.Prepare("delete from reply where thread_id=?") general.CheckErr(err) _, err = stmt.Exec(thr_id) general.CheckErr(err) }
func (R *Reply) Delete() (err error) { db, err := connectDB() general.CheckErr(err) defer db.Close() stmt, err := db.Prepare("delete from reply where id=?") general.CheckErr(err) _, err = stmt.Exec(R.Id) general.CheckErr(err) return }
func (R *Reply) Update() { //Updates edited reply to database db, err := connectDB() general.CheckErr(err) defer db.Close() stmt, err := db.Prepare("UPDATE reply SET title=?,content=? WHERE id=?") general.CheckErr(err) _, err = stmt.Exec(R.Name, R.Content, R.Id) general.CheckErr(err) }
func (R *Reply) Create() (id int64) { //Saves new reply to database db, err := connectDB() general.CheckErr(err) defer db.Close() stmt, err := db.Prepare("INSERT reply SET title=?,content=?, thread_id=?, user_id=?") general.CheckErr(err) res, err := stmt.Exec(R.Name, R.Content, R.Thread_id, R.UserID) id, err = res.LastInsertId() //fmt.Println(Id, "sgsdg") general.CheckErr(err) return id }
func (R *Reply) GetSingleReply() { //opens and closes connection db, err := connectDB() general.CheckErr(err) defer db.Close() rows, err := db.Query(`SELECT reply.title, reply.content, reply.user_id, user.username FROM reply INNER JOIN user ON reply.user_id=user.Id WHERE reply.id=?`, R.Id) general.CheckErr(err) for rows.Next() { err = rows.Scan(&R.Name, &R.Content, &R.UserID, &R.Username) general.CheckErr(err) } }
func (tl *Thread) GetReplies() { //opens and closes connection db, err := connectDB() general.CheckErr(err) defer db.Close() rows, err := db.Query(`SELECT reply.id, reply.title, reply.content,reply.thread_id, user.username, reply.user_id, reply.updated FROM reply INNER JOIN user ON reply.user_id=user.Id WHERE reply.thread_id=?`, tl.Id) general.CheckErr(err) for rows.Next() { var r Reply err = rows.Scan(&r.Id, &r.Name, &r.Content, &r.Thread_id, &r.Username, &r.UserID, &r.Updated) general.CheckErr(err) tl.Reply = append(tl.Reply, r) } }
func (M *Main) Save() { //Saves new main post and creates new corresponding thread in the database db, err := connectDB() general.CheckErr(err) defer db.Close() //gets the category Id based on the category name cat_result, err := db.Query("SELECT id FROM category WHERE `name`='" + M.Category + "'") var cat_id string for cat_result.Next() { err = cat_result.Scan(&cat_id) general.CheckErr(err) } general.CheckErr(err) //checks to see if create or update request is needed if M.ID == "" { M.Create(cat_id) } else { M.Update(cat_id) } }
func updateThreadTimestamp(id string) { db, err := connectDB() general.CheckErr(err) defer db.Close() rows, err := db.Query(`SELECT reply.updated, reply.thread_id, reply.user_id FROM reply WHERE id=?`, id) general.CheckErr(err) var upd, thrID, userID string fmt.Println(upd, thrID, "sgsdg") for rows.Next() { err = rows.Scan(&upd, &thrID, &userID) general.CheckErr(err) } fmt.Println(upd, thrID, userID, "fgfgfgf") stmt, err := db.Prepare("UPDATE thread SET updated=?, user_id=? WHERE id=?") general.CheckErr(err) _, err = stmt.Exec(upd, userID, thrID) general.CheckErr(err) }
func (m *Main) GetMainData() { //opens and closes connection db, err := connectDB() general.CheckErr(err) defer db.Close() //query to get data from db rows, err := db.Query(`SELECT category.name, main.title, main.content, main.id, main.user_id, user.username, main.created, main.updated FROM thread INNER JOIN category ON thread.category_id=category.id INNER JOIN main ON thread.main_id=main.id INNER JOIN user ON main.user_id=user.Id WHERE thread.id=?`, m.Thr_id) general.CheckErr(err) //saves values to thread //tl.Id=id for rows.Next() { err = rows.Scan(&m.Category, &m.Name, &m.Content, &m.ID, &m.UserID, &m.Username, &m.Created, &m.Updated) general.CheckErr(err) } }
func (M *Main) Update(cat_id string) { db, err := connectDB() general.CheckErr(err) defer db.Close() stmt, err := db.Prepare("UPDATE main SET title=?,content=? WHERE id=?") general.CheckErr(err) res, err := stmt.Exec(M.Name, M.Content, M.ID) general.CheckErr(err) _, err = res.RowsAffected() general.CheckErr(err) stmt, err = db.Prepare("UPDATE thread SET title=?, category_id=? WHERE main_id=?") general.CheckErr(err) res, err = stmt.Exec(M.Name, cat_id, M.ID) general.CheckErr(err) }
func (M *Main) Create(cat_id string) { db, err := connectDB() general.CheckErr(err) defer db.Close() //insert new entry into the table main stmt, err := db.Prepare("INSERT main SET title=?,content=?, user_id=?") general.CheckErr(err) res, err := stmt.Exec(M.Name, M.Content, M.UserID) general.CheckErr(err) main_id, err := res.LastInsertId() general.CheckErr(err) //insert new entry into the table thread stmt, err = db.Prepare("INSERT thread SET title=?, category_id=?, main_id=?, user_id=?") general.CheckErr(err) res, err = stmt.Exec(M.Name, cat_id, main_id, M.UserID) general.CheckErr(err) }