hako/internal/server/handlers/categories.go

51 lines
1.3 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"git.nakama.town/fmartingr/hako/internal/model"
"git.nakama.town/fmartingr/hako/internal/server/webcontext"
)
// CategoryHandler handles category-related HTTP requests
type CategoryHandler struct {
deps model.Dependencies
}
// NewCategoryHandler creates a new CategoryHandler
func NewCategoryHandler(deps model.Dependencies) *CategoryHandler {
return &CategoryHandler{
deps: deps,
}
}
// HandleListCategories handles GET /api/v1/categories
func (h *CategoryHandler) HandleListCategories(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
c := webcontext.NewWebContext(w, r)
// List categories
categories, err := h.deps.Domains().Categories().ListCategories(c.Context())
if err != nil {
http.Error(w, "Failed to list categories", http.StatusInternalServerError)
return
}
// Build response
items := make([]model.CategoryResponse, 0, len(categories))
for _, category := range categories {
items = append(items, model.CategoryResponse{
ID: category.ID,
Name: category.Name,
Icon: category.Icon,
})
}
c.ResponseWriter().Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(c.ResponseWriter()).Encode(items)
}