Small code cleanup
This commit is contained in:
@@ -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
|
||||
|
||||
+32
-27
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -13,11 +13,11 @@ templ NewThreadForm(board database.Board) {
|
||||
then trigger refreshThreads on body">
|
||||
<table>
|
||||
<tbody>
|
||||
<tr class="new-thread-form-field">
|
||||
<tr class="new-post-form-field">
|
||||
<th>Subject</th>
|
||||
<td><input name="subject" /></td>
|
||||
</tr>
|
||||
<tr class="new-thread-form-field">
|
||||
<tr class="new-post-form-field">
|
||||
<th>Comment</th>
|
||||
<td><textarea name="body" required></textarea></td>
|
||||
</tr>
|
||||
@@ -40,7 +40,7 @@ templ BoardHeader(board database.Board) {
|
||||
templ Board(board database.Board) {
|
||||
@shared.Layout() {
|
||||
@BoardHeader(board)
|
||||
<div class="new-thread-container">
|
||||
<div class="new-post-container">
|
||||
<button _="on click
|
||||
hide me
|
||||
then show #newThreadForm" id="newThreadBtn">[New Thread]</button>
|
||||
|
||||
@@ -10,11 +10,11 @@ import (
|
||||
templ Thread(board database.Board, thread database.Thread, posts []database.Post) {
|
||||
@shared.Layout() {
|
||||
@BoardHeader(board)
|
||||
<div class="new-thread-container">
|
||||
<div class="new-post-container">
|
||||
<form id="newPostForm" hx-swap="none" hx-post={ fmt.Sprintf("/%s/threads/%d", board.Slug, thread.Id) }>
|
||||
<table>
|
||||
<tbody>
|
||||
<tr class="new-thread-form-field">
|
||||
<tr class="new-post-form-field">
|
||||
<th>Comment</th>
|
||||
<td><textarea name="body" required></textarea></td>
|
||||
</tr>
|
||||
|
||||
@@ -9,8 +9,8 @@ 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>
|
||||
<div class="catalog-preview">
|
||||
<h1><a href={ templ.SafeURL(preview.ThreadURL) } class="catalog-preview-link">{ preview.Subject }</a></h1>
|
||||
<p>{ preview.Body }</p>
|
||||
</div>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user