|
|
|
@@ -4,6 +4,11 @@ import (
|
|
|
|
|
"database/sql"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Queryer interface {
|
|
|
|
|
Exec(query string, args ...any) (sql.Result, error)
|
|
|
|
|
QueryRow(query string, args ...any) *sql.Row
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetBoards(db *sql.DB) ([]Board, error) {
|
|
|
|
|
rows, err := db.Query(`
|
|
|
|
|
SELECT id, name, slug, tag
|
|
|
|
@@ -83,7 +88,7 @@ func GetThread(db *sql.DB, threadId int) (Thread, error) {
|
|
|
|
|
|
|
|
|
|
func GetPosts(db *sql.DB, threadId int) ([]Post, error) {
|
|
|
|
|
rows, err := db.Query(`
|
|
|
|
|
SELECT id, thread_id, author, body, created_at, media_path, ip_hash
|
|
|
|
|
SELECT id, thread_id, author, body, created_at, media_path, ip_hash, number
|
|
|
|
|
FROM posts
|
|
|
|
|
WHERE thread_id = ?`, threadId)
|
|
|
|
|
|
|
|
|
@@ -95,7 +100,7 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) {
|
|
|
|
|
var result []Post
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var p Post
|
|
|
|
|
err := rows.Scan(&p.Id, &p.ThreadId, &p.Author, &p.Body, &p.CreatedAt, &p.MediaPath, &p.IpHash)
|
|
|
|
|
err := rows.Scan(&p.Id, &p.ThreadId, &p.Author, &p.Body, &p.CreatedAt, &p.MediaPath, &p.IpHash, &p.Number)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@@ -107,13 +112,13 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) {
|
|
|
|
|
|
|
|
|
|
func GetOriginalPost(db *sql.DB, threadId int) (Post, error) {
|
|
|
|
|
row := db.QueryRow(`
|
|
|
|
|
SELECT id, thread_id, author, body, created_at, media_path, ip_hash
|
|
|
|
|
SELECT id, thread_id, author, body, created_at, media_path, ip_hash, number
|
|
|
|
|
FROM posts
|
|
|
|
|
WHERE thread_id = ?
|
|
|
|
|
ORDER BY created_at ASC LIMIT 1`, threadId)
|
|
|
|
|
|
|
|
|
|
var r Post
|
|
|
|
|
err := row.Scan(&r.Id, &r.ThreadId, &r.Author, &r.Body, &r.CreatedAt, &r.MediaPath, &r.IpHash)
|
|
|
|
|
err := row.Scan(&r.Id, &r.ThreadId, &r.Author, &r.Body, &r.CreatedAt, &r.MediaPath, &r.IpHash, &r.Number)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return Post{}, err
|
|
|
|
|
}
|
|
|
|
@@ -140,11 +145,7 @@ func PutThread(db *sql.DB, boardSlug string, subject string, body string, mediaP
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = tx.Exec(`
|
|
|
|
|
INSERT INTO posts (thread_id, body, media_path, ip_hash)
|
|
|
|
|
VALUES (?, ?, ?, ?)`, threadId, body, mediaPath, ip_hash)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err := PutPost(tx, boardSlug, int(threadId), body, mediaPath, ip_hash); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -155,10 +156,26 @@ func PutThread(db *sql.DB, boardSlug string, subject string, body string, mediaP
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PutPost(db *sql.DB, threadId int, body string, mediaPath string, ip_hash string) error {
|
|
|
|
|
func PutPost(db Queryer, boardSlug string, threadId int, body string, mediaPath string, ip_hash string) error {
|
|
|
|
|
row := db.QueryRow(`
|
|
|
|
|
SELECT MAX(p.number)
|
|
|
|
|
FROM posts p
|
|
|
|
|
INNER JOIN threads t ON p.thread_id = t.id
|
|
|
|
|
WHERE t.board_slug = ?`, boardSlug)
|
|
|
|
|
|
|
|
|
|
var latestPostNumber sql.NullInt64
|
|
|
|
|
if err := row.Scan(&latestPostNumber); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newPostNumber := 1
|
|
|
|
|
if latestPostNumber.Valid {
|
|
|
|
|
newPostNumber = int(latestPostNumber.Int64) + 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := db.Exec(`
|
|
|
|
|
INSERT INTO posts (thread_id, body, media_path, ip_hash)
|
|
|
|
|
VALUES (?, ?, ?, ?)`, threadId, body, mediaPath, ip_hash)
|
|
|
|
|
INSERT INTO posts (thread_id, body, media_path, ip_hash, number)
|
|
|
|
|
VALUES (?, ?, ?, ?, ?)`, threadId, body, mediaPath, ip_hash, newPostNumber)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|