Optimize catalog view
This commit is contained in:
@@ -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
@@ -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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user