Have got soft and hard deleting

This commit is contained in:
Leon Mika 2026-02-23 21:18:34 +11:00
parent aef3bb6a1e
commit 3ea5823ca0
27 changed files with 588 additions and 55 deletions

35
handlers/accepts.go Normal file
View file

@ -0,0 +1,35 @@
package handlers
import (
"github.com/gofiber/fiber/v3"
)
type acceptor struct {
canAccept func(ctx fiber.Ctx) bool
acceptFn func(ctx fiber.Ctx) error
}
func accepts(ctx fiber.Ctx, acceptors ...acceptor) error {
for _, a := range acceptors {
if a.canAccept(ctx) {
return a.acceptFn(ctx)
}
}
return fiber.ErrNotFound
}
func json(fn func() any) acceptor {
return acceptor{
canAccept: func(ctx fiber.Ctx) bool { return ctx.AcceptsJSON() && !ctx.AcceptsHTML() },
acceptFn: func(ctx fiber.Ctx) error {
return ctx.Status(fiber.StatusOK).JSON(fn())
},
}
}
func html(fn func(ctx fiber.Ctx) error) acceptor {
return acceptor{
canAccept: func(ctx fiber.Ctx) bool { return ctx.AcceptsHTML() },
acceptFn: fn,
}
}

View file

@ -2,6 +2,7 @@ package handlers
import (
"fmt"
"log"
"strconv"
"github.com/gofiber/fiber/v3"
@ -14,14 +15,26 @@ type PostsHandler struct {
}
func (ph PostsHandler) Index(c fiber.Ctx) error {
posts, err := ph.PostService.ListPosts(c.Context())
var req struct {
Filter string `query:"filter"`
}
if err := c.Bind().Query(&req); err != nil {
return fiber.ErrBadRequest
}
posts, err := ph.PostService.ListPosts(c.Context(), req.Filter == "deleted")
if err != nil {
return err
}
return c.Render("posts/index", fiber.Map{
"posts": posts,
})
return accepts(c, json(func() any {
return posts
}), html(func(c fiber.Ctx) error {
return c.Render("posts/index", fiber.Map{
"req": req,
"posts": posts,
})
}))
}
func (ph PostsHandler) New(c fiber.Ctx) error {
@ -49,9 +62,13 @@ func (ph PostsHandler) Edit(c fiber.Ctx) error {
return err
}
return c.Render("posts/edit", fiber.Map{
"post": post,
})
return accepts(c, json(func() any {
return post
}), html(func(c fiber.Ctx) error {
return c.Render("posts/edit", fiber.Map{
"post": post,
})
}))
}
func (ph PostsHandler) Update(c fiber.Ctx) error {
@ -65,5 +82,77 @@ func (ph PostsHandler) Update(c fiber.Ctx) error {
return err
}
return c.Redirect().To(fmt.Sprintf("/sites/%v/posts", post.SiteID))
return accepts(c, json(func() any {
// TODO: should be created if brand new
return post
}), html(func(c fiber.Ctx) error {
return c.Redirect().To(fmt.Sprintf("/sites/%v/posts", post.SiteID))
}))
}
func (ph PostsHandler) Patch(c fiber.Ctx) error {
log.Println("PATCH")
postIDStr := c.Params("postID")
if postIDStr == "" {
return fiber.ErrBadRequest
}
postID, err := strconv.ParseInt(postIDStr, 10, 64)
if err != nil {
return fiber.ErrBadRequest
}
var req struct {
Action string `json:"action"`
}
if err := c.Bind().Body(&req); err != nil {
return err
}
log.Println("Request")
switch req.Action {
case "restore":
if err := ph.PostService.RestorePost(c.Context(), postID); err != nil {
return err
}
default:
return fiber.ErrBadRequest
}
return accepts(c, json(func() any {
return struct{}{}
}), html(func(c fiber.Ctx) error {
return c.Redirect().To(fmt.Sprintf("/sites/%v/posts"))
}))
}
func (ph PostsHandler) Delete(c fiber.Ctx) error {
postIDStr := c.Params("postID")
if postIDStr == "" {
return fiber.ErrBadRequest
}
postID, err := strconv.ParseInt(postIDStr, 10, 64)
if err != nil {
return fiber.ErrBadRequest
}
var req struct {
Hard bool `query:"hard"`
}
if err := c.Bind().Query(&req); err != nil {
return err
}
if err := ph.PostService.DeletePost(c.Context(), postID, req.Hard); err != nil {
return err
}
return accepts(c, json(func() any {
return fiber.Map{}
}), html(func(c fiber.Ctx) error {
return c.Redirect().To("/sites")
}))
}