From aa1ab6182c36cf5128b05f292b7d6250e7419fe1 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 28 Aug 2025 13:41:30 -0400 Subject: [PATCH] Implement posting a message. --- api/handlers.go | 15 +++++++++++++++ data/models.go | 8 ++++++-- go.mod | 1 + go.sum | 2 ++ views/post/new.html | 16 +++++++++------- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index e421420..7be1417 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -51,6 +51,20 @@ func GetApiMessage(c *fiber.Ctx) error { return IfErrNil(err, c) } +func PostApiMessage(c *fiber.Ctx) error { + message, err := ReceivePost[data.Message](c) + if err != nil { return IfErrNil(err, c) } + + sql, args, err := sq.Insert("message"). + Columns("text", "user_id"). + Values(message.Text, 1).ToSql() + + err = data.Exec(err, sql, args...) + if err != nil { return IfErrNil(err, c) } + + return c.Redirect("/feed/") +} + func GetApiReplies(c *fiber.Ctx) error { message_id := c.Params("message_id") @@ -119,6 +133,7 @@ func Setup(app *fiber.App) { app.Post("/api/register", PostApiRegister) app.Get("/api/feed/:user_id", GetApiFeed) app.Get("/api/message/:id", GetApiMessage) + app.Post("/api/message/new", PostApiMessage) app.Get("/api/replies/:message_id", GetApiReplies) app.Get("/feed/", Page("feed")) diff --git a/data/models.go b/data/models.go index 74bd5f3..6fe4e13 100644 --- a/data/models.go +++ b/data/models.go @@ -1,6 +1,9 @@ package data -import "reflect" +import ( + "reflect" + "github.com/guregu/null/v6" +) type Login struct { Username string `db:"username" validate:"required,max=30"` @@ -21,9 +24,10 @@ type Message struct { CreatedAt string `db:"created_at" json:"created_at"` Likes int `db:"likes" json:"likes" validate:"numeric"` Bookmarks int `db:"bookmarks" json:"bookmarks" validate:"numeric"` - ReplyingTo int `db:"replying_to" json:"replying_to" validate:"numeric"` + ReplyingTo null.Int `db:"replying_to" json:"replying_to" validate:"omitempty,numeric"` } + type Bookmark struct { MessageId int `db:"message_id" json:"message_id" validate:"required,numeric"` UserId int `db:"user_id" json:"user_id" validate:"required,numeric"` diff --git a/go.mod b/go.mod index 6be0c03..37610ba 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/go-playground/validator/v10 v10.26.0 github.com/gofiber/fiber/v2 v2.52.8 github.com/gofiber/template/html/v2 v2.1.3 + github.com/guregu/null/v6 v6.0.0 github.com/jmoiron/sqlx v1.4.0 github.com/mattn/go-sqlite3 v1.14.28 github.com/stretchr/testify v1.10.0 diff --git a/go.sum b/go.sum index 3e62bca..b293dde 100644 --- a/go.sum +++ b/go.sum @@ -231,6 +231,8 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= +github.com/guregu/null/v6 v6.0.0 h1:N14VRS+4di81i1PXRiprbQJ9EM9gqBa0+KVMeS/QSjQ= +github.com/guregu/null/v6 v6.0.0/go.mod h1:hrMIhIfrOZeLPZhROSn149tpw2gHkidAqxoXNyeX3iQ= github.com/hairyhenderson/go-codeowners v0.7.0 h1:s0W4wF8bdsBEjTWzwzSlsatSthWtTAF2xLgo4a4RwAo= github.com/hairyhenderson/go-codeowners v0.7.0/go.mod h1:wUlNgQ3QjqC4z8DnM5nnCYVq/icpqXJyJOukKx5U8/Q= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= diff --git a/views/post/new.html b/views/post/new.html index b7aece2..d40563e 100644 --- a/views/post/new.html +++ b/views/post/new.html @@ -1,12 +1,14 @@ - - - - +
+ + + + - - - + + + +