I can now submit a form and store it in the database.

This commit is contained in:
Zed A. Shaw 2025-06-03 23:25:29 -04:00
parent eaaab2ac0b
commit 4045799ab9
4 changed files with 87 additions and 10 deletions

27
main.go
View file

@ -12,9 +12,9 @@ import (
type Link struct {
Id int `db:"id" json:"id"`
StreamId int `db:"stream_id" json:"stream_id"`
Url string `db:"url" json:"url"`
Description string `db:"description" json:"description"`
StreamId int `db:"stream_id" json:"stream_id" form:"stream_id"`
Url string `db:"url" json:"url" form:"url"`
Description string `db:"description" json:"description" form:"description"`
}
type Stream struct {
@ -74,13 +74,32 @@ func main() {
return GetJson[Stream](db, c, err, sql, args...)
})
app.Get("/api/stream/:id/links", func (c *fiber.Ctx) error {
sql, args, err := sq.Select("*").From("stream_link").Where("stream_id", c.Params("id")).ToSql()
return SelectJson[Link](db, c, err, sql, args...)
})
app.Post("/api/link", func (c *fiber.Ctx) error {
link := new(Link)
if err := c.BodyParser(link); err != nil {
log.Println(err);
return c.Redirect("/live/")
}
sql, args, err := sq.Insert("stream_link").Columns("stream_id", "url", "description").Values(link.StreamId, link.Url, link.Description).ToSql()
if err != nil {
log.Println(err)
return c.Redirect("/live/")
}
db.MustExec(sql, args...)
return c.Redirect("/live/")
})
app.Static("/", "./public")
log.Fatal(app.Listen(":5001"))