owl-blogs/web/admin_interaction_handler.go

42 lines
960 B
Go
Raw Permalink Normal View History

2023-08-09 18:48:16 +00:00
package web
import (
"owl-blogs/app/repository"
"github.com/gofiber/fiber/v2"
)
type AdminInteractionHandler struct {
interactionRepo repository.InteractionRepository
configRepo repository.ConfigRepository
}
func NewAdminInteractionHandler(configRepo repository.ConfigRepository, interactionRepo repository.InteractionRepository) *AdminInteractionHandler {
return &AdminInteractionHandler{
interactionRepo: interactionRepo,
configRepo: configRepo,
}
}
func (h *AdminInteractionHandler) HandleDelete(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
id := c.Params("id")
inter, err := h.interactionRepo.FindById(id)
entryId := inter.EntryID()
if err != nil {
return err
}
confirm := c.FormValue("confirm")
if confirm != "on" {
return c.Redirect("/posts/" + inter.ID() + "/")
}
err = h.interactionRepo.Delete(inter)
if err != nil {
return err
}
return c.Redirect("/posts/" + entryId + "/")
}