Implement posting a message.

master
Zed A. Shaw 2 weeks ago
parent ebf4e6436b
commit aa1ab6182c
  1. 15
      api/handlers.go
  2. 8
      data/models.go
  3. 1
      go.mod
  4. 2
      go.sum
  5. 16
      views/post/new.html

@ -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"))

@ -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"`

@ -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

@ -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=

@ -1,12 +1,14 @@
<block class="!gap-0 border-t-1 border-gray-600">
<bar class="justify-end">
<button class="!p-1" type="discard">Discard</button>
<button class="!p-1" type="button">Post</button>
</bar>
<form action="/api/message/new" method="POST">
<bar class="justify-end">
<button class="!p-1" type="button">Discard</button>
<button class="!p-1" type="submit">Post</button>
</bar>
<block class="!p-0 gap-2 mb-2">
<textarea class="border-1 border-gray-500" rows="10" placeholder="Type your message"></textarea>
</block>
<block class="!p-0 gap-2 mb-2">
<textarea name="text" class="border-1 border-gray-500" rows="10" placeholder="Type your message"></textarea>
</block>
</for>
<bar class="!p-1 mt-2 bg-gray-400 justify-evenly rounded-full">
<img src="/icons/message-square.svg" />

Loading…
Cancel
Save