Make threads openable from catalog
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
internal/database/comfychan.db
|
||||
**/*_templ.go
|
||||
tmp
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
live/templ:
|
||||
templ generate --watch --proxy="http://localhost:8080" --cmd="go run ." --open-browser=false -v
|
||||
templ generate --watch --proxy="http://localhost:8080" --cmd="go run ./web" --open-browser=false -v
|
||||
|
||||
live/sync_assets:
|
||||
go run github.com/air-verse/air@v1.61.7 \
|
||||
|
||||
@@ -28,12 +28,15 @@ func GetBoard(db *sql.DB, slug string) (Board, error) {
|
||||
row := db.QueryRow(`SELECT id, name, slug, tag FROM boards WHERE slug = ?`, slug)
|
||||
|
||||
var result Board
|
||||
row.Scan(&result.Id, &result.Name, &result.Slug, &result.Tag)
|
||||
err := row.Scan(&result.Id, &result.Name, &result.Slug, &result.Tag)
|
||||
if err != nil {
|
||||
return Board{}, err
|
||||
}
|
||||
|
||||
return result, row.Err()
|
||||
}
|
||||
|
||||
func GetBoardThreads(db *sql.DB, slug string) ([]Thread, error) {
|
||||
func GetThreads(db *sql.DB, slug string) ([]Thread, error) {
|
||||
rows, err := db.Query(
|
||||
`SELECT id, board_slug, subject, created_at, bumped_at FROM threads WHERE board_slug = ?`, slug)
|
||||
if err != nil {
|
||||
@@ -54,8 +57,20 @@ func GetBoardThreads(db *sql.DB, slug string) ([]Thread, error) {
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func GetThreadPosts(db *sql.DB, thread_id int) ([]Post, error) {
|
||||
rows, err := db.Query(`SELECT id, thread_id, author, body, created_at FROM posts where thread_id = ?`, thread_id)
|
||||
func GetThread(db *sql.DB, threadId int) (Thread, error) {
|
||||
row := db.QueryRow(`SELECT id, board_slug, subject, created_at, bumped_at FROM threads WHERE id = ?`, threadId)
|
||||
|
||||
var thread Thread
|
||||
err := row.Scan(&thread.Id, &thread.BoardSlug, &thread.Subject, &thread.CreatedAt, &thread.BumpedAt)
|
||||
if err != nil {
|
||||
return Thread{}, err
|
||||
}
|
||||
|
||||
return thread, row.Err()
|
||||
}
|
||||
|
||||
func GetThreadPosts(db *sql.DB, threadId int) ([]Post, error) {
|
||||
rows, err := db.Query(`SELECT id, thread_id, author, body, created_at FROM posts where thread_id = ?`, threadId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -74,14 +89,14 @@ func GetThreadPosts(db *sql.DB, thread_id int) ([]Post, error) {
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func PutBoardThread(db *sql.DB, board_slug string, subject string, body string) error {
|
||||
func PutBoardThread(db *sql.DB, boardSlug string, subject string, body string) error {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
res, err := tx.Exec(`INSERT INTO threads (board_slug, subject) VALUES (?, ?) RETURNING id`, board_slug, subject)
|
||||
res, err := tx.Exec(`INSERT INTO threads (board_slug, subject) VALUES (?, ?) RETURNING id`, boardSlug, subject)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+81
-35
@@ -3,13 +3,15 @@ package main
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/dominicf2001/comfychan/internal/database"
|
||||
"github.com/dominicf2001/comfychan/web/views"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var dev = true
|
||||
@@ -25,6 +27,10 @@ func disableCacheInDevMode(next http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// -----------------
|
||||
// SETUP
|
||||
// -----------------
|
||||
|
||||
db, err := sql.Open("sqlite3", "internal/database/comfychan.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -40,6 +46,12 @@ func main() {
|
||||
http.StripPrefix("/static",
|
||||
http.FileServer(http.Dir("web/static")))))
|
||||
|
||||
// -----------------
|
||||
|
||||
// -----------------
|
||||
// MAIN ROUTES
|
||||
// -----------------
|
||||
|
||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
views.Index().Render(r.Context(), w)
|
||||
})
|
||||
@@ -57,46 +69,38 @@ func main() {
|
||||
views.Board(board).Render(r.Context(), w)
|
||||
})
|
||||
|
||||
// r.Get("/{slug}/{threadId}/posts", func(w http.ResponseWriter, r *http.Request) {
|
||||
// slug := chi.URLParam(r, "slug")
|
||||
// threadIdStr := chi.URLParam(r, "postId")
|
||||
// threadId, err := strconv.Atoi(threadIdStr)
|
||||
// if err != nil {
|
||||
// http.Error(w, "Invalid thread id", http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// posts, err := database.GetThreadPosts(db, threadId)
|
||||
// if err != nil {
|
||||
// http.Error(w, "Failed to get thread posts", http.StatusInternalServerError)
|
||||
// }
|
||||
// })
|
||||
|
||||
r.Get("/hx/{slug}/catalog", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Get("/{slug}/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) {
|
||||
slug := chi.URLParam(r, "slug")
|
||||
threads, err := database.GetBoardThreads(db, 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, "Failed to get board threads", http.StatusInternalServerError)
|
||||
log.Printf("GetBoardThreads: %v", err)
|
||||
http.Error(w, "Invalid thread id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
vms := make([]views.ThreadGridBoxViewModel, 0, len(threads))
|
||||
for _, thread := range threads {
|
||||
posts, err := database.GetThreadPosts(db, thread.Id)
|
||||
if err != nil {
|
||||
http.Error(w, "Error getting thread posts", http.StatusBadRequest)
|
||||
log.Printf("GetThreadPosts: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
vms = append(vms, views.ThreadGridBoxViewModel{
|
||||
Thread: thread,
|
||||
Posts: posts,
|
||||
})
|
||||
board, err := database.GetBoard(db, slug)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
log.Printf("Board %q not found: %v", slug, err)
|
||||
return
|
||||
}
|
||||
|
||||
views.PostsCatalog(vms).Render(r.Context(), w)
|
||||
thread, err := database.GetThread(db, threadId)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to get thread", http.StatusInternalServerError)
|
||||
log.Printf("Failed to get thread %q: %v", threadIdStr, err)
|
||||
return
|
||||
}
|
||||
|
||||
posts, err := database.GetThreadPosts(db, threadId)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to get thread posts", http.StatusInternalServerError)
|
||||
log.Printf("Failed to get thread %q posts: %v", threadIdStr, err)
|
||||
return
|
||||
}
|
||||
|
||||
views.Thread(board, thread, posts).Render(r.Context(), w)
|
||||
})
|
||||
|
||||
r.Post("/{slug}/threads", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -113,6 +117,48 @@ func main() {
|
||||
database.PutBoardThread(db, slug, subject, body)
|
||||
})
|
||||
|
||||
// -----------------
|
||||
|
||||
// -----------------
|
||||
// PARTIALS
|
||||
// -----------------
|
||||
|
||||
r.Get("/hx/{slug}/catalog", func(w http.ResponseWriter, r *http.Request) {
|
||||
slug := chi.URLParam(r, "slug")
|
||||
threads, err := database.GetThreads(db, slug)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to get board threads", http.StatusInternalServerError)
|
||||
log.Printf("GetThreads: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
previews := make([]views.CatalogThreadPreview, 0, len(threads))
|
||||
for _, thread := range threads {
|
||||
posts, err := database.GetThreadPosts(db, thread.Id)
|
||||
if err != nil {
|
||||
http.Error(w, "Error getting thread posts", http.StatusBadRequest)
|
||||
log.Printf("GetThreadPosts: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(posts) == 0 {
|
||||
http.Error(w, "Malformed thread", http.StatusInternalServerError)
|
||||
log.Printf("GetThreadPosts: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
previews = append(previews, views.CatalogThreadPreview{
|
||||
Subject: thread.Subject,
|
||||
Body: posts[0].Body,
|
||||
ThreadURL: fmt.Sprintf("/%s/threads/%d", slug, thread.Id),
|
||||
})
|
||||
}
|
||||
|
||||
views.ThreadsCatalog(previews).Render(r.Context(), w)
|
||||
})
|
||||
|
||||
// -----------------
|
||||
|
||||
fmt.Println("Listening on :8080")
|
||||
http.ListenAndServe(":8080", r)
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package views
|
||||
|
||||
import "github.com/dominicf2001/comfychan/internal/database"
|
||||
|
||||
templ Post(board database.Board) {
|
||||
BoardHeader(board)
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package views
|
||||
|
||||
import "github.com/dominicf2001/comfychan/internal/database"
|
||||
|
||||
type ThreadGridBoxViewModel struct {
|
||||
Thread database.Thread
|
||||
Posts []database.Post
|
||||
}
|
||||
|
||||
templ PostsCatalog(vms []ThreadGridBoxViewModel) {
|
||||
<div id="catalog">
|
||||
for _, vm := range vms {
|
||||
<div class="catalog-post">
|
||||
<h1><a class="catalog-post-link">{ vm.Thread.Subject }</a></h1>
|
||||
<p>{ vm.Posts[0].Body }</p>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package views
|
||||
|
||||
import (
|
||||
"github.com/dominicf2001/comfychan/internal/database"
|
||||
"github.com/dominicf2001/comfychan/web/views/shared"
|
||||
)
|
||||
|
||||
templ Thread(board database.Board, thread database.Thread, posts []database.Post) {
|
||||
@shared.Layout() {
|
||||
@BoardHeader(board)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package views
|
||||
|
||||
type CatalogThreadPreview struct {
|
||||
Subject string
|
||||
Body string
|
||||
ThreadURL string
|
||||
}
|
||||
|
||||
templ ThreadsCatalog(previews []CatalogThreadPreview) {
|
||||
<div id="catalog">
|
||||
for _, preview := range previews {
|
||||
<div class="catalog-post">
|
||||
<h1><a href={ templ.SafeURL(preview.ThreadURL) } class="catalog-post-link">{ preview.Subject }</a></h1>
|
||||
<p>{ preview.Body }</p>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
Reference in New Issue
Block a user