summaryrefslogtreecommitdiff
path: root/internal/api/templates.go
blob: 024a6df2cd66569bb741bd74114da2c57af2f441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package api

import (
	"encoding/json"
	"errors"
	"net/http"
	"time"

	"github.com/google/uuid"
	"github.com/thepeterstone/claudomator/internal/storage"
	"github.com/thepeterstone/claudomator/internal/task"
)

func (s *Server) handleListTemplates(w http.ResponseWriter, r *http.Request) {
	templates, err := s.store.ListTemplates()
	if err != nil {
		writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
		return
	}
	if templates == nil {
		templates = []*storage.Template{}
	}
	writeJSON(w, http.StatusOK, templates)
}

func (s *Server) handleCreateTemplate(w http.ResponseWriter, r *http.Request) {
	var input struct {
		Name        string            `json:"name"`
		Description string            `json:"description"`
		Agent      task.AgentConfig `json:"agent"`
		Timeout     string            `json:"timeout"`
		Priority    string            `json:"priority"`
		Tags        []string          `json:"tags"`
	}
	if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
		writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON: " + err.Error()})
		return
	}
	if input.Name == "" {
		writeJSON(w, http.StatusBadRequest, map[string]string{"error": "name is required"})
		return
	}

	now := time.Now().UTC()
	tmpl := &storage.Template{
		ID:          uuid.New().String(),
		Name:        input.Name,
		Description: input.Description,
		Agent:       input.Agent,
		Timeout:     input.Timeout,
		Priority:    input.Priority,
		Tags:        input.Tags,
		CreatedAt:   now,
		UpdatedAt:   now,
	}
	if tmpl.Agent.Type == "" {
		tmpl.Agent.Type = "claude"
	}
	if tmpl.Priority == "" {
		tmpl.Priority = "normal"
	}
	if tmpl.Tags == nil {
		tmpl.Tags = []string{}
	}

	if err := s.store.CreateTemplate(tmpl); err != nil {
		writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
		return
	}
	writeJSON(w, http.StatusCreated, tmpl)
}

func (s *Server) handleGetTemplate(w http.ResponseWriter, r *http.Request) {
	id := r.PathValue("id")
	tmpl, err := s.store.GetTemplate(id)
	if err != nil {
		if errors.Is(err, storage.ErrTemplateNotFound) {
			writeJSON(w, http.StatusNotFound, map[string]string{"error": "template not found"})
			return
		}
		writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
		return
	}
	writeJSON(w, http.StatusOK, tmpl)
}

// handleUpdateTemplate fully replaces all fields of the template identified by {id}.
// All fields from the request body overwrite existing values; name is required.
func (s *Server) handleUpdateTemplate(w http.ResponseWriter, r *http.Request) {
	id := r.PathValue("id")
	existing, err := s.store.GetTemplate(id)
	if err != nil {
		if errors.Is(err, storage.ErrTemplateNotFound) {
			writeJSON(w, http.StatusNotFound, map[string]string{"error": "template not found"})
			return
		}
		writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
		return
	}

	var input struct {
		Name        string            `json:"name"`
		Description string            `json:"description"`
		Agent      task.AgentConfig `json:"agent"`
		Timeout     string            `json:"timeout"`
		Priority    string            `json:"priority"`
		Tags        []string          `json:"tags"`
	}
	if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
		writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON: " + err.Error()})
		return
	}
	if input.Name == "" {
		writeJSON(w, http.StatusBadRequest, map[string]string{"error": "name is required"})
		return
	}

	existing.Name = input.Name
	existing.Description = input.Description
	existing.Agent = input.Agent
	if existing.Agent.Type == "" {
		existing.Agent.Type = "claude"
	}
	existing.Timeout = input.Timeout
	existing.Priority = input.Priority
	if input.Tags != nil {
		existing.Tags = input.Tags
	}
	existing.UpdatedAt = time.Now().UTC()

	if err := s.store.UpdateTemplate(existing); err != nil {
		writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
		return
	}
	writeJSON(w, http.StatusOK, existing)
}

func (s *Server) handleDeleteTemplate(w http.ResponseWriter, r *http.Request) {
	id := r.PathValue("id")
	err := s.store.DeleteTemplate(id)
	if err != nil {
		if errors.Is(err, storage.ErrTemplateNotFound) {
			writeJSON(w, http.StatusNotFound, map[string]string{"error": "template not found"})
			return
		}
		writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
		return
	}
	w.WriteHeader(http.StatusNoContent)
}