First cut of the link fields

This commit is contained in:
Leon Mika 2025-03-02 10:46:36 +11:00
parent c5925e16e0
commit 38ebb21a34
8 changed files with 145 additions and 35 deletions

View file

@ -3,10 +3,12 @@ package handlers
import (
"errors"
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/gofiber/fiber/v3"
"lmika.dev/lmika/hugo-cms/models"
"lmika.dev/lmika/hugo-cms/services/posts"
"net/http"
"strings"
)
type Post struct {
@ -35,18 +37,12 @@ func (h *Post) New(c fiber.Ctx) error {
func (h *Post) Create(c fiber.Ctx) error {
site := GetSite(c)
var req struct {
Title string `json:"title" form:"title"`
Body string `json:"body" form:"body"`
}
if err := c.Bind().Body(&req); err != nil {
req, err := h.parseReqToNewPost(c)
if err != nil {
return err
}
_, err := h.Post.Create(c.Context(), site, posts.NewPost{
Title: req.Title,
Body: req.Body,
})
_, err = h.Post.Create(c.Context(), site, req)
if err != nil {
return err
}
@ -82,14 +78,13 @@ func (h *Post) Update(c fiber.Ctx) error {
return errors.New("postId is required")
}
var req struct {
Title string `json:"title" form:"title"`
Body string `json:"body" form:"body"`
}
if err := c.Bind().Body(&req); err != nil {
req, err := h.parseReqToNewPost(c)
if err != nil {
return err
}
spew.Dump(req)
post, err := h.Post.GetPost(c.Context(), postID)
if err != nil {
return err
@ -128,3 +123,26 @@ func (h *Post) Delete(c fiber.Ctx) error {
}),
)
}
func (h *Post) parseReqToNewPost(c fiber.Ctx) (req posts.NewPost, err error) {
reqMap := make(map[string]string)
if err := c.Bind().Body(&reqMap); err != nil {
return req, err
}
for k, v := range reqMap {
switch {
case k == "title":
req.Title = v
case k == "body":
req.Body = v
case strings.HasPrefix(k, "params."):
kk := strings.TrimPrefix(k, "params.")
if req.Params == nil {
req.Params = make(map[string]string)
}
req.Params[kk] = v
}
}
return req, nil
}