Total rebrand across all Go API source files: - Go module path: casera-api -> honeydue-api - All imports updated (130+ files) - Docker: containers, images, networks renamed - Email templates: support email, noreply, icon URL - Domains: casera.app/mycrib.treytartt.com -> honeyDue.treytartt.com - Bundle IDs: com.tt.casera -> com.tt.honeyDue - IAP product IDs updated - Landing page, admin panel, config defaults - Seeds, CI workflows, Makefile, docs - Database table names preserved (no migration needed) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
142 lines
4.4 KiB
Go
142 lines
4.4 KiB
Go
package responses
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/treytartt/honeydue-api/internal/models"
|
|
)
|
|
|
|
// TaskTemplateResponse represents a task template in the API response
|
|
type TaskTemplateResponse struct {
|
|
ID uint `json:"id"`
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
CategoryID *uint `json:"category_id"`
|
|
Category *TaskCategoryResponse `json:"category,omitempty"`
|
|
FrequencyID *uint `json:"frequency_id"`
|
|
Frequency *TaskFrequencyResponse `json:"frequency,omitempty"`
|
|
IconIOS string `json:"icon_ios"`
|
|
IconAndroid string `json:"icon_android"`
|
|
Tags []string `json:"tags"`
|
|
DisplayOrder int `json:"display_order"`
|
|
IsActive bool `json:"is_active"`
|
|
RegionID *uint `json:"region_id,omitempty"`
|
|
RegionName string `json:"region_name,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// TaskTemplateCategoryGroup represents templates grouped by category
|
|
type TaskTemplateCategoryGroup struct {
|
|
CategoryName string `json:"category_name"`
|
|
CategoryID *uint `json:"category_id"`
|
|
Templates []TaskTemplateResponse `json:"templates"`
|
|
Count int `json:"count"`
|
|
}
|
|
|
|
// TaskTemplatesGroupedResponse represents all templates grouped by category
|
|
type TaskTemplatesGroupedResponse struct {
|
|
Categories []TaskTemplateCategoryGroup `json:"categories"`
|
|
TotalCount int `json:"total_count"`
|
|
}
|
|
|
|
// NewTaskTemplateResponse creates a TaskTemplateResponse from a model
|
|
func NewTaskTemplateResponse(t *models.TaskTemplate) TaskTemplateResponse {
|
|
resp := TaskTemplateResponse{
|
|
ID: t.ID,
|
|
Title: t.Title,
|
|
Description: t.Description,
|
|
CategoryID: t.CategoryID,
|
|
FrequencyID: t.FrequencyID,
|
|
IconIOS: t.IconIOS,
|
|
IconAndroid: t.IconAndroid,
|
|
Tags: parseTags(t.Tags),
|
|
DisplayOrder: t.DisplayOrder,
|
|
IsActive: t.IsActive,
|
|
CreatedAt: t.CreatedAt,
|
|
UpdatedAt: t.UpdatedAt,
|
|
}
|
|
|
|
if t.Category != nil {
|
|
resp.Category = NewTaskCategoryResponse(t.Category)
|
|
}
|
|
if t.Frequency != nil {
|
|
resp.Frequency = NewTaskFrequencyResponse(t.Frequency)
|
|
}
|
|
|
|
if len(t.Regions) > 0 {
|
|
resp.RegionID = &t.Regions[0].ID
|
|
resp.RegionName = t.Regions[0].Name
|
|
}
|
|
|
|
return resp
|
|
}
|
|
|
|
// NewTaskTemplateListResponse creates a list of task template responses
|
|
func NewTaskTemplateListResponse(templates []models.TaskTemplate) []TaskTemplateResponse {
|
|
results := make([]TaskTemplateResponse, len(templates))
|
|
for i, t := range templates {
|
|
results[i] = NewTaskTemplateResponse(&t)
|
|
}
|
|
return results
|
|
}
|
|
|
|
// NewTaskTemplatesGroupedResponse creates a grouped response from templates
|
|
func NewTaskTemplatesGroupedResponse(templates []models.TaskTemplate) TaskTemplatesGroupedResponse {
|
|
// Group by category
|
|
categoryMap := make(map[string]*TaskTemplateCategoryGroup)
|
|
categoryOrder := []string{} // To maintain order
|
|
|
|
for _, t := range templates {
|
|
categoryName := "Uncategorized"
|
|
var categoryID *uint
|
|
if t.Category != nil {
|
|
categoryName = t.Category.Name
|
|
categoryID = &t.Category.ID
|
|
}
|
|
|
|
if _, exists := categoryMap[categoryName]; !exists {
|
|
categoryMap[categoryName] = &TaskTemplateCategoryGroup{
|
|
CategoryName: categoryName,
|
|
CategoryID: categoryID,
|
|
Templates: []TaskTemplateResponse{},
|
|
}
|
|
categoryOrder = append(categoryOrder, categoryName)
|
|
}
|
|
|
|
categoryMap[categoryName].Templates = append(categoryMap[categoryName].Templates, NewTaskTemplateResponse(&t))
|
|
}
|
|
|
|
// Build ordered result
|
|
categories := make([]TaskTemplateCategoryGroup, len(categoryOrder))
|
|
totalCount := 0
|
|
for i, name := range categoryOrder {
|
|
group := categoryMap[name]
|
|
group.Count = len(group.Templates)
|
|
totalCount += group.Count
|
|
categories[i] = *group
|
|
}
|
|
|
|
return TaskTemplatesGroupedResponse{
|
|
Categories: categories,
|
|
TotalCount: totalCount,
|
|
}
|
|
}
|
|
|
|
// parseTags splits a comma-separated tags string into a slice
|
|
func parseTags(tags string) []string {
|
|
if tags == "" {
|
|
return []string{}
|
|
}
|
|
parts := strings.Split(tags, ",")
|
|
result := make([]string, 0, len(parts))
|
|
for _, p := range parts {
|
|
trimmed := strings.TrimSpace(p)
|
|
if trimmed != "" {
|
|
result = append(result, trimmed)
|
|
}
|
|
}
|
|
return result
|
|
}
|