2025-01-26 23:19:31 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2025-01-27 03:23:54 +00:00
|
|
|
"context"
|
2025-01-26 23:19:31 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"lmika.dev/lmika/hugo-crm/services/sites"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Site struct {
|
|
|
|
Site *sites.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) Create() fiber.Handler {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
site, err := s.Site.CreateSite(c.UserContext(), "New Site "+time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(fmt.Sprintf("/sites/%v", site.ID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) Show() fiber.Handler {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
id, err := c.ParamsInt("siteId")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
site, err := s.Site.GetSite(c.UserContext(), id)
|
2025-01-27 03:23:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-01-26 23:19:31 +00:00
|
|
|
|
|
|
|
return c.Render("sites/index", fiber.Map{
|
|
|
|
"site": site,
|
|
|
|
}, "layouts/main")
|
|
|
|
}
|
|
|
|
}
|
2025-01-27 03:23:54 +00:00
|
|
|
|
|
|
|
func (s *Site) WithSite() fiber.Handler {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
id, err := c.ParamsInt("siteId")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
site, err := s.Site.GetSite(c.UserContext(), id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SetUserContext(context.WithValue(c.UserContext(), siteKey, site))
|
|
|
|
return c.Next()
|
|
|
|
}
|
|
|
|
}
|