diff --git a/internal/database/handlers.go b/internal/database/handlers.go index 89deffd..3b4befc 100644 --- a/internal/database/handlers.go +++ b/internal/database/handlers.go @@ -70,7 +70,7 @@ func GetThread(db *sql.DB, threadId int) (Thread, 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) + rows, err := db.Query(`SELECT id, thread_id, author, body, created_at, media_path FROM posts where thread_id = ?`, threadId) if err != nil { return nil, err } @@ -79,7 +79,7 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) { var result []Post for rows.Next() { var p Post - err := rows.Scan(&p.Id, &p.ThreadId, &p.Author, &p.Body, &p.CreatedAt) + err := rows.Scan(&p.Id, &p.ThreadId, &p.Author, &p.Body, &p.CreatedAt, &p.MediaPath) if err != nil { return nil, err } @@ -106,7 +106,7 @@ func PutThread(db *sql.DB, boardSlug string, subject string, body string) error return err } - _, err = tx.Exec(`INSERT INTO posts (thread_id, body) VALUES (?, ?)`, threadId, body) + _, err = tx.Exec(`INSERT INTO posts (thread_id, body, media_path) VALUES (?, ?, ?)`, threadId, body, "default.png") if err != nil { return err } diff --git a/internal/database/models.go b/internal/database/models.go index 42ee5ea..c1d5999 100644 --- a/internal/database/models.go +++ b/internal/database/models.go @@ -23,4 +23,5 @@ type Post struct { Author string Body string CreatedAt time.Time + MediaPath string } diff --git a/internal/database/seed.sql b/internal/database/seed.sql index 8ca9818..b2256f4 100644 --- a/internal/database/seed.sql +++ b/internal/database/seed.sql @@ -26,6 +26,7 @@ CREATE TABLE IF NOT EXISTS posts ( author TEXT DEFAULT 'Anonymous', body TEXT NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + media_path TEXT NOT NULL DEFAULT '', FOREIGN KEY (thread_id) REFERENCES threads(id) ON DELETE CASCADE ); @@ -46,11 +47,8 @@ INSERT INTO boards (slug, name, tag) VALUES 'comfy', 'Welcome to /comfy/.' ); - INSERT INTO posts (thread_id, body) VALUES ( + INSERT INTO posts (thread_id, body, media_path) VALUES ( (SELECT id FROM threads WHERE subject LIKE 'Welcome to /comfy/.%'), - 'Post comfy. Be nice.' - ); - INSERT INTO posts (thread_id, body) VALUES ( - (SELECT id FROM threads WHERE subject LIKE 'Welcome to /comfy/.%'), - 'Henlo fren.' + 'Post comfy. Be nice.', + '1.jpg' ); diff --git a/web/main.go b/web/main.go index 7b4539f..903c890 100644 --- a/web/main.go +++ b/web/main.go @@ -102,9 +102,9 @@ func main() { return } - if len(posts) == 0 { + if len(posts) == 0 || posts[0].MediaPath == "" { http.Error(w, "Malformed thread", http.StatusInternalServerError) - log.Printf("Thread %d has no posts", thread.Id) + log.Printf("Thread %d has no posts or no OP image", threadId) return } @@ -182,9 +182,9 @@ func main() { return } - if len(posts) == 0 { + if len(posts) == 0 || posts[0].MediaPath == "" { http.Error(w, "Malformed thread", http.StatusInternalServerError) - log.Printf("Thread %d has no posts", thread.Id) + log.Printf("Thread %d has no posts or no OP image", thread.Id) return } @@ -192,6 +192,7 @@ func main() { Subject: thread.Subject, Body: posts[0].Body, ThreadURL: fmt.Sprintf("/%s/threads/%d", slug, thread.Id), + MediaPath: posts[0].MediaPath, }) } @@ -216,9 +217,9 @@ func main() { return } - if len(posts) == 0 { + if len(posts) == 0 || posts[0].MediaPath == "" { http.Error(w, "Malformed thread", http.StatusInternalServerError) - log.Printf("Thread %d has no posts", threadId) + log.Printf("Thread %d has no posts or no OP image", threadId) return } diff --git a/web/static/img/posts/1.jpg b/web/static/img/posts/1.jpg new file mode 100644 index 0000000..64383cc Binary files /dev/null and b/web/static/img/posts/1.jpg differ diff --git a/web/static/img/posts/default.png b/web/static/img/posts/default.png new file mode 100644 index 0000000..2d00a48 Binary files /dev/null and b/web/static/img/posts/default.png differ diff --git a/web/static/index.css b/web/static/index.css index 76fff06..1545d25 100644 --- a/web/static/index.css +++ b/web/static/index.css @@ -53,13 +53,13 @@ body { /* THREAD CATALOG */ #catalog { - display: grid; - grid-template-columns: auto auto auto auto; - gap: 1rem; + display: flex; margin: 2rem; } .catalog-preview { + margin: 10px; + text-align: center; padding: 10px; border: 2px solid rgba(111, 111, 111, 0.34); min-width: 140px; @@ -82,6 +82,11 @@ body { cursor: pointer; } +.catalog-preview-img { + max-height: 70%; + max-width: 80%; + margin-bottom: 10px; +} /* NEW THREAD BUTTON/FORM */ @@ -153,11 +158,29 @@ body { color: #13A82A; } +.post-img-container { + float: left; + margin: 4px 12px 4px 0; +} + +.post-img { + height: auto; + max-width: 400px; + margin-bottom: 10px; +} + +.post-body { + overflow-wrap: break-word; + line-height: 1.3; +} + .op-post { margin-bottom: 10px; } .post { + display: inline-block; + margin: 5x; padding: 10px; background-color: #D6DAF0; border: 1px solid #282a2e; diff --git a/web/views/thread.templ b/web/views/thread.templ index e1729b1..e4fc2c6 100644 --- a/web/views/thread.templ +++ b/web/views/thread.templ @@ -20,6 +20,7 @@ for _, post := range (posts) {

{ post.Body }

+
} } @@ -33,7 +34,7 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post - + @@ -43,6 +44,11 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post
+ if posts[0].MediaPath != "" { +
+ +
+ }

{ thread.Subject } -

@@ -50,9 +56,7 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post { posts[0].CreatedAt.Format("15:04") } No. { strconv.Itoa(posts[0].Id) }
-
-

{ posts[0].Body }

-
+

{ posts[0].Body }

diff --git a/web/views/threads.templ b/web/views/threads.templ index 8bb2efe..a25a0f9 100644 --- a/web/views/threads.templ +++ b/web/views/threads.templ @@ -1,15 +1,19 @@ package views +import "fmt" + type CatalogThreadPreview struct { Subject string Body string ThreadURL string +MediaPath string } templ ThreadsCatalog(previews []CatalogThreadPreview) {
for _, preview := range previews {
+

{ preview.Subject }

{ preview.Body }

Commentcomment