diff --git a/web/main.go b/web/main.go index d75705e..7b4539f 100644 --- a/web/main.go +++ b/web/main.go @@ -97,7 +97,7 @@ func main() { posts, err := database.GetPosts(db, threadId) if err != nil { - http.Error(w, "Failed to get thread posts", http.StatusInternalServerError) + http.Error(w, "Failed to get posts", http.StatusInternalServerError) log.Printf("Failed to get thread %q posts: %v", threadIdStr, err) return } @@ -168,7 +168,7 @@ func main() { slug := chi.URLParam(r, "slug") threads, err := database.GetThreads(db, slug) if err != nil { - http.Error(w, "Failed to get board threads", http.StatusInternalServerError) + http.Error(w, "Failed to get threads", http.StatusInternalServerError) log.Printf("GetThreads: %v", err) return } @@ -177,7 +177,7 @@ func main() { for _, thread := range threads { posts, err := database.GetPosts(db, thread.Id) if err != nil { - http.Error(w, "Error getting thread posts", http.StatusBadRequest) + http.Error(w, "Failed to get posts", http.StatusBadRequest) log.Printf("GetPosts: %v", err) return } @@ -198,6 +198,34 @@ func main() { views.ThreadsCatalog(previews).Render(r.Context(), w) }) + // THREAD POSTS + r.Get("/hx/{slug}/threads/{threadId}/posts", func(w http.ResponseWriter, r *http.Request) { + // slug := chi.URLParam(r, "slug") + // TODO: use relative thread_nums (see issue #1) + threadIdStr := chi.URLParam(r, "threadId") + threadId, err := strconv.Atoi(threadIdStr) + if err != nil { + http.Error(w, "Invalid thread id", http.StatusBadRequest) + return + } + + posts, err := database.GetPosts(db, threadId) + if err != nil { + http.Error(w, "Failed to get posts", http.StatusBadRequest) + log.Printf("GetPosts: %v", err) + return + } + + if len(posts) == 0 { + http.Error(w, "Malformed thread", http.StatusInternalServerError) + log.Printf("Thread %d has no posts", threadId) + return + } + + // dont pass the op post. only replies + views.ThreadPosts(posts[1:]).Render(r.Context(), w) + }) + // ----------------- fmt.Println("Listening on :8080") diff --git a/web/views/thread.templ b/web/views/thread.templ index 7f63e2f..e1729b1 100644 --- a/web/views/thread.templ +++ b/web/views/thread.templ @@ -7,16 +7,34 @@ import ( "strconv" ) +templ ThreadPosts(posts []database.Post) { +for _, post := range (posts) { +
+
+ + + { post.CreatedAt.Format("15:04") } + No. { strconv.Itoa(post.Id) } +
+
+

{ post.Body }

+
+
+} +} + templ Thread(board database.Board, thread database.Thread, posts []database.Post) { @shared.Layout() { @BoardHeader(board)
-
+ - +
Comment
@@ -36,19 +54,8 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post

{ posts[0].Body }

- for _, post := range (posts[1:]) { -
-
- - - { post.CreatedAt.Format("15:04") } - No. { strconv.Itoa(post.Id) } -
-
-

{ post.Body }

-
-
- } +
} }