Optimize catalog view

This commit is contained in:
Dominic Ferrando
2025-04-19 14:47:11 -04:00
parent 39c5b41657
commit 399d2b969d
2 changed files with 23 additions and 7 deletions
+15 -1
View File
@@ -70,7 +70,7 @@ func GetThread(db *sql.DB, threadId int) (Thread, error) {
} }
func GetPosts(db *sql.DB, threadId int) ([]Post, error) { func GetPosts(db *sql.DB, threadId int) ([]Post, error) {
rows, err := db.Query(`SELECT id, thread_id, author, body, created_at, media_path FROM posts where thread_id = ?`, threadId) rows, err := db.Query(`SELECT id, thread_id, author, body, created_at, media_path FROM posts WHERE thread_id = ?`, threadId)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -89,6 +89,20 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) {
return result, rows.Err() return result, rows.Err()
} }
func GetOriginalPost(db *sql.DB, threadId int) (Post, error) {
row := db.QueryRow(`SELECT id, thread_id, author, body, created_at, media_path
FROM posts
WHERE thread_id = ?
ORDER BY created_at ASC LIMIT 1`, threadId)
var result Post
err := row.Scan(&result.Id, &result.ThreadId, &result.Author, &result.Body, &result.CreatedAt, &result.MediaPath)
if err != nil {
return Post{}, err
}
return result, row.Err()
}
func PutThread(db *sql.DB, boardSlug string, subject string, body string, mediaPath string) error { func PutThread(db *sql.DB, boardSlug string, subject string, body string, mediaPath string) error {
tx, err := db.Begin() tx, err := db.Begin()
if err != nil { if err != nil {
+8 -6
View File
@@ -235,24 +235,26 @@ func main() {
previews := make([]views.CatalogThreadPreview, 0, len(threads)) previews := make([]views.CatalogThreadPreview, 0, len(threads))
for _, thread := range threads { for _, thread := range threads {
posts, err := database.GetPosts(db, thread.Id) op, err := database.GetOriginalPost(db, thread.Id)
if err != nil { if err != nil {
http.Error(w, "Failed to get posts", http.StatusBadRequest) http.Error(w, "Failed to get posts", http.StatusBadRequest)
log.Printf("GetPosts: %v", err) log.Printf("GetOriginalPost: %v", err)
return return
} }
if len(posts) == 0 || posts[0].MediaPath == "" { log.Printf("OP: %+v", op)
if op.MediaPath == "" {
http.Error(w, "Malformed thread", http.StatusInternalServerError) http.Error(w, "Malformed thread", http.StatusInternalServerError)
log.Printf("Thread %d has no posts or no OP image", thread.Id) log.Printf("Thread %d has no OP image", thread.Id)
return return
} }
previews = append(previews, views.CatalogThreadPreview{ previews = append(previews, views.CatalogThreadPreview{
Subject: thread.Subject, Subject: thread.Subject,
Body: posts[0].Body, Body: op.Body,
ThreadURL: fmt.Sprintf("/%s/threads/%d", slug, thread.Id), ThreadURL: fmt.Sprintf("/%s/threads/%d", slug, thread.Id),
MediaPath: posts[0].MediaPath, MediaPath: op.MediaPath,
}) })
} }