diff --git a/internal/database/handlers.go b/internal/database/handlers.go index 6cce218..89deffd 100644 --- a/internal/database/handlers.go +++ b/internal/database/handlers.go @@ -36,9 +36,9 @@ func GetBoard(db *sql.DB, slug string) (Board, error) { return result, row.Err() } -func GetThreads(db *sql.DB, slug string) ([]Thread, error) { +func GetThreads(db *sql.DB, boardSlug string) ([]Thread, error) { rows, err := db.Query( - `SELECT id, board_slug, subject, created_at, bumped_at FROM threads WHERE board_slug = ?`, slug) + `SELECT id, board_slug, subject, created_at, bumped_at FROM threads WHERE board_slug = ?`, boardSlug) if err != nil { return nil, err } @@ -69,7 +69,7 @@ func GetThread(db *sql.DB, threadId int) (Thread, error) { return thread, row.Err() } -func GetThreadPosts(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 FROM posts where thread_id = ?`, threadId) if err != nil { return nil, err diff --git a/web/main.go b/web/main.go index 5ff750b..d75705e 100644 --- a/web/main.go +++ b/web/main.go @@ -3,15 +3,14 @@ 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" + "strconv" ) var dev = true @@ -52,10 +51,12 @@ func main() { // MAIN ROUTES // ----------------- + // INDEX PAGE r.Get("/", func(w http.ResponseWriter, r *http.Request) { views.Index().Render(r.Context(), w) }) + // MAIN BOARD PAGE r.Get("/{slug}", func(w http.ResponseWriter, r *http.Request) { slug := chi.URLParam(r, "slug") @@ -69,6 +70,7 @@ func main() { views.Board(board).Render(r.Context(), w) }) + // THREAD PAGE r.Get("/{slug}/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) { slug := chi.URLParam(r, "slug") // TODO: use relative thread_nums (see issue #1) @@ -93,7 +95,7 @@ func main() { return } - posts, err := database.GetThreadPosts(db, threadId) + posts, err := database.GetPosts(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) @@ -109,6 +111,27 @@ func main() { views.Thread(board, thread, posts).Render(r.Context(), w) }) + // CREATE THREAD + r.Post("/{slug}/threads", func(w http.ResponseWriter, r *http.Request) { + slug := chi.URLParam(r, "slug") + + if err := r.ParseForm(); err != nil { + http.Error(w, "Bad form data", http.StatusBadRequest) + log.Printf("ParseForm: %v", err) + return + } + + subject := r.FormValue("subject") + body := r.FormValue("body") + + if err := database.PutThread(db, slug, subject, body); err != nil { + http.Error(w, "Failed to create thread", http.StatusInternalServerError) + log.Printf("PutThread: %v", err) + return + } + }) + + // CREATE POST r.Post("/{slug}/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) { // slug := chi.URLParam(r, "slug") // TODO: use relative thread_nums (see issue #1) @@ -134,31 +157,13 @@ func main() { } }) - r.Post("/{slug}/threads", func(w http.ResponseWriter, r *http.Request) { - slug := chi.URLParam(r, "slug") - - if err := r.ParseForm(); err != nil { - http.Error(w, "Bad form data", http.StatusBadRequest) - log.Printf("ParseForm: %v", err) - return - } - - subject := r.FormValue("subject") - body := r.FormValue("body") - - if err := database.PutThread(db, slug, subject, body); err != nil { - http.Error(w, "Failed to create thread", http.StatusInternalServerError) - log.Printf("PutThread: %v", err) - return - } - }) - // ----------------- // ----------------- - // PARTIALS + // PARTIALS (htmx) // ----------------- + // CATALOG r.Get("/hx/{slug}/catalog", func(w http.ResponseWriter, r *http.Request) { slug := chi.URLParam(r, "slug") threads, err := database.GetThreads(db, slug) @@ -170,10 +175,10 @@ func main() { previews := make([]views.CatalogThreadPreview, 0, len(threads)) for _, thread := range threads { - posts, err := database.GetThreadPosts(db, thread.Id) + posts, err := database.GetPosts(db, thread.Id) if err != nil { http.Error(w, "Error getting thread posts", http.StatusBadRequest) - log.Printf("GetThreadPosts: %v", err) + log.Printf("GetPosts: %v", err) return } diff --git a/web/static/index.css b/web/static/index.css index 702df29..76fff06 100644 --- a/web/static/index.css +++ b/web/static/index.css @@ -59,7 +59,7 @@ body { margin: 2rem; } -.catalog-post { +.catalog-preview { padding: 10px; border: 2px solid rgba(111, 111, 111, 0.34); min-width: 140px; @@ -67,18 +67,18 @@ body { max-height: 192px; } -.catalog-post h1 { +.catalog-preview h1 { font-size: 14px; color: #0F0C5D; font-weight: bold; } -.catalog-post p { +.catalog-preview p { margin-top: 10px; font-size: 12px; } -.catalog-post-link { +.catalog-preview-link { cursor: pointer; } @@ -106,13 +106,13 @@ body { margin-left: auto; } -.new-thread-container { +.new-post-container { width: max-content; margin: auto; margin-top: 16px; } -.new-thread-form-field th { +.new-post-form-field th { border: 1px solid black; text-align: left; vertical-align: middle; @@ -122,7 +122,7 @@ body { font-size: .8rem; } -.new-thread-form-field td { +.new-post-form-field td { display: block; margin: 2px; } diff --git a/web/views/board.templ b/web/views/board.templ index cb08aac..d4a8961 100644 --- a/web/views/board.templ +++ b/web/views/board.templ @@ -13,11 +13,11 @@ templ NewThreadForm(board database.Board) { then trigger refreshThreads on body"> - + - + @@ -40,7 +40,7 @@ templ BoardHeader(board database.Board) { templ Board(board database.Board) { @shared.Layout() { @BoardHeader(board) -
+
diff --git a/web/views/thread.templ b/web/views/thread.templ index 015f571..7f63e2f 100644 --- a/web/views/thread.templ +++ b/web/views/thread.templ @@ -10,11 +10,11 @@ import ( templ Thread(board database.Board, thread database.Thread, posts []database.Post) { @shared.Layout() { @BoardHeader(board) -
+
Subject
Comment
- + diff --git a/web/views/threads.templ b/web/views/threads.templ index 9465194..8bb2efe 100644 --- a/web/views/threads.templ +++ b/web/views/threads.templ @@ -9,8 +9,8 @@ ThreadURL string templ ThreadsCatalog(previews []CatalogThreadPreview) {
for _, preview := range previews { -
-

{ preview.Subject }

+
+

{ preview.Subject }

{ preview.Body }

}
Comment