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>
139 lines
4.2 KiB
Go
139 lines
4.2 KiB
Go
package repositories
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/treytartt/honeydue-api/internal/models"
|
|
)
|
|
|
|
// TaskTemplateRepository handles database operations for task templates
|
|
type TaskTemplateRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewTaskTemplateRepository creates a new task template repository
|
|
func NewTaskTemplateRepository(db *gorm.DB) *TaskTemplateRepository {
|
|
return &TaskTemplateRepository{db: db}
|
|
}
|
|
|
|
// GetAll returns all active task templates ordered by category and display order
|
|
func (r *TaskTemplateRepository) GetAll() ([]models.TaskTemplate, error) {
|
|
var templates []models.TaskTemplate
|
|
err := r.db.
|
|
Preload("Category").
|
|
Preload("Frequency").
|
|
Where("is_active = ?", true).
|
|
Order("display_order ASC, title ASC").
|
|
Find(&templates).Error
|
|
return templates, err
|
|
}
|
|
|
|
// GetByCategory returns all active templates for a specific category
|
|
func (r *TaskTemplateRepository) GetByCategory(categoryID uint) ([]models.TaskTemplate, error) {
|
|
var templates []models.TaskTemplate
|
|
err := r.db.
|
|
Preload("Category").
|
|
Preload("Frequency").
|
|
Where("is_active = ? AND category_id = ?", true, categoryID).
|
|
Order("display_order ASC, title ASC").
|
|
Find(&templates).Error
|
|
return templates, err
|
|
}
|
|
|
|
// Search searches templates by title and tags
|
|
func (r *TaskTemplateRepository) Search(query string) ([]models.TaskTemplate, error) {
|
|
var templates []models.TaskTemplate
|
|
escaped := escapeLikeWildcards(strings.ToLower(query))
|
|
searchTerm := "%" + escaped + "%"
|
|
|
|
err := r.db.
|
|
Preload("Category").
|
|
Preload("Frequency").
|
|
Where("is_active = ? AND (LOWER(title) LIKE ? OR LOWER(tags) LIKE ?)", true, searchTerm, searchTerm).
|
|
Order("display_order ASC, title ASC").
|
|
Limit(20). // Limit search results
|
|
Find(&templates).Error
|
|
return templates, err
|
|
}
|
|
|
|
// GetByID returns a single template by ID
|
|
func (r *TaskTemplateRepository) GetByID(id uint) (*models.TaskTemplate, error) {
|
|
var template models.TaskTemplate
|
|
err := r.db.
|
|
Preload("Category").
|
|
Preload("Frequency").
|
|
First(&template, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &template, nil
|
|
}
|
|
|
|
// Create creates a new task template
|
|
func (r *TaskTemplateRepository) Create(template *models.TaskTemplate) error {
|
|
return r.db.Create(template).Error
|
|
}
|
|
|
|
// Update updates an existing task template
|
|
func (r *TaskTemplateRepository) Update(template *models.TaskTemplate) error {
|
|
return r.db.Omit("Category", "Frequency").Save(template).Error
|
|
}
|
|
|
|
// Delete hard deletes a task template
|
|
func (r *TaskTemplateRepository) Delete(id uint) error {
|
|
return r.db.Delete(&models.TaskTemplate{}, id).Error
|
|
}
|
|
|
|
// GetAllIncludingInactive returns all templates including inactive ones (for admin)
|
|
func (r *TaskTemplateRepository) GetAllIncludingInactive() ([]models.TaskTemplate, error) {
|
|
var templates []models.TaskTemplate
|
|
err := r.db.
|
|
Preload("Category").
|
|
Preload("Frequency").
|
|
Order("display_order ASC, title ASC").
|
|
Find(&templates).Error
|
|
return templates, err
|
|
}
|
|
|
|
// Count returns the total count of active templates
|
|
func (r *TaskTemplateRepository) Count() (int64, error) {
|
|
var count int64
|
|
err := r.db.Model(&models.TaskTemplate{}).Where("is_active = ?", true).Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
// GetByRegion returns active templates associated with a specific climate region
|
|
func (r *TaskTemplateRepository) GetByRegion(regionID uint) ([]models.TaskTemplate, error) {
|
|
var templates []models.TaskTemplate
|
|
err := r.db.
|
|
Preload("Category").
|
|
Preload("Frequency").
|
|
Preload("Regions").
|
|
Joins("JOIN task_tasktemplate_regions ON task_tasktemplate_regions.task_template_id = task_tasktemplate.id").
|
|
Where("task_tasktemplate_regions.climate_region_id = ? AND task_tasktemplate.is_active = ?", regionID, true).
|
|
Order("task_tasktemplate.display_order ASC, task_tasktemplate.title ASC").
|
|
Find(&templates).Error
|
|
return templates, err
|
|
}
|
|
|
|
// GetGroupedByCategory returns templates grouped by category name
|
|
func (r *TaskTemplateRepository) GetGroupedByCategory() (map[string][]models.TaskTemplate, error) {
|
|
templates, err := r.GetAll()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := make(map[string][]models.TaskTemplate)
|
|
for _, t := range templates {
|
|
categoryName := "Uncategorized"
|
|
if t.Category != nil {
|
|
categoryName = t.Category.Name
|
|
}
|
|
result[categoryName] = append(result[categoryName], t)
|
|
}
|
|
|
|
return result, nil
|
|
}
|