feat: add categories admin UI with CRUD
Wire up categories service, add CategoriesHandler with full CRUD, create index/edit templates, register routes in server.go, and add Categories nav link. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
3c80f63a55
commit
ffa86b12e9
6 changed files with 198 additions and 0 deletions
101
handlers/categories.go
Normal file
101
handlers/categories.go
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/services/categories"
|
||||
)
|
||||
|
||||
type CategoriesHandler struct {
|
||||
CategoryService *categories.Service
|
||||
}
|
||||
|
||||
func (ch CategoriesHandler) Index(c fiber.Ctx) error {
|
||||
cats, err := ch.CategoryService.ListCategoriesWithCounts(c.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Render("categories/index", fiber.Map{
|
||||
"categories": cats,
|
||||
})
|
||||
}
|
||||
|
||||
func (ch CategoriesHandler) New(c fiber.Ctx) error {
|
||||
cat := models.Category{
|
||||
GUID: models.NewNanoID(),
|
||||
}
|
||||
return c.Render("categories/edit", fiber.Map{
|
||||
"category": cat,
|
||||
"isNew": true,
|
||||
})
|
||||
}
|
||||
|
||||
func (ch CategoriesHandler) Edit(c fiber.Ctx) error {
|
||||
catID, err := strconv.ParseInt(c.Params("categoryID"), 10, 64)
|
||||
if err != nil {
|
||||
return fiber.ErrBadRequest
|
||||
}
|
||||
|
||||
cat, err := ch.CategoryService.GetCategory(c.Context(), catID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Render("categories/edit", fiber.Map{
|
||||
"category": cat,
|
||||
"isNew": false,
|
||||
})
|
||||
}
|
||||
|
||||
func (ch CategoriesHandler) Create(c fiber.Ctx) error {
|
||||
var req categories.CreateCategoryParams
|
||||
if err := c.Bind().Body(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := ch.CategoryService.CreateCategory(c.Context(), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
site := models.MustGetSite(c.Context())
|
||||
return c.Redirect().To(fmt.Sprintf("/sites/%v/categories", site.ID))
|
||||
}
|
||||
|
||||
func (ch CategoriesHandler) Update(c fiber.Ctx) error {
|
||||
catID, err := strconv.ParseInt(c.Params("categoryID"), 10, 64)
|
||||
if err != nil {
|
||||
return fiber.ErrBadRequest
|
||||
}
|
||||
|
||||
var req categories.CreateCategoryParams
|
||||
if err := c.Bind().Body(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = ch.CategoryService.UpdateCategory(c.Context(), catID, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
site := models.MustGetSite(c.Context())
|
||||
return c.Redirect().To(fmt.Sprintf("/sites/%v/categories", site.ID))
|
||||
}
|
||||
|
||||
func (ch CategoriesHandler) Delete(c fiber.Ctx) error {
|
||||
catID, err := strconv.ParseInt(c.Params("categoryID"), 10, 64)
|
||||
if err != nil {
|
||||
return fiber.ErrBadRequest
|
||||
}
|
||||
|
||||
if err := ch.CategoryService.DeleteCategory(c.Context(), catID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
site := models.MustGetSite(c.Context())
|
||||
return c.Redirect().To(fmt.Sprintf("/sites/%v/categories", site.ID))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue