2026-02-20 23:22:10 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-02-23 10:18:34 +00:00
|
|
|
"log"
|
2026-02-21 23:09:34 +00:00
|
|
|
"strconv"
|
2026-02-20 23:22:10 +00:00
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
|
|
|
"lmika.dev/lmika/weiro/models"
|
|
|
|
|
"lmika.dev/lmika/weiro/services/posts"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type PostsHandler struct {
|
|
|
|
|
PostService *posts.Service
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ph PostsHandler) Index(c fiber.Ctx) error {
|
2026-02-23 10:18:34 +00:00
|
|
|
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")
|
2026-02-21 23:09:34 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 10:18:34 +00:00
|
|
|
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,
|
|
|
|
|
})
|
|
|
|
|
}))
|
2026-02-20 23:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ph PostsHandler) New(c fiber.Ctx) error {
|
2026-02-21 23:09:34 +00:00
|
|
|
p := models.Post{
|
|
|
|
|
GUID: models.NewNanoID(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.Render("posts/edit", fiber.Map{
|
|
|
|
|
"post": p,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ph PostsHandler) Edit(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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
post, err := ph.PostService.GetPost(c.Context(), postID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 10:18:34 +00:00
|
|
|
return accepts(c, json(func() any {
|
|
|
|
|
return post
|
|
|
|
|
}), html(func(c fiber.Ctx) error {
|
|
|
|
|
return c.Render("posts/edit", fiber.Map{
|
|
|
|
|
"post": post,
|
|
|
|
|
})
|
|
|
|
|
}))
|
2026-02-20 23:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ph PostsHandler) Update(c fiber.Ctx) error {
|
|
|
|
|
var req posts.CreatePostParams
|
|
|
|
|
if err := c.Bind().Body(&req); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
post, err := ph.PostService.PublishPost(c.Context(), req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 10:18:34 +00:00
|
|
|
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")
|
|
|
|
|
}))
|
2026-02-20 23:22:10 +00:00
|
|
|
}
|