Some database refactoring
This commit is contained in:
@@ -33,9 +33,9 @@ func GetBoard(db *sql.DB, slug string) (Board, error) {
|
|||||||
return result, row.Err()
|
return result, row.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetBoardThreads(db *sql.DB, board_id int) ([]Thread, error) {
|
func GetBoardThreads(db *sql.DB, slug string) ([]Thread, error) {
|
||||||
rows, err := db.Query(
|
rows, err := db.Query(
|
||||||
`SELECT id, board_id, subject, created_at, bumped_at FROM threads WHERE board_id = ?`, board_id)
|
`SELECT id, board_slug, subject, created_at, bumped_at FROM threads WHERE board_slug = ?`, slug)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -44,7 +44,7 @@ func GetBoardThreads(db *sql.DB, board_id int) ([]Thread, error) {
|
|||||||
var result []Thread
|
var result []Thread
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var t Thread
|
var t Thread
|
||||||
err := rows.Scan(&t.Id, &t.BoardId, &t.Subject, &t.CreatedAt, &t.BumpedAt)
|
err := rows.Scan(&t.Id, &t.BoardSlug, &t.Subject, &t.CreatedAt, &t.BumpedAt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -74,14 +74,14 @@ func GetThreadPosts(db *sql.DB, thread_id int) ([]Post, error) {
|
|||||||
return result, rows.Err()
|
return result, rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func PutBoardThread(db *sql.DB, boardId int, subject string, body string) error {
|
func PutBoardThread(db *sql.DB, board_slug string, subject string, body string) error {
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
res, err := tx.Exec(`INSERT INTO threads (board_id, subject) VALUES (?, ?) RETURNING id`, boardId, subject)
|
res, err := tx.Exec(`INSERT INTO threads (board_slug, subject) VALUES (?, ?) RETURNING id`, board_slug, subject)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ type Board struct {
|
|||||||
|
|
||||||
type Thread struct {
|
type Thread struct {
|
||||||
Id int
|
Id int
|
||||||
BoardId int
|
BoardSlug string
|
||||||
Subject string
|
Subject string
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
BumpedAt time.Time
|
BumpedAt time.Time
|
||||||
|
|||||||
@@ -13,11 +13,11 @@ CREATE TABLE IF NOT EXISTS boards (
|
|||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS threads (
|
CREATE TABLE IF NOT EXISTS threads (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
board_id INTEGER NOT NULL,
|
board_slug TEXT NOT NULL,
|
||||||
subject TEXT NOT NULL DEFAULT '',
|
subject TEXT NOT NULL DEFAULT '',
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
bumped_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
bumped_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (board_id) REFERENCES boards(id) ON DELETE CASCADE
|
FOREIGN KEY (board_slug) REFERENCES boards(slug) ON DELETE CASCADE ON UPDATE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS posts (
|
CREATE TABLE IF NOT EXISTS posts (
|
||||||
@@ -42,8 +42,8 @@ INSERT INTO boards (slug, name, tag) VALUES
|
|||||||
-- Welcome threads
|
-- Welcome threads
|
||||||
|
|
||||||
-- /comfy/
|
-- /comfy/
|
||||||
INSERT INTO threads (board_id, subject) VALUES (
|
INSERT INTO threads (board_slug, subject) VALUES (
|
||||||
(SELECT id FROM boards WHERE slug='comfy'),
|
'comfy',
|
||||||
'Welcome to /comfy/.'
|
'Welcome to /comfy/.'
|
||||||
);
|
);
|
||||||
INSERT INTO posts (thread_id, body) VALUES (
|
INSERT INTO posts (thread_id, body) VALUES (
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var dev = true
|
var dev = true
|
||||||
@@ -58,15 +57,24 @@ func main() {
|
|||||||
views.Board(board).Render(r.Context(), w)
|
views.Board(board).Render(r.Context(), w)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Get("/hx/boards/{boardId}/catalog", func(w http.ResponseWriter, r *http.Request) {
|
// r.Get("/{slug}/{threadId}/posts", func(w http.ResponseWriter, r *http.Request) {
|
||||||
boardIdStr := chi.URLParam(r, "boardId")
|
// slug := chi.URLParam(r, "slug")
|
||||||
boardId, err := strconv.Atoi(boardIdStr)
|
// threadIdStr := chi.URLParam(r, "postId")
|
||||||
if err != nil {
|
// threadId, err := strconv.Atoi(threadIdStr)
|
||||||
http.Error(w, "invalid board id", http.StatusBadRequest)
|
// if err != nil {
|
||||||
return
|
// 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)
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
|
||||||
threads, err := database.GetBoardThreads(db, boardId)
|
r.Get("/hx/{slug}/catalog", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slug := chi.URLParam(r, "slug")
|
||||||
|
threads, err := database.GetBoardThreads(db, slug)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to get board threads", http.StatusInternalServerError)
|
http.Error(w, "Failed to get board threads", http.StatusInternalServerError)
|
||||||
log.Printf("GetBoardThreads: %v", err)
|
log.Printf("GetBoardThreads: %v", err)
|
||||||
@@ -91,13 +99,8 @@ func main() {
|
|||||||
views.PostsCatalog(vms).Render(r.Context(), w)
|
views.PostsCatalog(vms).Render(r.Context(), w)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Post("/boards/{boardId}/threads", func(w http.ResponseWriter, r *http.Request) {
|
r.Post("/{slug}/threads", func(w http.ResponseWriter, r *http.Request) {
|
||||||
boardIdStr := chi.URLParam(r, "boardId")
|
slug := chi.URLParam(r, "slug")
|
||||||
boardId, err := strconv.Atoi(boardIdStr)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "invalid board id", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := r.ParseForm(); err != nil {
|
if err := r.ParseForm(); err != nil {
|
||||||
http.Error(w, "Bad form data", http.StatusBadRequest)
|
http.Error(w, "Bad form data", http.StatusBadRequest)
|
||||||
@@ -107,7 +110,7 @@ func main() {
|
|||||||
subject := r.FormValue("subject")
|
subject := r.FormValue("subject")
|
||||||
body := r.FormValue("body")
|
body := r.FormValue("body")
|
||||||
|
|
||||||
database.PutBoardThread(db, boardId, subject, body)
|
database.PutBoardThread(db, slug, subject, body)
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println("Listening on :8080")
|
fmt.Println("Listening on :8080")
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ body {
|
|||||||
margin: 2rem;
|
margin: 2rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.thread-catalog-post {
|
.catalog-post {
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
border: 2px solid rgba(111, 111, 111, 0.34);
|
border: 2px solid rgba(111, 111, 111, 0.34);
|
||||||
min-width: 140px;
|
min-width: 140px;
|
||||||
@@ -72,17 +72,21 @@ body {
|
|||||||
max-height: 192px;
|
max-height: 192px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.thread-catalog-post h1 {
|
.catalog-post h1 {
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
color: #0F0C5D;
|
color: #0F0C5D;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
.thread-catalog-post p {
|
.catalog-post p {
|
||||||
margin-top: 10px;
|
margin-top: 10px;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.catalog-post-link {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
#newThreadBtn {
|
#newThreadBtn {
|
||||||
margin-top: 12px;
|
margin-top: 12px;
|
||||||
background: none;
|
background: none;
|
||||||
@@ -103,8 +107,6 @@ body {
|
|||||||
margin-left: auto;
|
margin-left: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
.new-thread-form-field {}
|
|
||||||
|
|
||||||
.new-thread-form-field th {
|
.new-thread-form-field th {
|
||||||
border: 1px solid black;
|
border: 1px solid black;
|
||||||
text-align: left;
|
text-align: left;
|
||||||
|
|||||||
+25
-22
@@ -1,24 +1,21 @@
|
|||||||
package views
|
package views
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
database "github.com/dominicf2001/comfychan/internal/database"
|
database "github.com/dominicf2001/comfychan/internal/database"
|
||||||
"github.com/dominicf2001/comfychan/web/views/shared"
|
"github.com/dominicf2001/comfychan/web/views/shared"
|
||||||
)
|
)
|
||||||
|
|
||||||
templ NewThreadForm(board database.Board) {
|
templ NewThreadForm(board database.Board) {
|
||||||
<form
|
<form style="display: none;" id="newThreadForm" hx-swap="none" hx-post={ fmt.Sprintf("/%s/threads", board.Slug) } _="on htmx:afterRequest
|
||||||
style="display: none;"
|
hide me
|
||||||
id="newThreadForm"
|
then show #newThreadBtn
|
||||||
hx-swap="none"
|
then trigger refreshThreads on body">
|
||||||
hx-post={ fmt.Sprintf("/boards/%d/threads", board.Id) }
|
|
||||||
_="on htmx:afterRequest hide me then show #newThreadBtn then trigger refreshThreads on body"
|
|
||||||
>
|
|
||||||
<table>
|
<table>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr class="new-thread-form-field">
|
<tr class="new-thread-form-field">
|
||||||
<th>Subject</th>
|
<th>Subject</th>
|
||||||
<td><input name="subject"/></td>
|
<td><input name="subject" /></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr class="new-thread-form-field">
|
<tr class="new-thread-form-field">
|
||||||
<th>Comment</th>
|
<th>Comment</th>
|
||||||
@@ -27,22 +24,28 @@ templ NewThreadForm(board database.Board) {
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<button type="submit">Submit</button>
|
<button type="submit">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
}
|
}
|
||||||
|
|
||||||
templ Board(board database.Board) {
|
templ BoardHeader(board database.Board) {
|
||||||
@shared.Layout() {
|
<header class="board-header">
|
||||||
<header class="board-header">
|
<img src="/static/img/banner-comfy.png" />
|
||||||
<img src="/static/img/banner-comfy.png"/>
|
|
||||||
<h1>
|
<h1>
|
||||||
{ fmt.Sprintf("/%s/ - %s", board.Slug, board.Name) }
|
{ fmt.Sprintf("/%s/ - %s", board.Slug, board.Name) }
|
||||||
</h1>
|
</h1>
|
||||||
<p>{ board.Tag }</p>
|
<p>{ board.Tag }</p>
|
||||||
</header>
|
</header>
|
||||||
<div class="new-thread-container">
|
}
|
||||||
<button _="on click hide me then show #newThreadForm" id="newThreadBtn">[New Thread]</button>
|
|
||||||
|
templ Board(board database.Board) {
|
||||||
|
@shared.Layout() {
|
||||||
|
@BoardHeader(board)
|
||||||
|
<div class="new-thread-container">
|
||||||
|
<button _="on click
|
||||||
|
hide me
|
||||||
|
then show #newThreadForm" id="newThreadBtn">[New Thread]</button>
|
||||||
@NewThreadForm(board)
|
@NewThreadForm(board)
|
||||||
</div>
|
</div>
|
||||||
<div hx-get={ fmt.Sprintf("/hx/boards/%d/catalog", board.Id) } hx-trigger="load, refreshThreads from:body"></div>
|
<div hx-get={ fmt.Sprintf("/hx/%s/catalog", board.Slug) } hx-trigger="load, refreshThreads from:body"></div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package views
|
||||||
|
|
||||||
|
import "github.com/dominicf2001/comfychan/internal/database"
|
||||||
|
|
||||||
|
templ Post(board database.Board) {
|
||||||
|
BoardHeader(board)
|
||||||
|
}
|
||||||
@@ -3,17 +3,17 @@ package views
|
|||||||
import "github.com/dominicf2001/comfychan/internal/database"
|
import "github.com/dominicf2001/comfychan/internal/database"
|
||||||
|
|
||||||
type ThreadGridBoxViewModel struct {
|
type ThreadGridBoxViewModel struct {
|
||||||
Thread database.Thread
|
Thread database.Thread
|
||||||
Posts []database.Post
|
Posts []database.Post
|
||||||
}
|
}
|
||||||
|
|
||||||
templ PostsCatalog(vms []ThreadGridBoxViewModel) {
|
templ PostsCatalog(vms []ThreadGridBoxViewModel) {
|
||||||
<div id="catalog">
|
<div id="catalog">
|
||||||
for _, vm := range vms {
|
for _, vm := range vms {
|
||||||
<div class="thread-catalog-post">
|
<div class="catalog-post">
|
||||||
<h1>{ vm.Thread.Subject }</h1>
|
<h1><a class="catalog-post-link">{ vm.Thread.Subject }</a></h1>
|
||||||
<p>{ vm.Posts[0].Body }</p>
|
<p>{ vm.Posts[0].Body }</p>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user