1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-10 12:56:24 -07:00

Add ability to update thread attributes from manage page

This commit is contained in:
Eggbertx 2023-02-02 11:12:39 -08:00
parent 0f0c9362eb
commit 34cab06311
5 changed files with 174 additions and 70 deletions

View file

@ -3,6 +3,7 @@ package gcsql
import (
"database/sql"
"errors"
"fmt"
"strconv"
)
@ -41,7 +42,7 @@ func createThread(tx *sql.Tx, boardID int, locked bool, stickied bool, anchored
return threadID, err
}
// GetThread returns a a thread object from the database
// GetThread returns a a thread object from the database, given its ID
func GetThread(threadID int) (*Thread, error) {
const query = selectThreadsBaseSQL + `WHERE id = ?`
thread := new(Thread)
@ -52,6 +53,17 @@ func GetThread(threadID int) (*Thread, error) {
return thread, err
}
// GetPostThread returns a thread object from the database, given the ID of a post in the thread
func GetPostThread(opID int) (*Thread, error) {
const query = selectThreadsBaseSQL + `WHERE id = (SELECT thread_id FROM DBPREFIXposts WHERE id = ? LIMIT 1)`
thread := new(Thread)
err := QueryRowSQL(query, interfaceSlice(opID), interfaceSlice(
&thread.ID, &thread.BoardID, &thread.Locked, &thread.Stickied, &thread.Anchored, &thread.Cyclical,
&thread.LastBump, &thread.DeletedAt, &thread.IsDeleted,
))
return thread, err
}
// GetTopPostThreadID gets the thread ID from the database, given the post ID of a top post
func GetTopPostThreadID(opID int) (int, error) {
const query = `SELECT thread_id FROM DBPREFIXposts WHERE id = ? and is_top_post`
@ -195,6 +207,23 @@ func (t *Thread) GetUploads() ([]Upload, error) {
return uploads, nil
}
// UpdateThreadAttribute updates the given attribute (valid attribute values are "locked", "stickied, "anchored",
// or "cyclical") for the thread with the given top post ID
func UpdateThreadAttribute(opID int, attribute string, value bool) error {
updateSQL := "UPDATE DBPREFIXthreads SET "
if attribute == "locked" || attribute == "stickied" || attribute == "anchored" || attribute == "cyclical" {
updateSQL += attribute + " = ? WHERE id = ?"
} else {
return fmt.Errorf("invalid thread attribute %q", attribute)
}
threadID, err := GetTopPostThreadID(opID)
if err != nil {
return err
}
_, err = ExecSQL(updateSQL, value, threadID)
return err
}
// deleteThread updates the thread and sets it as deleted, as well as the posts where thread_id = threadID
func deleteThread(threadID int) error {
const deletePostsSQL = `UPDATE DBPREFIXposts SET is_deleted = TRUE, deleted_at = CURRENT_TIMESTAMP WHERE thread_id = ?`