- Add TaskTemplate model with category and frequency support - Add task template repository with CRUD and search operations - Add task template service layer - Add public API endpoints for templates (no auth required): - GET /api/tasks/templates/ - list all templates - GET /api/tasks/templates/grouped/ - templates grouped by category - GET /api/tasks/templates/search/?q= - search templates - GET /api/tasks/templates/by-category/:id/ - templates by category - GET /api/tasks/templates/:id/ - single template - Add admin panel for task template management (CRUD) - Add admin API endpoints for templates - Add seed file with predefined task templates - Add i18n translations for template errors 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
107 lines
3.1 KiB
Go
107 lines
3.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/treytartt/casera-api/internal/i18n"
|
|
"github.com/treytartt/casera-api/internal/services"
|
|
)
|
|
|
|
// TaskTemplateHandler handles task template endpoints
|
|
type TaskTemplateHandler struct {
|
|
templateService *services.TaskTemplateService
|
|
}
|
|
|
|
// NewTaskTemplateHandler creates a new task template handler
|
|
func NewTaskTemplateHandler(templateService *services.TaskTemplateService) *TaskTemplateHandler {
|
|
return &TaskTemplateHandler{
|
|
templateService: templateService,
|
|
}
|
|
}
|
|
|
|
// GetTemplates handles GET /api/tasks/templates/
|
|
// Returns all active task templates as a flat list
|
|
func (h *TaskTemplateHandler) GetTemplates(c *gin.Context) {
|
|
templates, err := h.templateService.GetAll()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": i18n.LocalizedMessage(c, "error.failed_to_fetch_templates")})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, templates)
|
|
}
|
|
|
|
// GetTemplatesGrouped handles GET /api/tasks/templates/grouped/
|
|
// Returns all templates grouped by category
|
|
func (h *TaskTemplateHandler) GetTemplatesGrouped(c *gin.Context) {
|
|
grouped, err := h.templateService.GetGrouped()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": i18n.LocalizedMessage(c, "error.failed_to_fetch_templates")})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, grouped)
|
|
}
|
|
|
|
// SearchTemplates handles GET /api/tasks/templates/search/
|
|
// Searches templates by query string
|
|
func (h *TaskTemplateHandler) SearchTemplates(c *gin.Context) {
|
|
query := c.Query("q")
|
|
if query == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Query parameter 'q' is required"})
|
|
return
|
|
}
|
|
|
|
if len(query) < 2 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Query must be at least 2 characters"})
|
|
return
|
|
}
|
|
|
|
templates, err := h.templateService.Search(query)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": i18n.LocalizedMessage(c, "error.failed_to_search_templates")})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, templates)
|
|
}
|
|
|
|
// GetTemplatesByCategory handles GET /api/tasks/templates/by-category/:category_id/
|
|
// Returns templates for a specific category
|
|
func (h *TaskTemplateHandler) GetTemplatesByCategory(c *gin.Context) {
|
|
categoryID, err := strconv.ParseUint(c.Param("category_id"), 10, 32)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid category ID"})
|
|
return
|
|
}
|
|
|
|
templates, err := h.templateService.GetByCategory(uint(categoryID))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": i18n.LocalizedMessage(c, "error.failed_to_fetch_templates")})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, templates)
|
|
}
|
|
|
|
// GetTemplate handles GET /api/tasks/templates/:id/
|
|
// Returns a single template by ID
|
|
func (h *TaskTemplateHandler) GetTemplate(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid template ID"})
|
|
return
|
|
}
|
|
|
|
template, err := h.templateService.GetByID(uint(id))
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": i18n.LocalizedMessage(c, "error.template_not_found")})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, template)
|
|
}
|